2024-07-25 21:59:46 +08:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
2024-09-12 11:16:10 +08:00
|
|
|
|
declare path="$1"
|
|
|
|
|
declare cron="$2"
|
2024-07-25 21:59:46 +08:00
|
|
|
|
declare web_path
|
|
|
|
|
declare local_pick
|
|
|
|
|
declare baidu_pick
|
|
|
|
|
declare aliyun_pick
|
|
|
|
|
declare cron
|
2024-09-21 16:00:55 +08:00
|
|
|
|
declare ignore=1
|
|
|
|
|
|
2024-07-25 21:59:46 +08:00
|
|
|
|
|
2024-07-26 19:16:29 +08:00
|
|
|
|
if [[ -f "${path}/backup.sh" ]];then
|
2024-07-25 21:59:46 +08:00
|
|
|
|
echo "该路径文件已经存在"
|
|
|
|
|
fi
|
|
|
|
|
|
2024-09-21 16:36:07 +08:00
|
|
|
|
read -p "请输入数据目录,默认 /var/www :" web_path
|
|
|
|
|
if [[ -z ${web_path} ]];then
|
|
|
|
|
web_path='/var/www'
|
|
|
|
|
fi
|
2024-07-25 21:59:46 +08:00
|
|
|
|
|
2024-09-21 16:36:07 +08:00
|
|
|
|
for item in "$web_path"/* ; do
|
2024-09-21 16:52:53 +08:00
|
|
|
|
[[ $ignore -eq 1 ]] && echo "当前脚本会备份的目录如下" && ignore=
|
2024-09-21 16:36:07 +08:00
|
|
|
|
declare item_name=$(basename "$item")
|
2024-09-21 16:40:27 +08:00
|
|
|
|
echo "${item_name}"
|
2024-09-21 16:00:55 +08:00
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
echo "请输入需要屏蔽的目录用逗号隔开"
|
2024-09-21 16:40:27 +08:00
|
|
|
|
read -p "请输入:" ignore
|
2024-09-21 16:00:55 +08:00
|
|
|
|
|
2024-09-21 16:36:07 +08:00
|
|
|
|
|
2024-07-25 21:59:46 +08:00
|
|
|
|
read -p "是否备份到本地,默认 关闭 ,输入 y 开启:" local_pick
|
|
|
|
|
read -p "是否备份到百度网盘,默认 开启 ,输入 n 关闭:" baidu_pick
|
2024-07-27 15:12:30 +08:00
|
|
|
|
read -p "是否备份到阿里云盘 默认 开启 , 输入 n 关闭:" aliyun_pick
|
2024-07-25 21:59:46 +08:00
|
|
|
|
|
|
|
|
|
if [[ ! $local_pick =~ [Yy] && $baidu_pick =~ [Nn] && $aliyun_pick =~ [Nn] ]];then
|
2024-09-21 16:00:55 +08:00
|
|
|
|
echo "没有选择备份的选项"
|
2024-07-25 21:59:46 +08:00
|
|
|
|
exit
|
|
|
|
|
fi
|
|
|
|
|
|
2024-09-21 16:52:53 +08:00
|
|
|
|
if [[ $local_pick =~ [Yy] ]];then
|
2024-07-25 21:59:46 +08:00
|
|
|
|
declare loacl_path
|
|
|
|
|
read -p "请输入本地备份路径,默认 /var/webbackup :" loacl_path
|
|
|
|
|
if [[ -z $loacl_path ]];then
|
|
|
|
|
loacl_path='/var/webbackup'
|
|
|
|
|
fi
|
|
|
|
|
if [[ -d $loacl_path ]];then
|
|
|
|
|
mkdir -p "$loacl_path"
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ ! $baidu_pick =~ [Nn] ]];then
|
2024-09-12 13:20:44 +08:00
|
|
|
|
if [[ -f "/usr/bin/apt-get" ]];then
|
|
|
|
|
sudo apt-get install python3-venv -y
|
|
|
|
|
elif [[ -f "/usr/bin/apt" ]];then
|
|
|
|
|
sudo apt-get install python3-venv -y
|
|
|
|
|
else
|
|
|
|
|
echo "无法自动安装 python3-venv 请手动安装"
|
|
|
|
|
exit
|
|
|
|
|
fi
|
2024-07-25 21:59:46 +08:00
|
|
|
|
python3 -m venv "${path}/venv"
|
|
|
|
|
source "${path}/venv/bin/activate"
|
|
|
|
|
pip install bypy
|
|
|
|
|
pip install requests
|
|
|
|
|
echo "1.将提示中的链接粘贴到浏览器中登录"
|
|
|
|
|
echo "2.输入账号密码登录后授权,获取授权码"
|
|
|
|
|
echo "3.将授权码粘贴回终端并按回车"
|
|
|
|
|
bypy info
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if [[ ! $aliyun_pick =~ [Nn] ]];then
|
2024-07-26 19:42:07 +08:00
|
|
|
|
if [[ ! -d "${path}/aliyunpan" ]];then
|
2024-07-26 19:38:03 +08:00
|
|
|
|
wget -P "${path}" https://github.com/tickstep/aliyunpan/releases/download/v0.3.2/aliyunpan-v0.3.2-linux-amd64.zip -O "${path}/aliyunpan.zip"
|
|
|
|
|
unzip "${path}/aliyunpan.zip" -d "${path}"
|
|
|
|
|
rm "${path}/aliyunpan.zip"
|
2024-07-27 15:12:30 +08:00
|
|
|
|
mv "${path}/$(ls "${path}" | grep "aliyunpan")" "${path}/aliyunpan"
|
2024-09-10 17:18:11 +08:00
|
|
|
|
fi
|
|
|
|
|
if [[ "$( ${path}/aliyunpan/aliyunpan who)" == "未登录账号" ]];then
|
2024-07-26 19:38:03 +08:00
|
|
|
|
${path}/aliyunpan/aliyunpan login
|
2024-07-26 19:23:25 +08:00
|
|
|
|
fi
|
2024-07-25 21:59:46 +08:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
cat > "${path}/backup.sh" << EOF
|
|
|
|
|
#!/bin/bash
|
2024-08-12 09:52:02 +08:00
|
|
|
|
declare date_time=\$(date +"%Y_%m_%d") # 日期格式
|
2024-08-05 21:39:58 +08:00
|
|
|
|
declare year=\$(date +"%Y") #年份
|
2024-09-21 16:00:55 +08:00
|
|
|
|
declare ignore=$ignore
|
2024-09-21 16:52:53 +08:00
|
|
|
|
source \"${path}/venv/bin/activate\"
|
2024-07-25 21:59:46 +08:00
|
|
|
|
|
|
|
|
|
for item in "$web_path"/*; do
|
2024-07-26 21:06:33 +08:00
|
|
|
|
declare item_name=\$(basename "\$item")
|
2024-09-21 16:00:55 +08:00
|
|
|
|
if [[ "\$ignore" =~ "\$item" ]];then
|
|
|
|
|
continue
|
|
|
|
|
fi
|
2024-07-25 21:59:46 +08:00
|
|
|
|
cd "\$item" || exit
|
2024-08-05 22:13:25 +08:00
|
|
|
|
tar -czf "\${item_name}_\${date_time}.tar.gz" \$(ls)
|
2024-07-25 21:59:46 +08:00
|
|
|
|
bypy upload "\${item_name}_\${date_time}.tar.gz" "/\${item_name}/"
|
2024-07-26 19:26:45 +08:00
|
|
|
|
${path}/aliyunpan/aliyunpan upload "\${item_name}_\${date_time}.tar.gz" "/网站/\${item_name}/\${year}/"
|
2024-09-21 17:04:35 +08:00
|
|
|
|
mkdir -p "\${item_name}" && cp "\${item_name}_\${date_time}.tar.gz" "${loacl_path}/\${year}/\${item_name}"
|
2024-07-25 21:59:46 +08:00
|
|
|
|
rm "\${item_name}_\${date_time}.tar.gz"
|
|
|
|
|
done
|
|
|
|
|
EOF
|
|
|
|
|
chmod +x "$path/backup.sh"
|
|
|
|
|
|
|
|
|
|
declare cron_job="${cron} ${path}/backup.sh"
|
2024-07-26 18:39:43 +08:00
|
|
|
|
(crontab -l 2>/dev/null | grep -v "backup.sh") | sudo crontab -
|
2024-07-25 21:59:46 +08:00
|
|
|
|
|
|
|
|
|
sudo systemctl restart cron 2>> /dev/null || echo "自动任务重启失败"
|
|
|
|
|
|
2024-09-21 16:52:53 +08:00
|
|
|
|
if [[ $local_pick == [Yy] ]];then
|
2024-07-25 21:59:46 +08:00
|
|
|
|
echo "本地备份路径:${web_path}/年份/目录名称"
|
|
|
|
|
else
|
2024-09-21 17:02:10 +08:00
|
|
|
|
sed -i '/mkdir.*/d' "${path}/backup.sh"
|
2024-07-25 21:59:46 +08:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ $baidu_pick == [Nn] ]];then
|
|
|
|
|
sed -i '/bypy.*/d' "${path}/backup.sh"
|
|
|
|
|
sed -i '/source.*/d' "${path}/backup.sh"
|
|
|
|
|
else
|
|
|
|
|
echo "百度网盘备份路径:我的应用数据/bypy/目录名称"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ $baidu_pick == [Nn] ]];then
|
|
|
|
|
sed -i '/.*aliyunpan.*/d' "/var/script/backup.sh"
|
|
|
|
|
else
|
|
|
|
|
echo "阿里云盘备份路径:网盘/目录名称/日期"
|
|
|
|
|
fi
|