Shell_文件恢复脚本

Shell文件恢复脚本

该脚本为快速恢复本地文件数据到指定服务器指定位置

配置文件目录:

config

功能:

1
恢复本地文件数据到指定服务器指定位置, 交互型命令行输入

常用命令

1
2
3
sudo chmod 600 env/id_rsa_114
su root
./resolve.sh

resolve.sh脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#! /bin/sh

# --------------------------方法区域-------------------------------------

# * [连接远端执行命令(使用秘钥文件_无密码)]
execOrigin(){
echo "${current_time}ssh -i ${identify_file} -o StrictHostKeyChecking=no -p ${PORT} ${USER}@${IP} ${1}" >> "${2}"
ssh -i "${identify_file}" -o StrictHostKeyChecking=no -p "${PORT}" "${USER}@${IP}" "${1}"
}

#/**
# * [使用scp传输文件到远端]
# * @parame ${1} [需要传输的文件清单]
# * @parame ${2} [最大尝试次数]
# */
scpRecover(){
cat ${1} | while read LINE ; do
file_path=${LINE}
file_name=`basename ${LINE}`
count_times=0
# 尝试最多三次拉取远端文件
while [ ${count_times} -lt ${2} ]; do
count_times=`expr ${count_times} + 1`
scp -r -v -p -i ${identify_file} -o StrictHostKeyChecking=no -P ${PORT} -l ${transfer_speed} ${file_path} ${USER}@${IP}:${origin_tmp_dir}
cmd_result=$?
# 推送成功后跳出
if [ ${cmd_result} -eq 0 ]; then
echo "${current_time}第${count_times}次推送数据${file_path}成功"
echo "${current_time}第${count_times}次推送数据${file_path}成功" >> ${log_file}
break
fi
sleep 5
done

# 判读是否成功移动,写入日志
if [ ${cmd_result} -eq 0 ]; then
echo "${current_time}推送文件到远端缓存目录 ${origin_tmp_dir} 成功"
echo "${current_time}推送文件到远端缓存目录 ${origin_tmp_dir} 成功" >> ${log_file}
else
echo ${cmd_result} > ${dir_choice2}
${current_time}推送文件到远端缓存目录 ${origin_tmp_dir}
${current_time}推送文件到远端缓存目录 ${origin_tmp_dir} >> ${log_file}
fi
done
if [ ! -f ${dir_choice2} ]; then
touch ${dir_choice2}
fi
final_result=`cat ${dir_choice2}`
rm -f ${dir_choice2}
if [ "${final_result}" != '' ]; then
echo "${IP}:${file_path}" "!!!${current_time}备份文件恢复有错误,请查看日志详情"
else
echo "${IP}:${file_path}" "!!!${current_time}备份文件恢复成功"
fi

}

#/**
# * [使用scp传输文件到远端]
# * @parame ${1} [需要传输的文件清单]
# * @parame ${2} [最大尝试次数]
# */
scpRecoverByPass(){
count_times=0
# 尝试最多三次推送远端文件
while [ ${count_times} -lt ${2} ]; do
count_times=`expr ${count_times} + 1`
scp -r -v -p -o StrictHostKeyChecking=no -P ${PORT} -l ${transfer_speed} ${1} ${USER}@${IP}:${origin_tmp_dir}
cmd_result=$?
# 推送成功后跳出
if [ ${cmd_result} -eq 0 ]; then
echo "${current_time}第${count_times}次推送数据${1}成功"
echo "${current_time}第${count_times}次推送数据${1}成功" >> ${log_file}
break
fi
sleep 5
done

# 判读是否成功移动,写入日志
if [ ${cmd_result} -eq 0 ]; then
echo "${current_time}推送文件到远端缓存目录 ${origin_tmp_dir} 成功"
echo "${current_time}推送文件到远端缓存目录 ${origin_tmp_dir} 成功" >> ${log_file}
else
echo ${cmd_result} > ${dir_choice2}
${current_time}推送文件到远端缓存目录 ${origin_tmp_dir}
${current_time}推送文件到远端缓存目录 ${origin_tmp_dir} >> ${log_file}
fi

if [ ! -f ${dir_choice2} ]; then
touch ${dir_choice2}
fi
final_result=`cat ${dir_choice2}`
rm -f ${dir_choice2}
if [ "${final_result}" != '' ]; then
echo "${IP}:${1}" "!!!${current_time}备份文件恢复有错误,请查看日志详情"
else
echo "${IP}:${1}" "!!!${current_time}备份文件恢复成功"
fi

}




# --------------------------方法区域-------------------------------------

# [[ 定义公共参数 ]]
## 引入基础配置文件
. ./baseconfig
echo "使用本地备份文件目录: ${backfile}"
## 当前时间
current_time=`date "+%Y-%m-%d_%H:%M:%S"`

## 本地缓存文件
project_list=${current_time}project.txt
project_choiced=${current_time}choice.txt
file_choice=${current_time}file.txt
cmd_result=${current_time}cmd.txt
dir_choice2=${current_time}dir.txt
log_file=log.txt




## 创建缓存文件
if [ ! -f ${log_file} ]; then
touch ${log_file}
fi
if [ ! -f ${dir_file} ]; then
touch ${dir_file}
fi

# [[ 开始流程-用户自定选项 ]]
echo '欢迎使用图凌备份还原系统'

## 1.列出现有备份文件夹
echo "请选择你需要还原的备份"
count=1
find ${backfile} -maxdepth 1 -mindepth 1 -type d | while read line; do
dirname=`basename ${line}`
echo "${count}.${dirname}" > ${project_list}
count=`expr ${count} + 1`
done
### 1.1空值判定(文件夹为空)
if [ ! -s ${project_list} ]; then
echo "指定备份文件夹${backfile}没有备份文件,退出"
rm -f ${project_list}
exit 000001
fi
### 1.2输出备份文件夹内容
echo `cat ${project_list}`

## 2.依用户输入获取使用项目包名
read -p "输入要做恢复操作的项目备份包文件夹编号(数字) ## " index
line_index=${index}
### 2.1输入空值验证
if [ "${line_index}" = '' ]; then
echo "输入编号不能为空"
rm -f ${project_list}
exit 000002
fi
echo '输入字符为'${line_index}
### 2.2匹配输入编号对应项目
cat ${project_list} | while read project; do
txt_index=`expr substr "${project}" 1 1` # 获取字符串第一位
txt_file=`expr substr "${project}" 3 300` # 获取第三位到300位

if [ "${line_index}" -eq "${txt_index}" ]; then
echo ${txt_file} > ${project_choiced}
break
fi
done
#### 2.2.1回收缓存文件
rm -f ${project_list}
### 2.3验证是否有匹配值
request_file=`cat ${project_choiced}`
if [ "${request_file}" = '' ]; then
echo '请输入正确的编号'
rm -f ${project_choiced}
exit 000003
else
echo "您选定的为${request_file}项目"
fi
#### 2.3.1回收缓存文件
rm -f ${project_choiced}

## 3.确认使用项目
read -p "是否确认使用该备份恢复项目(y默认/n) ## " confirm
if [ "${confirm}" = 'y' ] || [ "${confirm}" = 'Y' ] || [ "${confirm}" = '' ]; then
back_project="${backfile}/${request_file}"
echo "备份项目所在目录为${back_project}"
else
echo '放弃使用,中断'
exit 000004
fi

## 4.确认配置文件
project_file=${request_file}
config_file="config/${request_file}"
echo "该备份项目配置文件为: ${backfile}/${config_file}"
if [ ! -f ${config_file} ]; then
echo '该项目没有默认配置文件,请创建后继续'
exit 000005
fi
echo "引入配置文件:${project_file}"
. ${config_file}
## 4.1创建项目执行远端命令日志
log_record="${NAME}.txt"
if [ ! -f ${log_record} ]; then
touch ${log_record}
fi

## 5.展示配置文件
default='n'

until [ "${default}" = 'y' ] || [ "${default}" = 'Y' ] || [ "${default}" = '' ]; do

echo " # # # # # # # 该项目使用配置如下:"
echo "------1.恢复目标: ${USER}@${IP}:${PORT} 使用身份秘钥: ${identify_file}"
echo "------2.备份文件默认恢复到远端文件夹: ${back_up_file_path}"
echo "------3.最大传输速度为: ${transfer_speed}kb/s"

### 5.1 确认是否使用默认配置
read -p "是否使用当前配置(y默认/n) ## " default
### 5.2 自定义配置
if [ "${default}" = 'y' ] || [ "${default}" = 'Y' ] || [ "${default}" = '' ]; then
echo "${request_file}使用当前配置"
else
read -p "需要修改哪条配置(仅本次生效) ## " config_index
if [ ${config_index} -eq 1 ]; then
echo "修改恢复目标请配置config文件"
read -p "请输入使用身份秘钥路径 > " id_file
if [ "${id_file}" != '' ]; then
identify_file=${id_file}
fi
elif [ ${config_index} -eq 2 ]; then
read -p "请输入远端恢复目标文件夹 > " origin_file
if [ "${origin_file}" != '' ]; then
back_up_file_path=${origin_file}
fi
elif [ ${config_index} -eq 3 ]; then
read -p "请输入最大传输速度 > " trans_speed
if [ "${trans_speed}" != '' ]; then
transfer_speed=${trans_speed}
fi
else
echo '请输入正确的编号'
fi
fi
done
### 5.3展示修改后当前配置
echo "---------------------------------------------------------------------"
echo " 确认该项目使用配置如下:"
echo "1.恢复目标: ${USER}@${IP}:${PORT} 使用身份秘钥: ${identify_file}"
echo "2.备份文件默认恢复到远端文件夹: ${back_up_file_path}"
echo "3.最大传输速度为: ${transfer_speed}kb/s"
echo "---------------------------------------------------------------------"

# 6.判断需要全量恢复还是指定日期恢复
read -p "请选择恢复类型(1.全量(默认)/2.指定日期之前/3.指定日期之后) ## " recover_type

if [ "${recover_type}" = '1' ] || [ "${recover_type}" = '' ]; then
rec_type="all"
elif [ "${recover_type}" = '2' ]; then
read -p "请输入需要恢复多少天前的备份(纯数字天数) ## " pre_date
if [ ${pre_date} -gt 0 ] 2>/dev/null; then
echo "输入${recover_type}"
else
echo '输入格式错误'
exit 000006
fi
rec_type="+${pre_date}"

elif [ "${recover_type}" = '3' ]; then
read -p "请输入需要恢复多少天前的备份(纯数字天数) ## " aft_date
if [ ${aft_date} -gt 0 ] 2>/dev/null; then
echo "输入${recover_type}"
else
echo '输入格式错误'
exit 000007
fi
rec_type="-${aft_date}"
else
echo "输入${recover_type}不符合规则"
exit 000010
fi
echo "恢复类型${rec_type}(参考find -ctime 命令)"

# 7.判断恢复是全部覆盖还是存在的文件不覆盖
echo "---------------------------------------------------------------------"
echo "---1.全部覆盖(默认)"
echo "---2.当目标文件不存在时才执行操作"
echo "---3.不要覆盖任何"
echo "---4.存在时,备份覆盖"
echo "---------------------------------------------------------------------"
read -p "请选择数据覆盖类型 ## " cover_type
if [ "${cover_type}" = '1' ] || [ "${cover_type}" = '' ]; then
cover_type="-f"
elif [ "${cover_type}" = '2' ]; then
cover_type="-f"
elif [ "${cover_type}" = '3' ]; then
cover_type="-n"
elif [ "${cover_type}" = '4' ]; then
cover_type="-b"
else
echo "输入${cover_type}不符合规则"
exit 000011
fi
echo "覆盖类型${cover_type}"

# 8.判断需要使用密码登录传输还是公钥文件传输
read -p "请选择连接类型(1.使用配置密钥(慢速)/2.密码登录(快速)) ## " link_type

if [ "${link_type}" = 1 ] || [ "${link_type}" = '' ]; then
send_type='pubkey'
elif [ "${link_type}" = 2 ]; then
send_type='pass'
else
echo "输入${link_type}不符合规则"
exit 000012
fi
echo "验证连接方式: ${send_type}"

# 9.执行备份文件传输
## 9.1 前置准备工作
### 9.1.1 测试远端命令是否执行成功
execOrigin "ls -ahl" "${log_record}"
if [ $? -ne 0 ]; then
echo "远端命令不可用,无法连接,中断"
exit 000013
fi
### 9.1.2 远端创建备份文件夹(大容量)
echo "创建远端缓存文件夹: ${mk_tmp_dir}"
origin_mk_tmp_dir="mkdir ${origin_tmp_dir}"
execOrigin "${origin_mk_tmp_dir}" "${log_record}"

exit

## 9.2 获取需要传输的文件(打包/清单)
project_dir=${back_project}

### 9.2.1 创建缓存文件夹
temp_tar="${request_file}${current_time}.tar" # 打包备份文件名: 项目名+时间.tar
temp_dir="${project_dir}/../../temp" # 打包备份文件夹
if [ ! -d ${temp_dir} ]; then
mkdir ${temp_dir}
fi
### 9.2.2 打包/列出 需要回复的备份文件
if [ ${rec_type} = "all" ]; then
find ${project_dir} -type f > ${file_choice}
# 空值判定
if [ ! -s ${file_choice} ]; then
echo '指定日期没有备份文件,退出'
rm -f ${file_choice}
exit 000014
fi
if [ "${send_type}" = 'pass' ]; then
echo "---使用文件:"
echo `cat ${file_choice}`
echo "---创建压缩包: ${temp_dir}/${temp_tar}"
find ${project_dir} -type f | xargs tar -cvf "${temp_dir}/${temp_tar}"
fi
else
find ${project_dir} -type f -mtime ${rec_type} > ${file_choice}
# 空值判定
if [ ! -s ${file_choice} ]; then
echo '指定日期没有备份文件,退出'
rm -f ${file_choice}
exit 000015
fi
if [ "${send_type}" = 'pass' ]; then
echo "---使用文件:"
echo `cat ${file_choice}`
echo "---创建压缩包: ${temp_dir}/${temp_tar}"
find ${project_dir} -type f -mtime ${rec_type} | xargs tar -cvf "${temp_dir}/${temp_tar}"
fi
fi

## 9.3 传输文件到远端
if [ "${send_type}" = 'pass' ]; then
echo "使用密码方式传输文件: ${temp_dir}/${temp_tar}"
scpRecoverByPass "${temp_dir}/${temp_tar}" 3
# 解压远端总包
cmd_eject="cd ${origin_tmp_dir} && tar -xvpf ${temp_tar}) --force-local && rm -f ${temp_tar}"
execOrigin "${cmd_eject}" "${log_record}"
rm -f "${temp_dir}/${temp_tar}"
else
echo "使用秘钥文件${identify_file}传输文件"
scpRecover ${file_choice} 3
fi
## 9.3.1 解压远端文件
### 9.3.1.1 拼接解压命令
cmd_line="cd ${origin_tmp_dir}"
cat ${file_choice} | while read eachLine; do
echo "待解压:${eachLine}"
# 文件名
file_name=`basename ${eachLine}`
echo "文件名: ${file_name}"
# 文件后缀
file_ext=`expr "${file_name}" : '.*\(\..*\)'`
echo "后缀名: ${file_ext}"
if [ "${file_ext}" = '.tar' ]; then
cmd_tar="tar -xvpf ${file_name} --force-local"
elif [ "${file_ext}" = '.gz' ]; then
cmd_tar="tar -xzvpf ${file_name} --force-local"
elif [ "${file_ext}" = '.bz2' ]; then
cmd_tar="tar -xjvpf ${file_name} --force-local"
elif [ "${file_ext}" = '.Z' ]; then
cmd_tar="tar –xZvpf ${file_name} --force-local"
elif [ "${file_ext}" = '.rar' ]; then
cmd_tar="unrar e ${file_name}"
elif [ "${file_ext}" = '.zip' ]; then
cmd_tar="unzip ${file_name}"
else
echo "未知格式${file_ext}"
continue
fi
echo "日志文件: ${log_record}"
echo "执行命令: ${cmd_tar}"
echo "删除命令: rm -f ${file_name}"
# 进入文件夹,解压文件
cmd_line="${cmd_line} && ${cmd_tar}"
# 删除原包
cmd_line="${cmd_line} && rm -f ${file_name}"
echo ${cmd_line} > ${cmd_result}
done
### 9.3.1.2 执行远端解压命令
cmd_total=`cat ${cmd_result}`
### 9.3.1.3 删除缓存文件
rm -f ${file_choice}
rm -f ${cmd_result}
echo "解压命令为: ${cmd_total}"
execOrigin "${cmd_total}" "${log_record}"

## 9.4 从缓存文件夹移动备份文件夹到源目录

echo "----------------正在覆盖文件到${back_up_file_path}----------------------"
# 创建文件夹
cmd_mkf="mkdir ${back_up_file_path}"
execOrigin "${cmd_mkf}" "${log_record}" 2>/dev/null # 创建新文件夹
# 移动文件到目标文件夹
config_lever=${package_level}
# cmd_move="mv ${cover_type} ${origin_tmp_dir}${config_lever}/* ${back_up_file_path}/"
cmd_move="cp -rf ${origin_tmp_dir}/${config_lever}/* ${back_up_file_path}/"
execOrigin "${cmd_move}" "${log_record}"
#cmd_remove="rm -rf ${origin_tmp_dir}"
#execOrigin "${cmd_remove}" "${log_record}"
echo "------------------- 文件恢复完毕,ENJOY ---------------------------------"

config配置文件

1
backfile='/var/www/File'

config/[项目名] 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
# 远端配置
NAME='测试服数据恢复' # 项目名称
USER='ubuntu' # 用户名
IP='xxx.xxx.xxx.xxx' # IP地址
PORT='22' # 端口
# 功能配置
identify_file='./env/id_rsa_114' # 登录公钥文件
transfer_speed='2000000' # 最大传输速度 kb/s
# 项目文件配置
origin_tmp_dir='/var/www/tmp'
back_up_file_path='/var/www/[项目名]/uploadfiles1' # 远端备份路径
package_level='[压缩包内文件名,可多层]' # 包层级