Spaces:
Paused
Paused
Update sync_data.sh
Browse files- sync_data.sh +97 -117
sync_data.sh
CHANGED
|
@@ -1,151 +1,131 @@
|
|
| 1 |
#!/bin/bash
|
| 2 |
-
# sync_data.sh
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
WEBDAV_USERNAME=${WEBDAV_USERNAME}
|
| 10 |
-
WEBDAV_PASSWORD=${WEBDAV_PASSWORD}
|
| 11 |
-
WEBDAV_BACKUP_PATH=${WEBDAV_BACKUP_PATH:-alist_backups}
|
| 12 |
-
SYNC_INTERVAL=${SYNC_INTERVAL:-3600}
|
| 13 |
-
|
| 14 |
-
# 创建日志文件
|
| 15 |
-
exec > >(tee -a "$LOG_FILE") 2>&1
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# 激活虚拟环境
|
| 23 |
-
source /
|
| 24 |
-
|
| 25 |
-
# 函数:恢复备份
|
| 26 |
-
restore_backup() {
|
| 27 |
-
log "开始恢复最新备份..."
|
| 28 |
-
|
| 29 |
-
# 检查 WebDAV 配置
|
| 30 |
-
if [ -z "$WEBDAV_URL" ] || [ -z "$WEBDAV_USERNAME" ] || [ -z "$WEBDAV_PASSWORD" ]; then
|
| 31 |
-
log "WebDAV 配置缺失,跳过恢复"
|
| 32 |
-
return
|
| 33 |
-
fi
|
| 34 |
-
|
| 35 |
-
# 查找最新备份文件
|
| 36 |
-
LATEST_BACKUP=$(python3 -c "
|
| 37 |
-
import os
|
| 38 |
-
from webdav3.client import Client
|
| 39 |
-
options = {
|
| 40 |
-
'webdav_hostname': '$WEBDAV_URL',
|
| 41 |
-
'webdav_login': '$WEBDAV_USERNAME',
|
| 42 |
-
'webdav_password': '$WEBDAV_PASSWORD',
|
| 43 |
-
'webdav_root': '$WEBDAV_BACKUP_PATH'
|
| 44 |
-
}
|
| 45 |
-
client = Client(options)
|
| 46 |
-
backups = [f for f in client.list() if f.startswith('alist_backup_') and f.endswith('.tar.gz')]
|
| 47 |
-
if backups:
|
| 48 |
-
print(sorted(backups)[-1])
|
| 49 |
-
else:
|
| 50 |
-
print('')
|
| 51 |
-
")
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
fi
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 60 |
python3 -c "
|
|
|
|
|
|
|
|
|
|
| 61 |
import requests
|
|
|
|
| 62 |
from webdav3.client import Client
|
| 63 |
options = {
|
| 64 |
-
'webdav_hostname': '$
|
| 65 |
'webdav_login': '$WEBDAV_USERNAME',
|
| 66 |
-
'webdav_password': '$WEBDAV_PASSWORD'
|
| 67 |
-
'webdav_root': '$WEBDAV_BACKUP_PATH'
|
| 68 |
}
|
| 69 |
client = Client(options)
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
"
|
| 76 |
-
|
| 77 |
-
# 恢复备份
|
| 78 |
-
if [ -f "/tmp/latest_backup.tar.gz" ]; then
|
| 79 |
-
log "解压备份文件到 $DATA_DIR"
|
| 80 |
-
mkdir -p "$DATA_DIR"
|
| 81 |
-
tar -xzf /tmp/latest_backup.tar.gz -C "$DATA_DIR" --strip-components=1
|
| 82 |
-
chown -R user:user "$DATA_DIR"
|
| 83 |
-
log "备份恢复完成"
|
| 84 |
-
else
|
| 85 |
-
log "备份文件不存在,跳过恢复"
|
| 86 |
-
fi
|
| 87 |
}
|
| 88 |
|
| 89 |
-
#
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
while true; do
|
| 92 |
-
|
| 93 |
-
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
|
| 94 |
-
BACKUP_FILE="alist_backup_${TIMESTAMP}.tar.gz"
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
}
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
client.upload_stream(f, '$BACKUP_FILE')
|
| 112 |
-
print('备份上传完成')
|
| 113 |
-
"
|
| 114 |
-
log "清理本地备份文件"
|
| 115 |
-
rm -f "/tmp/$BACKUP_FILE"
|
| 116 |
else
|
| 117 |
-
|
| 118 |
fi
|
| 119 |
|
| 120 |
-
# 清理旧备份
|
| 121 |
-
log "清理旧备份(保留最近 5 个)"
|
| 122 |
python3 -c "
|
|
|
|
| 123 |
from webdav3.client import Client
|
| 124 |
options = {
|
| 125 |
-
'webdav_hostname': '$
|
| 126 |
'webdav_login': '$WEBDAV_USERNAME',
|
| 127 |
-
'webdav_password': '$WEBDAV_PASSWORD'
|
| 128 |
-
'webdav_root': '$WEBDAV_BACKUP_PATH'
|
| 129 |
}
|
| 130 |
client = Client(options)
|
| 131 |
-
backups =
|
|
|
|
| 132 |
if len(backups) > 5:
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
| 136 |
"
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
sleep $SYNC_INTERVAL
|
| 139 |
done
|
| 140 |
}
|
| 141 |
|
| 142 |
-
#
|
| 143 |
-
|
| 144 |
-
restore)
|
| 145 |
-
restore_backup
|
| 146 |
-
;;
|
| 147 |
-
*)
|
| 148 |
-
restore_backup &
|
| 149 |
-
sync_backup &
|
| 150 |
-
;;
|
| 151 |
-
esac
|
|
|
|
| 1 |
#!/bin/bash
|
|
|
|
| 2 |
|
| 3 |
+
# 检查环境变量
|
| 4 |
+
if [ -z "$WEBDAV_URL" ] || [ -z "$WEBDAV_USERNAME" ] || [ -z "$WEBDAV_PASSWORD" ]; then
|
| 5 |
+
echo "Starting without backup functionality - missing WEBDAV_URL, WEBDAV_USERNAME, or WEBDAV_PASSWORD"
|
| 6 |
+
exit 0
|
| 7 |
+
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# 设置备份路径
|
| 10 |
+
WEBDAV_BACKUP_PATH=${WEBDAV_BACKUP_PATH:-""}
|
| 11 |
+
FULL_WEBDAV_URL="${WEBDAV_URL}"
|
| 12 |
+
if [ -n "$WEBDAV_BACKUP_PATH" ]; then
|
| 13 |
+
FULL_WEBDAV_URL="${WEBDAV_URL}/${WEBDAV_BACKUP_PATH}"
|
| 14 |
+
fi
|
| 15 |
|
| 16 |
# 激活虚拟环境
|
| 17 |
+
source $VIRTUAL_ENV/bin/activate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# 定义数据目录和配置目录
|
| 20 |
+
DATA_DIR=${DATA_DIR:-"/app/data"}
|
| 21 |
+
CONFIG_DIR=${CONFIG_DIR:-"/app/config"}
|
|
|
|
| 22 |
|
| 23 |
+
# 下载最新备份并恢复
|
| 24 |
+
restore_backup() {
|
| 25 |
+
echo "开始从 WebDAV 下载最新备份..."
|
| 26 |
python3 -c "
|
| 27 |
+
import sys
|
| 28 |
+
import os
|
| 29 |
+
import tarfile
|
| 30 |
import requests
|
| 31 |
+
import shutil
|
| 32 |
from webdav3.client import Client
|
| 33 |
options = {
|
| 34 |
+
'webdav_hostname': '$FULL_WEBDAV_URL',
|
| 35 |
'webdav_login': '$WEBDAV_USERNAME',
|
| 36 |
+
'webdav_password': '$WEBDAV_PASSWORD'
|
|
|
|
| 37 |
}
|
| 38 |
client = Client(options)
|
| 39 |
+
backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('alist_backup_')]
|
| 40 |
+
if not backups:
|
| 41 |
+
print('没有找到备份文件')
|
| 42 |
+
sys.exit()
|
| 43 |
+
latest_backup = sorted(backups)[-1]
|
| 44 |
+
print(f'最新备份文件:{latest_backup}')
|
| 45 |
+
with requests.get(f'$FULL_WEBDAV_URL/{latest_backup}', auth=('$WEBDAV_USERNAME', '$WEBDAV_PASSWORD'), stream=True) as r:
|
| 46 |
+
if r.status_code == 200:
|
| 47 |
+
with open(f'/tmp/{latest_backup}', 'wb') as f:
|
| 48 |
+
for chunk in r.iter_content(chunk_size=8192):
|
| 49 |
+
f.write(chunk)
|
| 50 |
+
print(f'成功下载备份文件到 /tmp/{latest_backup}')
|
| 51 |
+
if os.path.exists(f'/tmp/{latest_backup}'):
|
| 52 |
+
# 如果目录已存在,先删除它
|
| 53 |
+
if os.path.exists('$DATA_DIR'):
|
| 54 |
+
shutil.rmtree('$DATA_DIR')
|
| 55 |
+
if os.path.exists('$CONFIG_DIR'):
|
| 56 |
+
shutil.rmtree('$CONFIG_DIR')
|
| 57 |
+
|
| 58 |
+
os.makedirs('$DATA_DIR', exist_ok=True)
|
| 59 |
+
os.makedirs('$CONFIG_DIR', exist_ok=True)
|
| 60 |
+
|
| 61 |
+
# 解压备份文件
|
| 62 |
+
with tarfile.open(f'/tmp/{latest_backup}', 'r:gz') as tar:
|
| 63 |
+
tar.extractall('/')
|
| 64 |
+
|
| 65 |
+
print(f'成功从 {latest_backup} 恢复备份')
|
| 66 |
+
else:
|
| 67 |
+
print('下载的备份文件不存在')
|
| 68 |
+
else:
|
| 69 |
+
print(f'下载备份失败:{r.status_code}')
|
| 70 |
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
}
|
| 72 |
|
| 73 |
+
# 首次启动时下载最新备份
|
| 74 |
+
echo "Downloading latest backup from WebDAV..."
|
| 75 |
+
restore_backup
|
| 76 |
+
|
| 77 |
+
# 同步函数
|
| 78 |
+
sync_data() {
|
| 79 |
while true; do
|
| 80 |
+
echo "Starting sync process at $(date)"
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
if [ ! -d $DATA_DIR ] || [ ! -d $CONFIG_DIR ]; then
|
| 83 |
+
mkdir -p $DATA_DIR $CONFIG_DIR
|
| 84 |
+
echo "Data/Config directory created."
|
| 85 |
+
fi
|
| 86 |
+
|
| 87 |
+
timestamp=$(date +%Y%m%d_%H%M%S)
|
| 88 |
+
backup_file="alist_backup_${timestamp}.tar.gz"
|
| 89 |
+
|
| 90 |
+
# 压缩数据目录和配置目录
|
| 91 |
+
tar -czf "/tmp/${backup_file}" -C / app/data app/config
|
| 92 |
+
|
| 93 |
+
# 上传新备份到WebDAV
|
| 94 |
+
curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "/tmp/${backup_file}" "$FULL_WEBDAV_URL/${backup_file}"
|
| 95 |
+
if [ $? -eq 0 ]; then
|
| 96 |
+
echo "Successfully uploaded ${backup_file} to WebDAV"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
else
|
| 98 |
+
echo "Failed to upload ${backup_file} to WebDAV"
|
| 99 |
fi
|
| 100 |
|
| 101 |
+
# 清理旧备份文件
|
|
|
|
| 102 |
python3 -c "
|
| 103 |
+
import sys
|
| 104 |
from webdav3.client import Client
|
| 105 |
options = {
|
| 106 |
+
'webdav_hostname': '$FULL_WEBDAV_URL',
|
| 107 |
'webdav_login': '$WEBDAV_USERNAME',
|
| 108 |
+
'webdav_password': '$WEBDAV_PASSWORD'
|
|
|
|
| 109 |
}
|
| 110 |
client = Client(options)
|
| 111 |
+
backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('alist_backup_')]
|
| 112 |
+
backups.sort()
|
| 113 |
if len(backups) > 5:
|
| 114 |
+
to_delete = len(backups) - 5
|
| 115 |
+
for file in backups[:to_delete]:
|
| 116 |
+
client.clean(file)
|
| 117 |
+
print(f'Successfully deleted {file}.')
|
| 118 |
+
else:
|
| 119 |
+
print('Only {} backups found, no need to clean.'.format(len(backups)))
|
| 120 |
"
|
| 121 |
+
|
| 122 |
+
rm -f "/tmp/${backup_file}"
|
| 123 |
+
|
| 124 |
+
SYNC_INTERVAL=${SYNC_INTERVAL:-600}
|
| 125 |
+
echo "Next sync in ${SYNC_INTERVAL} seconds..."
|
| 126 |
sleep $SYNC_INTERVAL
|
| 127 |
done
|
| 128 |
}
|
| 129 |
|
| 130 |
+
# 启动同步进程
|
| 131 |
+
sync_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|