|
#!/bin/sh |
|
|
|
|
|
if [ -z "$WEBDAV_URL" ] || [ -z "$WEBDAV_USERNAME" ] || [ -z "$WEBDAV_PASSWORD" ]; then |
|
echo "Starting without backup functionality - missing WEBDAV_URL, WEBDAV_USERNAME, or WEBDAV_PASSWORD" |
|
exec $APP_DIR/$APP_NAME$APP_SUFFIX |
|
exit 0 |
|
fi |
|
|
|
|
|
WEBDAV_BACKUP_PATH=${WEBDAV_BACKUP_PATH:-""} |
|
FULL_WEBDAV_URL="${WEBDAV_URL}" |
|
if [ -n "$WEBDAV_BACKUP_PATH" ]; then |
|
FULL_WEBDAV_URL="${WEBDAV_URL}/${WEBDAV_BACKUP_PATH}" |
|
fi |
|
|
|
|
|
restore_backup() { |
|
echo "开始从 WebDAV 下载最新备份..." |
|
python3 -c " |
|
import sys |
|
import os |
|
import tarfile |
|
import requests |
|
import shutil |
|
from webdav3.client import Client |
|
options = { |
|
'webdav_hostname': '$FULL_WEBDAV_URL', |
|
'webdav_login': '$WEBDAV_USERNAME', |
|
'webdav_password': '$WEBDAV_PASSWORD' |
|
} |
|
client = Client(options) |
|
backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('imageflow_backup_')] |
|
if not backups: |
|
print('没有找到备份文件') |
|
sys.exit() |
|
latest_backup = sorted(backups)[-1] |
|
print(f'最新备份文件:{latest_backup}') |
|
with requests.get(f'$FULL_WEBDAV_URL/{latest_backup}', auth=('$WEBDAV_USERNAME', '$WEBDAV_PASSWORD'), stream=True) as r: |
|
if r.status_code == 200: |
|
with open(f'/tmp/{latest_backup}', 'wb') as f: |
|
for chunk in r.iter_content(chunk_size=8192): |
|
f.write(chunk) |
|
print(f'成功下载备份文件到 /tmp/{latest_backup}') |
|
if os.path.exists(f'/tmp/{latest_backup}'): |
|
# 如果images目录已存在,先尝试安全删除它 |
|
if os.path.exists('/app/static/images'): |
|
print('尝试删除现有的images目录') |
|
try: |
|
# 首先尝试添加写权限 |
|
for root, dirs, files in os.walk('/app/static/images'): |
|
os.chmod(root, 0o777) |
|
for f in files: |
|
os.chmod(os.path.join(root, f), 0o666) |
|
# 然后尝试删除 |
|
shutil.rmtree('/app/static/images') |
|
print('成功删除images目录') |
|
except Exception as e: |
|
print(f'删除目录时出错: {e}') |
|
print('尝试使用系统命令删除') |
|
# 如果Python无法删除,尝试使用系统命令 |
|
os.system('chmod -R 777 /app/static/images') |
|
os.system('rm -rf /app/static/images') |
|
# 验证是否删除成功 |
|
if not os.path.exists('/app/static/images'): |
|
print('使用系统命令成功删除images目录') |
|
else: |
|
print('警告:无法删除现有目录,将尝试继续恢复') |
|
|
|
# 创建新的images目录(如果不存在) |
|
os.makedirs('/app/static/images', exist_ok=True) |
|
os.chmod('/app/static/images', 0o777) |
|
|
|
# 解压备份文件 |
|
with tarfile.open(f'/tmp/{latest_backup}', 'r:gz') as tar: |
|
tar.extractall('/app/') |
|
|
|
# 确保恢复后的目录有正确的权限 |
|
for root, dirs, files in os.walk('/app/static/images'): |
|
os.chmod(root, 0o777) |
|
for f in files: |
|
os.chmod(os.path.join(root, f), 0o666) |
|
|
|
print(f'成功从 {latest_backup} 恢复备份') |
|
else: |
|
print('下载的备份文件不存在') |
|
else: |
|
print(f'下载备份失败:{r.status_code}') |
|
" |
|
} |
|
|
|
|
|
echo "Downloading latest backup from WebDAV..." |
|
restore_backup |
|
|
|
|
|
sync_data() { |
|
while true; do |
|
echo "Starting sync process at $(date)" |
|
|
|
if [ -d "/app/static/images" ]; then |
|
timestamp=$(date +%Y%m%d_%H%M%S) |
|
backup_file="imageflow_backup_${timestamp}.tar.gz" |
|
|
|
|
|
cd /app |
|
tar -czf "/tmp/${backup_file}" static/images |
|
|
|
|
|
curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "/tmp/${backup_file}" "$FULL_WEBDAV_URL/${backup_file}" |
|
if [ $? -eq 0 ]; then |
|
echo "Successfully uploaded ${backup_file} to WebDAV" |
|
else |
|
echo "Failed to upload ${backup_file} to WebDAV" |
|
fi |
|
|
|
|
|
python3 -c " |
|
import sys |
|
from webdav3.client import Client |
|
options = { |
|
'webdav_hostname': '$FULL_WEBDAV_URL', |
|
'webdav_login': '$WEBDAV_USERNAME', |
|
'webdav_password': '$WEBDAV_PASSWORD' |
|
} |
|
client = Client(options) |
|
backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('imageflow_backup_')] |
|
backups.sort() |
|
if len(backups) > 1: |
|
to_delete = len(backups) - 1 |
|
for file in backups[:to_delete]: |
|
client.clean(file) |
|
print(f'Successfully deleted {file}.') |
|
else: |
|
print('Only {} backups found, no need to clean.'.format(len(backups))) |
|
" 2>&1 |
|
|
|
rm -f "/tmp/${backup_file}" |
|
else |
|
echo "/app/static/images directory does not exist, waiting for next sync..." |
|
fi |
|
|
|
SYNC_INTERVAL=${SYNC_INTERVAL:-600} |
|
echo "Next sync in ${SYNC_INTERVAL} seconds..." |
|
sleep $SYNC_INTERVAL |
|
done |
|
} |
|
|
|
|
|
sync_data & |
|
|
|
|
|
sleep 30 |
|
exec $APP_DIR/$APP_NAME$APP_SUFFIX |