Ampere123/reader
0
1#!/bin/sh2 3# 检查环境变量4if [ -z "$WEBDAV_URL" ] || [ -z "$WEBDAV_USERNAME" ] || [ -z "$WEBDAV_PASSWORD" ]; then5 echo "Starting without backup functionality - missing WEBDAV_URL, WEBDAV_USERNAME, or WEBDAV_PASSWORD"6 exec java -jar /app/bin/reader.jar7 exit 08fi9 10# 设置备份路径11WEBDAV_BACKUP_PATH=${WEBDAV_BACKUP_PATH:-""}12FULL_WEBDAV_URL="${WEBDAV_URL}"13if [ -n "$WEBDAV_BACKUP_PATH" ]; then14 FULL_WEBDAV_URL="${WEBDAV_URL}/${WEBDAV_BACKUP_PATH}"15fi16 17# 下载最新备份并恢复18restore_backup() {19 python3 -c "20import sys21import os22import tarfile23import requests24from webdav3.client import Client25options = {26 'webdav_hostname': '$FULL_WEBDAV_URL',27 'webdav_login': '$WEBDAV_USERNAME',28 'webdav_password': '$WEBDAV_PASSWORD'29}30client = Client(options)31backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('reader_backup_')]32if not backups:33 print('No backup files found')34 sys.exit()35latest_backup = sorted(backups)[-1]36with requests.get(f'$FULL_WEBDAV_URL/{latest_backup}', auth=('$WEBDAV_USERNAME', '$WEBDAV_PASSWORD'), stream=True) as r:37 if r.status_code == 200:38 with open(f'/tmp/{latest_backup}', 'wb') as f:39 for chunk in r.iter_content(chunk_size=8192):40 f.write(chunk)41 42 if os.path.exists(f'/tmp/{latest_backup}'):43 with tarfile.open(f'/tmp/{latest_backup}', 'r:gz') as tar:44 tar.extractall('/storage')45 print(f'Successfully restored backup from {latest_backup}')46 else:47 print('Failed to download backup file')48 else:49 print(f'Failed to download backup: {r.status_code}')50"51}52 53# 首次启动时下载最新备份54echo "Downloading latest backup from WebDAV..."55restore_backup56 57# 同步函数58sync_data() {59 while true; do60 echo "Starting sync process at $(date)"61 62 if [ -d /storage ]; then63 timestamp=$(date +%Y%m%d_%H%M%S)64 backup_file="reader_backup_${timestamp}.tar.gz"65 66 # 压缩数据目录67 tar -czf "/tmp/${backup_file}" -C /storage .68 69 # 上传新备份到WebDAV70 curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "/tmp/${backup_file}" "$FULL_WEBDAV_URL/${backup_file}"71 if [ $? -eq 0 ]; then72 echo "Successfully uploaded ${backup_file} to WebDAV"73 else74 echo "Failed to upload ${backup_file} to WebDAV"75 fi76 77 # 清理旧备份文件78 python3 -c "79import sys80from webdav3.client import Client81options = {82 'webdav_hostname': '$FULL_WEBDAV_URL',83 'webdav_login': '$WEBDAV_USERNAME',84 'webdav_password': '$WEBDAV_PASSWORD'85}86client = Client(options)87backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('reader_backup_')]88backups.sort()89if len(backups) > 2:90 to_delete = len(backups) - 291 for file in backups[:to_delete]:92 client.clean(file)93 print(f'Successfully deleted {file}.')94else:95 print('Only {} backups found, no need to clean.'.format(len(backups)))96" 2>&197 98 rm -f "/tmp/${backup_file}"99 else100 echo "/storage directory does not exist, waiting for next sync..."101 fi102 103 SYNC_INTERVAL=${SYNC_INTERVAL:-600}104 sleep $SYNC_INTERVAL105 done106}107 108# 后台启动同步进程109sync_data &110 111# 启动应用程序主进程112exec java -jar /app/bin/reader.jar