forked from 07031218/normal-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emby-bs.sh
336 lines (327 loc) · 11.9 KB
/
emby-bs.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
#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
red='\033[0;31m'
green='\033[0;32m'
White='\033[37m'
blue='\033[36m'
yellow='\033[0;33m'
plain='\033[0m'
echoType='echo -e'
DATE=`date +%Y%m%d`
sys_dir=/opt/emby-server/
config_dir=/var/lib/
backto_dir="" # 填写gd网盘挂载路径下面其中一个文件夹作为数据备份存放目录
xuegua_dir="" # 填写自定义削刮文件的存放路径,如未曾自定义过削刮文件存放目录,则留空即可
baksys="" # 是否备份Emby主程序的开关,如果要备份Emby的主程序,则填写Y或者y,否则留空即可
DEL_DAY=7 # 备份文件保存的天数,默认7天,支持自定义修改
echoContent() {
case $1 in
# 红色
"red")
# shellcheck disable=SC2154
${echoType} "\033[31m$2\033[0m"
;;
# 绿色
"green")
${echoType} "\033[32m$2\033[0m"
;;
# 黄色
"yellow")
${echoType} "\033[33m$2\033[0m"
;;
# 蓝色
"blue")
${echoType} "\033[34m$2\033[0m"
;;
# 紫色
"purple")
${echoType} "\033[35m$2\033[0m"
;;
# 天蓝色
"skyBlue")
${echoType} "\033[36m$2\033[0m"
;;
# 白色
"white")
${echoType} "\033[37m$2\033[0m"
;;
esac
}
targz(){
if [[ `which pv` == "" ]]; then
apt install pv -y || yum install pv -y
fi
if [[ $1 = '' ]]; then
echo "参数缺失,用法 'targz 压缩名 文件名/目录名'"
exit 1
fi
tar -cf - $3 $2 | pv -s $(du -sk $2 | awk '{print $1}') | gzip > $1
}
untar(){
if [[ `which pv` == "" ]]; then
apt install pv -y || yum install pv -y
fi
total_size=`du -sk $1 | awk '{print $1}'`
echo
pv -s $((${total_size} * 1020)) $1 | tar zxf - -C $2
}
backup_emby(){
mkdir -p ${backto_dir}/${DATE}
echoContent white "即将开始对Emby Server进行备份,需要一定的时间,请耐心等待······"
sleep 3s
if [[ ${xuegua_dir} == "" ]]; then
xuegua_dir=$config_dir
systemctl stop emby-server
cd $xuegua_dir
echoContent yellow "Emby削刮包和数据库备份中,请耐心等待······"
while true; do
targz ${backto_dir}/${DATE}/Emby削刮包和LibEmby数据库.tar.gz ./emby
if [[ "$?" -eq 0 ]]; then
# clear
echoContent green "Emby削刮包和LibEmby数据库备份完成"
break
else
rm ${backto_dir}/${DATE}/Emby削刮包和LibEmby数据库.tar.gz
echoContent red "Emby削刮包和LibEmby数据库备份失败,开始重试······"
fi
done
if [[ $baksys == "Y" ]]||[[ $baksys == "y" ]]; then
cd $sys_dir
echoContent yellow "Emby-server主程序备份中,请耐心等待······"
while true; do
targz ${backto_dir}/${DATE}/Emby-server主程序.tar.gz ./
if [[ "$?" -eq 0 ]]; then
# clear
echoContent green "Emby-server主程序备份完成"
break
else
echoContent red "Emby-server主程序备份失败,开始重试······"
fi
done
fi
echoContent yellow "恭喜,所有备份均已完成。"
systemctl start emby-server
else
systemctl stop emby-server
cd $xuegua_dir
echoContent yellow "Emby削刮包备份中,请耐心等待······"
while true; do
targz ${backto_dir}/${DATE}/Emby削刮包.tar.gz ./
if [[ "$?" -eq 0 ]]; then
# clear
echoContent green "Emby削刮包备份完成"
break
else
rm ${backto_dir}/${DATE}/Emby削刮包.tar.gz
echoContent red "Emby削刮包备份失败,开始重试······"
fi
done
cd $config_dir
echoContent yellow "LibEmby数据库备份中,请耐心等待······"
# 备份VarLibEmby数据库(排除包含帐户数据相关的文件)
while true; do
targz ${backto_dir}/${DATE}/LibEmby数据库.tar.gz ./emby
if [[ "$?" -eq 0 ]]; then
# clear
echoContent green "LibEmby数据库备份完成"
break
else
echoContent red "LibEmby数据库备份失败,开始重试······"
fi
done
if [[ $baksys == "Y" ]]||[[ $baksys == "y" ]]; then
cd $sys_dir
echoContent yellow "Emby-server主程序备份中,请耐心等待······"
while true; do
targz ${backto_dir}/${DATE}/Emby-server主程序.tar.gz ./
if [[ "$?" -eq 0 ]]; then
# clear
echoContent green "Emby-server主程序备份完成"
break
else
echoContent red "Emby-server主程序备份失败,开始重试······"
fi
done
fi
echoContent yellow "恭喜,所有备份均已完成。"
systemctl start emby-server
fi
LIST=$(ls $backto_dir)
# 获取7天前的时间,用于作比较,早于该时间的文件将删除
SECONDS=$(date -d "$(date +%F) -${DEL_DAY} days" +%s)
for index in ${LIST}
do
# 对目录名进行格式化,取命名末尾的时间,格式如 20200902
timeString=$(echo ${index} | egrep -o "?[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]")
if [ -n "$timeString" ]
then
indexDate=${timeString//./-}
indexSecond=$( date -d ${indexDate} +%s )
# 与当天的时间做对比,把早于7天的备份文件删除
if [ $(( $SECONDS- $indexSecond )) -gt 0 ]
then
rm -rf $backto_dir/$index
fi
fi
done
}
restore_emby(){
echoContent yellow "是否还原Emby-sever主程序?[Y/N]:"
read restore_sys
i=1
list=()
if [[ ! -d ${backto_dir} ]]; then
echoContent red "错误,未检索到备份数据目录。"
exit 1
elif [[ -d ${backto_dir} ]];then
items=$(ls ${backto_dir}/ -l|awk 'NR>1{print $9}')
fi
for item in $items
do
list[i]=${item}
i=$((i+1))
done
while [[ 0 ]]
do
while [[ 0 ]]
do
echo
echoContent skyBlue "当前[yyyy-mm-dd]格式数据备份列表如下:"
# echo
echoContent skyBlue "-------------------------------"
for((j=1;j<=${#list[@]};j++))
do
temp="${j}:${list[j]}"
count=$((`echo "${temp}" | wc -m` -1))
if [ "${count}" -le 6 ];then
temp="${temp}\t\t\t"
elif [ "${count}" -gt 6 ] && [ "$count" -le 14 ];then
temp="${temp}\t\t"
elif [ "${count}" -gt 14 ];then
temp="${temp}"
fi
echoContent skyBlue "${temp}"
echoContent skyBlue "-------------------------------"
done
echo
read -n3 -p "请选择要使用的备份档(输入数字即可):" bak_date_name
if [[ ${bak_date_name} -eq 0 ]]; then
echo
echoContent red "输入不正确,请重新输入。"
elif [ ${bak_date_name} -le ${#list[@]} ] && [ -n ${bak_date_name} ];then
echo
echoContent purple "您选择了:${list[bak_date_name]}"
break
else
echo
echoContent red "输入不正确,请重新输入。"
echo
fi
done
break
done
echoContent red "即将开始还原操作,是否继续执行:[Y/N]"
read yn
if [[ $yn != "Y" ]] && [[ $yn != "y" ]]; then
exit 0
fi
systemctl stop emby-server
if [[ ${xuegua_dir} == "" ]]; then
xuegua_dir=$config_dir
echoContent yellow "Emby削刮包和LibEmby数据库恢复中,请耐心等待······"
untar ${backto_dir}/${list[bak_date_name]}/Emby削刮包和LibEmby数据库.tar.gz $xuegua_dir
if [[ "$?" -eq 0 ]]; then
# clear
echoContent green "Emby削刮包和LibEmby数据库恢复完成"
sleep 5s
else
echoContent red "Emby削刮包和LibEmby数据库失败"
systemctl start emby-server
exit 1
fi
else
echoContent yellow "Emby削刮包恢复中,请耐心等待······"
untar ${backto_dir}/${list[bak_date_name]}/Emby削刮包.tar.gz $xuegua_dir
if [[ "$?" -eq 0 ]]; then
echoContent green "Emby削刮包恢复完成"
sleep 3s
else
echoContent red "Emby削刮包恢复失败"
systemctl start emby-server
exit 1
fi
echoContent yellow "LibEmby数据库恢复中,请耐心等待······"
untar ${backto_dir}/${list[bak_date_name]}/LibEmby数据库.tar.gz $config_dir
if [[ "$?" -eq 0 ]]; then
echoContent green "LibEmby数据库恢复完成"
else
echoContent red "LibEmby数据库恢复失败"
systemctl start emby-server
exit 1
fi
fi
if [[ $restore_sys == "y" ]]||[[ $restore_sys == "Y" ]]; then
echoContent yellow "Emby-server主程序恢复中,请耐心等待······"
untar ${backto_dir}/${list[bak_date_name]}/Emby-server主程序.tar.gz $sys_dir
if [[ "$?" -eq 0 ]]; then
# clear
echoContent green "Emby-server主程序恢复完成"
sleep 5s
else
echoContent red "Emby-server主程序恢复失败"
systemctl start emby-server
exit 1
fi
fi
systemctl start emby-server
echoContent yellow "恭喜,所有恢复均已完成。"
}
copyright(){
clear
echo -e "
${green}###########################################################${plain}
${green}# #${plain}
${green}# Emby Server数据一键备份、还原 #${plain}
${green}# 仅支持宿主机直装的Emby Server #${plain}
${green}# BUG反馈电报群:https://t.me/EmbyDrive #${plain}
${green}# Blog:https://blog.20120714.xyz #${plain}
${green}# Power By:翔翎 #${plain}
${green}# #${plain}
${green}###########################################################${plain}"
}
main(){
copyright
echo -e "
${red}0.${plain} 退出脚本
${green}———————————————————————————————————————————————————————————${plain}
${green}1.${plain} 一键备份
${green}2.${plain} 一键还原
"
echo -e "${yellow}请选择你要使用的功能${plain}"
read -p "请输入数字 :" num
case "$num" in
0)
exit 0
;;
1)
backup_emby
;;
2)
restore_emby
;;
*)
clear
echo -e "${red}出现错误:请输入正确数字 ${plain}"
sleep 2s
copyright
main
;;
esac
}
if [[ $1 == "b" ]]; then
# 可通过命令 bash emby-bs.sh b 实现一键备份
backup_emby
else
main
fi