cfjy888 commited on
Commit
b0aa259
·
verified ·
1 Parent(s): 85d7850

Update sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +97 -117
sync_data.sh CHANGED
@@ -1,151 +1,131 @@
1
  #!/bin/bash
2
- # sync_data.sh
3
 
4
- # 设置环境变量
5
- DATA_DIR=${DATA_DIR:-"/app/data"}
6
- BACKUP_DIR=${BACKUP_DIR:-"/app/backup"}
7
- LOG_FILE="$BACKUP_DIR/sync.log"
8
- WEBDAV_URL=${WEBDAV_URL}
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
- log() {
19
- echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
20
- }
 
 
21
 
22
  # 激活虚拟环境
23
- source /app/venv/bin/activate
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
- if [ -z "$LATEST_BACKUP" ]; then
54
- log "未找到备份文件"
55
- return
56
- fi
57
 
58
- # 下载备份文件
59
- log "下载最新备份: $LATEST_BACKUP"
 
60
  python3 -c "
 
 
 
61
  import requests
 
62
  from webdav3.client import Client
63
  options = {
64
- 'webdav_hostname': '$WEBDAV_URL',
65
  'webdav_login': '$WEBDAV_USERNAME',
66
- 'webdav_password': '$WEBDAV_PASSWORD',
67
- 'webdav_root': '$WEBDAV_BACKUP_PATH'
68
  }
69
  client = Client(options)
70
- with client.download_to_stream('$LATEST_BACKUP', '/tmp/latest_backup.tar.gz') as response:
71
- with open('/tmp/latest_backup.tar.gz', 'wb') as f:
72
- for chunk in response.iter_content(chunk_size=8192):
73
- f.write(chunk)
74
- print('备份下载完成')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- sync_backup() {
 
 
 
 
91
  while true; do
92
- log "开始同步备份..."
93
- TIMESTAMP=$(date +%Y%m%d_%H%M%S)
94
- BACKUP_FILE="alist_backup_${TIMESTAMP}.tar.gz"
95
 
96
- # 创建备份
97
- if [ -d "$DATA_DIR" ]; then
98
- log "压缩数据目录: $DATA_DIR"
99
- tar -czf "/tmp/$BACKUP_FILE" -C "$DATA_DIR" .
100
- log "上传备份到 WebDAV: $BACKUP_FILE"
101
- python3 -c "
102
- from webdav3.client import Client
103
- options = {
104
- 'webdav_hostname': '$WEBDAV_URL',
105
- 'webdav_login': '$WEBDAV_USERNAME',
106
- 'webdav_password': '$WEBDAV_PASSWORD',
107
- 'webdav_root': '$WEBDAV_BACKUP_PATH'
108
- }
109
- client = Client(options)
110
- with open('/tmp/$BACKUP_FILE', 'rb') as f:
111
- client.upload_stream(f, '$BACKUP_FILE')
112
- print('备份上传完成')
113
- "
114
- log "清理本地备份文件"
115
- rm -f "/tmp/$BACKUP_FILE"
116
  else
117
- log "数据目录不存在,跳过备份"
118
  fi
119
 
120
- # 清理旧备份
121
- log "清理旧备份(保留最近 5 个)"
122
  python3 -c "
 
123
  from webdav3.client import Client
124
  options = {
125
- 'webdav_hostname': '$WEBDAV_URL',
126
  'webdav_login': '$WEBDAV_USERNAME',
127
- 'webdav_password': '$WEBDAV_PASSWORD',
128
- 'webdav_root': '$WEBDAV_BACKUP_PATH'
129
  }
130
  client = Client(options)
131
- backups = sorted([f for f in client.list() if f.startswith('alist_backup_')], reverse=True)
 
132
  if len(backups) > 5:
133
- for f in backups[5:]:
134
- client.clean(f)
135
- print(f'已删除旧备份: {f}')
 
 
 
136
  "
137
- log "同步完成,等待下一次同步..."
 
 
 
 
138
  sleep $SYNC_INTERVAL
139
  done
140
  }
141
 
142
- # 主逻辑
143
- case "$1" in
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