Update sync_data.sh
Browse files- sync_data.sh +137 -77
sync_data.sh
CHANGED
|
@@ -1,109 +1,169 @@
|
|
| 1 |
-
#!/bin/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
# 检查环境变量
|
| 4 |
-
if [ -z "$WEBDAV_URL" ] || [ -z "$WEBDAV_USERNAME" ] || [ -z "$WEBDAV_PASSWORD" ]; then
|
| 5 |
-
echo "
|
| 6 |
exit 0
|
| 7 |
fi
|
| 8 |
|
| 9 |
-
# 设置备份路径
|
| 10 |
-
WEBDAV_BACKUP_PATH=${WEBDAV_BACKUP_PATH:-"
|
| 11 |
-
FULL_WEBDAV_URL="${WEBDAV_URL}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
restore_backup() {
|
| 15 |
-
echo "
|
| 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 |
echo "Downloading latest backup from WebDAV..."
|
| 52 |
restore_backup
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# 同步函数
|
| 55 |
sync_data() {
|
| 56 |
while true; do
|
| 57 |
echo "Starting sync process at $(date)"
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
timestamp=$(date +%Y%m%d_%H%M%S)
|
| 61 |
-
backup_file="
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
| 70 |
if [ $? -eq 0 ]; then
|
| 71 |
echo "Successfully uploaded ${backup_file} to WebDAV"
|
| 72 |
else
|
| 73 |
echo "Failed to upload ${backup_file} to WebDAV"
|
| 74 |
fi
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
else
|
| 77 |
echo "Failed to create backup archive."
|
| 78 |
fi
|
| 79 |
-
cd -
|
| 80 |
-
|
| 81 |
-
# 清理旧备份文件 (使用 curl)
|
| 82 |
-
echo "清理旧备份文件..."
|
| 83 |
-
BACKUP_FILES=$(curl -s -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -X PROPFIND -depth 1 "$FULL_WEBDAV_URL/" | grep '<d:href>' | grep 'cloudreve_backup_' | grep '.tar.gz' | sed -e 's/.*<d:href>\([^<]*\)<\/d:href>.*/\1/' | sort)
|
| 84 |
-
BACKUP_ARRAY=($BACKUP_FILES)
|
| 85 |
-
NUM_BACKUPS="${#BACKUP_ARRAY[@]}"
|
| 86 |
-
MAX_BACKUPS=5
|
| 87 |
-
|
| 88 |
-
if [ "$NUM_BACKUPS" -gt "$MAX_BACKUPS" ]; then
|
| 89 |
-
echo "找到 $NUM_BACKUPS 个备份文件,保留最新的 $MAX_BACKUPS 个。"
|
| 90 |
-
for ((i=0; i<$(($NUM_BACKUPS - $MAX_BACKUPS)); i++)); do
|
| 91 |
-
FILE_TO_DELETE="${BACKUP_ARRAY[i]}"
|
| 92 |
-
echo "删除旧备份: $FULL_WEBDAV_URL/$FILE_TO_DELETE"
|
| 93 |
-
curl -X DELETE -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" "$FULL_WEBDAV_URL/$FILE_TO_DELETE"
|
| 94 |
-
if [ $? -eq 0 ]; then
|
| 95 |
-
echo "成功删除 $FILE_TO_DELETE"
|
| 96 |
-
else
|
| 97 |
-
echo "删除 $FILE_TO_DELETE 失败"
|
| 98 |
-
fi
|
| 99 |
-
done
|
| 100 |
-
else
|
| 101 |
-
echo "备份文件数量少于 $MAX_BACKUPS,无需清理。"
|
| 102 |
-
fi
|
| 103 |
|
| 104 |
-
SYNC_INTERVAL=${SYNC_INTERVAL:-
|
| 105 |
echo "Next sync in ${SYNC_INTERVAL} seconds..."
|
| 106 |
-
sleep $SYNC_INTERVAL
|
| 107 |
done
|
| 108 |
}
|
| 109 |
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
# 设置默认的同步间隔为 600 秒 (10 分钟),可以作为环境变量传入
|
| 4 |
+
SYNC_INTERVAL=${SYNC_INTERVAL:-600}
|
| 5 |
+
|
| 6 |
+
# 设置备份保留数量,默认为 5,可以作为环境变量传入
|
| 7 |
+
BACKUP_RETENTION=${BACKUP_RETENTION:-5}
|
| 8 |
|
| 9 |
# 检查环境变量
|
| 10 |
+
if [[ -z "$WEBDAV_URL" ]] || [[ -z "$WEBDAV_USERNAME" ]] || [[ -z "$WEBDAV_PASSWORD" ]]; then
|
| 11 |
+
echo "Warning: Backup functionality will not be enabled - missing WEBDAV_URL, WEBDAV_USERNAME, or WEBDAV_PASSWORD"
|
| 12 |
exit 0
|
| 13 |
fi
|
| 14 |
|
| 15 |
+
# 设置备份路径,如果 WEBDAV_BACKUP_PATH 为空,则直接使用 WEBDAV_URL
|
| 16 |
+
WEBDAV_BACKUP_PATH=${WEBDAV_BACKUP_PATH:-""}
|
| 17 |
+
FULL_WEBDAV_URL="${WEBDAV_URL}"
|
| 18 |
+
if [ -n "$WEBDAV_BACKUP_PATH" ]; then
|
| 19 |
+
FULL_WEBDAV_URL="${WEBDAV_URL}/${WEBDAV_BACKUP_PATH}"
|
| 20 |
+
fi
|
| 21 |
+
|
| 22 |
+
# 设置 Cloudreve 数据目录 (修改为正确的路径)
|
| 23 |
+
CLOUDREVE_DATA_DIR="/opt/cloudreve/data"
|
| 24 |
|
| 25 |
+
# 激活虚拟环境
|
| 26 |
+
if [ -f "$HOME/venv/bin/activate" ]; then
|
| 27 |
+
source "$HOME/venv/bin/activate"
|
| 28 |
+
fi
|
| 29 |
+
|
| 30 |
+
# 下载最新备份并恢复
|
| 31 |
restore_backup() {
|
| 32 |
+
echo "Starting to download the latest backup from WebDAV..."
|
| 33 |
+
WEBDAV_URL="$FULL_WEBDAV_URL" WEBDAV_USERNAME="$WEBDAV_USERNAME" WEBDAV_PASSWORD="$WEBDAV_PASSWORD" CLOUDREVE_DATA_DIR="$CLOUDREVE_DATA_DIR" python3 -c "
|
| 34 |
+
import sys
|
| 35 |
+
import os
|
| 36 |
+
import tarfile
|
| 37 |
+
import requests
|
| 38 |
+
import shutil
|
| 39 |
+
from webdav3.client import Client
|
| 40 |
+
|
| 41 |
+
WEBDAV_URL = os.environ.get('WEBDAV_URL')
|
| 42 |
+
WEBDAV_USERNAME = os.environ.get('WEBDAV_USERNAME')
|
| 43 |
+
WEBDAV_PASSWORD = os.environ.get('WEBDAV_PASSWORD')
|
| 44 |
+
CLOUDREVE_DATA_DIR = os.environ.get('CLOUDREVE_DATA_DIR')
|
| 45 |
+
|
| 46 |
+
options = {
|
| 47 |
+
'webdav_hostname': WEBDAV_URL,
|
| 48 |
+
'webdav_login': WEBDAV_USERNAME,
|
| 49 |
+
'webdav_password': WEBDAV_PASSWORD
|
| 50 |
+
}
|
| 51 |
+
client = Client(options)
|
| 52 |
+
try:
|
| 53 |
+
files = client.list()
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f'Error listing WebDAV directory: {e}')
|
| 56 |
+
sys.exit(1)
|
| 57 |
+
|
| 58 |
+
backups = [file for file in files if file.endswith('.tar.gz') and file.startswith('alist_backup_')]
|
| 59 |
+
if not backups:
|
| 60 |
+
print('No backup files found on WebDAV.')
|
| 61 |
+
sys.exit()
|
| 62 |
+
|
| 63 |
+
latest_backup = sorted(backups)[-1]
|
| 64 |
+
backup_file_path = f'/tmp/{latest_backup}'
|
| 65 |
+
print(f'Latest backup file found: {latest_backup}')
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
client.download_file(latest_backup, backup_file_path)
|
| 69 |
+
print(f'Successfully downloaded backup file to {backup_file_path}')
|
| 70 |
+
except Exception as e:
|
| 71 |
+
print(f'Failed to download backup file: {e}')
|
| 72 |
+
sys.exit(1)
|
| 73 |
+
|
| 74 |
+
if os.path.exists(backup_file_path):
|
| 75 |
+
if os.path.exists(CLOUDREVE_DATA_DIR):
|
| 76 |
+
print(f\"Deleting existing data directory: {CLOUDREVE_DATA_DIR}\")
|
| 77 |
+
shutil.rmtree(CLOUDREVE_DATA_DIR)
|
| 78 |
+
os.makedirs(CLOUDREVE_DATA_DIR, exist_ok=True)
|
| 79 |
+
print(f\"Extracting backup to: {CLOUDREVE_DATA_DIR}\")
|
| 80 |
+
try:
|
| 81 |
+
with tarfile.open(backup_file_path, 'r:gz') as tar:
|
| 82 |
+
tar.extractall(CLOUDREVE_DATA_DIR)
|
| 83 |
+
print(f'Successfully restored backup from {latest_backup}')
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f'Error extracting backup file: {e}')
|
| 86 |
+
sys.exit(1)
|
| 87 |
+
else:
|
| 88 |
+
print('Downloaded backup file does not exist.')
|
| 89 |
+
"
|
| 90 |
}
|
| 91 |
|
| 92 |
# 首次启动时下载最新备份
|
| 93 |
echo "Downloading latest backup from WebDAV..."
|
| 94 |
restore_backup
|
| 95 |
|
| 96 |
+
# 等待30秒后启动程序
|
| 97 |
+
sleep 30
|
| 98 |
+
|
| 99 |
+
# 启动程序 (根据您的日志,您运行的是 Cloudreve)
|
| 100 |
+
./cloudreve -c /opt/cloudreve/conf/cloudreve.ini &
|
| 101 |
+
|
| 102 |
# 同步函数
|
| 103 |
sync_data() {
|
| 104 |
while true; do
|
| 105 |
echo "Starting sync process at $(date)"
|
| 106 |
|
| 107 |
+
if [ ! -d "$CLOUDREVE_DATA_DIR" ]; then
|
| 108 |
+
mkdir -p "$CLOUDREVE_DATA_DIR"
|
| 109 |
+
echo "Data directory created."
|
| 110 |
+
fi
|
| 111 |
+
|
| 112 |
timestamp=$(date +%Y%m%d_%H%M%S)
|
| 113 |
+
backup_file="alist_backup_${timestamp}.tar.gz"
|
| 114 |
+
backup_file_path="/tmp/${backup_file}"
|
| 115 |
+
|
| 116 |
+
echo "Creating backup archive: ${backup_file_path}"
|
| 117 |
+
# 压缩数据目录,排除可能存在的临时文件
|
| 118 |
+
tar -czf "${backup_file_path}" --exclude='*.tmp' -C "$CLOUDREVE_DATA_DIR" .
|
| 119 |
+
|
| 120 |
+
if [ -f "${backup_file_path}" ]; then
|
| 121 |
+
echo "Uploading backup to WebDAV: ${FULL_WEBDAV_URL}/${backup_file}"
|
| 122 |
+
curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "${backup_file_path}" "${FULL_WEBDAV_URL}/${backup_file}"
|
| 123 |
if [ $? -eq 0 ]; then
|
| 124 |
echo "Successfully uploaded ${backup_file} to WebDAV"
|
| 125 |
else
|
| 126 |
echo "Failed to upload ${backup_file} to WebDAV"
|
| 127 |
fi
|
| 128 |
+
|
| 129 |
+
# 清理旧备份文件
|
| 130 |
+
WEBDAV_URL="$FULL_WEBDAV_URL" WEBDAV_USERNAME="$WEBDAV_USERNAME" WEBDAV_PASSWORD="$WEBDAV_PASSWORD" BACKUP_RETENTION="$BACKUP_RETENTION" python3 -c "
|
| 131 |
+
import os
|
| 132 |
+
from webdav3.client import Client
|
| 133 |
+
|
| 134 |
+
WEBDAV_URL = os.environ.get('WEBDAV_URL')
|
| 135 |
+
WEBDAV_USERNAME = os.environ.get('WEBDAV_USERNAME')
|
| 136 |
+
WEBDAV_PASSWORD = os.environ.get('WEBDAV_PASSWORD')
|
| 137 |
+
BACKUP_RETENTION = int(os.environ.get('BACKUP_RETENTION', '5'))
|
| 138 |
+
|
| 139 |
+
options = {
|
| 140 |
+
'webdav_hostname': WEBDAV_URL,
|
| 141 |
+
'webdav_login': WEBDAV_USERNAME,
|
| 142 |
+
'webdav_password': WEBDAV_PASSWORD
|
| 143 |
+
}
|
| 144 |
+
client = Client(options)
|
| 145 |
+
try:
|
| 146 |
+
backups = sorted([file for file in client.list() if file.endswith('.tar.gz') and file.startswith('alist_backup_')])
|
| 147 |
+
if len(backups) > BACKUP_RETENTION:
|
| 148 |
+
num_to_delete = len(backups) - BACKUP_RETENTION
|
| 149 |
+
for file in backups[:num_to_delete]:
|
| 150 |
+
try:
|
| 151 |
+
client.clean(file)
|
| 152 |
+
print(f'Successfully deleted old backup: {file}')
|
| 153 |
+
except Exception as e:
|
| 154 |
+
print(f'Error deleting {file}: {e}')
|
| 155 |
+
except Exception as e:
|
| 156 |
+
print(f'Error managing backups: {e}')
|
| 157 |
+
" 2>&1
|
| 158 |
+
|
| 159 |
+
rm -f "${backup_file_path}"
|
| 160 |
else
|
| 161 |
echo "Failed to create backup archive."
|
| 162 |
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
SYNC_INTERVAL=${SYNC_INTERVAL:-600}
|
| 165 |
echo "Next sync in ${SYNC_INTERVAL} seconds..."
|
| 166 |
+
sleep "$SYNC_INTERVAL"
|
| 167 |
done
|
| 168 |
}
|
| 169 |
|