flzta commited on
Commit
5871b78
·
verified ·
1 Parent(s): 5d70eb8

Update sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +59 -92
sync_data.sh CHANGED
@@ -1,84 +1,69 @@
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 "/opt/venv/bin/activate" ]; then
27
- source "/opt/venv/bin/activate"
28
- fi
29
 
30
  # 下载最新备份并恢复
31
  restore_backup() {
32
  echo "开始从 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
- import subprocess
40
  from webdav3.client import Client
41
-
42
- # 设置 WebDAV 客户端
43
  options = {
44
- 'webdav_hostname': '${FULL_WEBDAV_URL}',
45
- 'webdav_login': '${WEBDAV_USERNAME}',
46
- 'webdav_password': '${WEBDAV_PASSWORD}'
47
  }
48
  client = Client(options)
49
-
50
- # 列出备份文件
51
- try:
52
- backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('cloudreve_backup_')]
53
- except Exception as e:
54
- print(f'连接 WebDAV 服务器出错: {e}')
55
- sys.exit(1)
56
-
57
  if not backups:
58
- print('没有找到备份文件,跳过恢复步骤。')
59
- sys.exit(0)
60
-
61
- # 获取最新备份文件
62
  latest_backup = sorted(backups)[-1]
63
  print(f'最新备份文件:{latest_backup}')
64
-
65
- # 下载最新备份
66
- try:
67
- with requests.get(f'${FULL_WEBDAV_URL}/{latest_backup}', auth=('${WEBDAV_USERNAME}', '${WEBDAV_PASSWORD}'), stream=True) as r:
68
- if r.status_code == 200:
69
- with open(f'/tmp/{latest_backup}', 'wb') as f:
70
- for chunk in r.iter_content(chunk_size=8192):
71
- f.write(chunk)
72
- print(f'成功下载备份文件到 /tmp/{latest_backup}')
 
 
73
 
74
  # 解压备份文件
75
  with tarfile.open(f'/tmp/{latest_backup}', 'r:gz') as tar:
76
- tar.extractall('/opt/cloudreve/data/')
 
77
  print(f'成功从 {latest_backup} 恢复备份')
78
  else:
79
- print(f'下载备份失败:{r.status_code}')
80
- except Exception as e:
81
- print(f'恢复备份过程中出错: {e}')
82
  "
83
  }
84
 
@@ -86,33 +71,20 @@ except Exception as e:
86
  echo "Downloading latest backup from WebDAV..."
87
  restore_backup
88
 
89
- # 等待30秒后启动程序
90
- sleep 30
91
-
92
- # 启动程序 (根据您的日志,您运行的是 Cloudreve)
93
- ./cloudreve &
94
-
95
  # 同步函数
96
  sync_data() {
97
  while true; do
98
  echo "Starting sync process at $(date)"
99
 
100
- if [ ! -d "$CLOUDREVE_DATA_DIR" ]; then
101
- mkdir -p "$CLOUDREVE_DATA_DIR"
102
- echo "Data directory created."
103
- fi
104
 
105
- timestamp=$(date +%Y%m%d_%H%M%S)
106
- backup_file="cloudreve_backup_${timestamp}.tar.gz"
107
- backup_file_path="/tmp/${backup_file}"
108
 
109
- echo "Creating backup archive: ${backup_file_path}"
110
- # 压缩数据目录,排除可能存在的临时文件
111
- tar -czf "${backup_file_path}" --exclude='*.tmp' -C "$CLOUDREVE_DATA_DIR" .
112
-
113
- if [ -f "${backup_file_path}" ]; then
114
- echo "Uploading backup to WebDAV: ${FULL_WEBDAV_URL}/${backup_file}"
115
- curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "${backup_file_path}" "${FULL_WEBDAV_URL}/${backup_file}"
116
  if [ $? -eq 0 ]; then
117
  echo "Successfully uploaded ${backup_file} to WebDAV"
118
  else
@@ -120,45 +92,40 @@ sync_data() {
120
  fi
121
 
122
  # 清理旧备份文件
123
- WEBDAV_URL="$FULL_WEBDAV_URL" WEBDAV_USERNAME="$WEBDAV_USERNAME" WEBDAV_PASSWORD="$WEBDAV_PASSWORD" BACKUP_RETENTION="$BACKUP_RETENTION" python3 -c "
124
- import os
125
  from webdav3.client import Client
126
-
127
- WEBDAV_URL = os.environ.get('WEBDAV_URL')
128
- WEBDAV_USERNAME = os.environ.get('WEBDAV_USERNAME')
129
- WEBDAV_PASSWORD = os.environ.get('WEBDAV_PASSWORD')
130
- BACKUP_RETENTION = int(os.environ.get('BACKUP_RETENTION', '5'))
131
-
132
  options = {
133
- 'webdav_hostname': WEBDAV_URL,
134
- 'webdav_login': WEBDAV_USERNAME,
135
- 'webdav_password': WEBDAV_PASSWORD
136
  }
137
  client = Client(options)
138
- try:
139
- backups = sorted([file for file in client.list() if file.endswith('.tar.gz') and file.startswith('cloudreve_backup_')])
140
- if len(backups) > BACKUP_RETENTION:
141
- num_to_delete = len(backups) - BACKUP_RETENTION
142
- for file in backups[:to_delete]:
143
- try:
144
- client.clean(file)
145
- print(f'Successfully deleted old backup: {file}')
146
- except Exception as e:
147
- print(f'Error deleting {file}: {e}')
148
- except Exception as e:
149
- print(f'Error managing backups: {e}')
150
  " 2>&1
151
 
152
- rm -f "${backup_file_path}"
153
  else
154
- echo "Failed to create backup archive."
155
  fi
156
 
157
  SYNC_INTERVAL=${SYNC_INTERVAL:-600}
158
  echo "Next sync in ${SYNC_INTERVAL} seconds..."
159
- sleep "$SYNC_INTERVAL"
160
  done
161
  }
162
 
163
  # 启动同步进程
164
- sync_data &
 
 
 
 
 
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
+ exec /opt/cloudreve/cloudreve -c /opt/cloudreve/config.ini
7
  exit 0
8
  fi
9
 
10
+ # 设置备份路径
11
  WEBDAV_BACKUP_PATH=${WEBDAV_BACKUP_PATH:-""}
12
  FULL_WEBDAV_URL="${WEBDAV_URL}"
13
  if [ -n "$WEBDAV_BACKUP_PATH" ]; then
14
  FULL_WEBDAV_URL="${WEBDAV_URL}/${WEBDAV_BACKUP_PATH}"
15
  fi
16
 
17
+ # 定义 Cloudreve 主程序目录
18
+ CLOUDREVE_DIR="/opt/cloudreve"
19
+ BACKUP_PREFIX="cloudreve_backup"
20
 
21
  # 激活虚拟环境
22
+ source /opt/venv/bin/activate
 
 
23
 
24
  # 下载最新备份并恢复
25
  restore_backup() {
26
  echo "开始从 WebDAV 下载最新备份..."
27
+ python3 -c "
28
  import sys
29
  import os
30
  import tarfile
31
  import requests
32
  import shutil
 
33
  from webdav3.client import Client
 
 
34
  options = {
35
+ 'webdav_hostname': '$FULL_WEBDAV_URL',
36
+ 'webdav_login': '$WEBDAV_USERNAME',
37
+ 'webdav_password': '$WEBDAV_PASSWORD'
38
  }
39
  client = Client(options)
40
+ backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('$BACKUP_PREFIX')]
 
 
 
 
 
 
 
41
  if not backups:
42
+ print('没有找到备份文件')
43
+ sys.exit()
 
 
44
  latest_backup = sorted(backups)[-1]
45
  print(f'最新备份文件:{latest_backup}')
46
+ with requests.get(f'$FULL_WEBDAV_URL/{latest_backup}', auth=('$WEBDAV_USERNAME', '$WEBDAV_PASSWORD'), stream=True) as r:
47
+ if r.status_code == 200:
48
+ with open(f'/tmp/{latest_backup}', 'wb') as f:
49
+ for chunk in r.iter_content(chunk_size=8192):
50
+ f.write(chunk)
51
+ print(f'成功下载备份文件到 /tmp/{latest_backup}')
52
+ if os.path.exists(f'/tmp/{latest_backup}'):
53
+ # 如果目录已存在,先删除它
54
+ if os.path.exists('$CLOUDREVE_DIR'):
55
+ shutil.rmtree('$CLOUDREVE_DIR')
56
+ os.makedirs('$CLOUDREVE_DIR', exist_ok=True)
57
 
58
  # 解压备份文件
59
  with tarfile.open(f'/tmp/{latest_backup}', 'r:gz') as tar:
60
+ tar.extractall('$CLOUDREVE_DIR')
61
+
62
  print(f'成功从 {latest_backup} 恢复备份')
63
  else:
64
+ print('下载备份文件不存在')
65
+ else:
66
+ print(f'下载备份失败:{r.status_code}')
67
  "
68
  }
69
 
 
71
  echo "Downloading latest backup from WebDAV..."
72
  restore_backup
73
 
 
 
 
 
 
 
74
  # 同步函数
75
  sync_data() {
76
  while true; do
77
  echo "Starting sync process at $(date)"
78
 
79
+ if [ -d "$CLOUDREVE_DIR" ]; then
80
+ timestamp=$(date +%Y%m%d_%H%M%S)
81
+ backup_file="${BACKUP_PREFIX}_${timestamp}.tar.gz"
 
82
 
83
+ # 压缩数据��录
84
+ tar -czf "/tmp/${backup_file}" -C "$CLOUDREVE_DIR" cloudreve cloudreve.db config.ini
 
85
 
86
+ # 上传新备份到WebDAV
87
+ curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "/tmp/${backup_file}" "$FULL_WEBDAV_URL/${backup_file}"
 
 
 
 
 
88
  if [ $? -eq 0 ]; then
89
  echo "Successfully uploaded ${backup_file} to WebDAV"
90
  else
 
92
  fi
93
 
94
  # 清理旧备份文件
95
+ python3 -c "
96
+ import sys
97
  from webdav3.client import Client
 
 
 
 
 
 
98
  options = {
99
+ 'webdav_hostname': '$FULL_WEBDAV_URL',
100
+ 'webdav_login': '$WEBDAV_USERNAME',
101
+ 'webdav_password': '$WEBDAV_PASSWORD'
102
  }
103
  client = Client(options)
104
+ backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('$BACKUP_PREFIX')]
105
+ backups.sort()
106
+ if len(backups) > 5:
107
+ to_delete = len(backups) - 5
108
+ for file in backups[:to_delete]:
109
+ client.delete(client.cleanurl(file))
110
+ print(f'Successfully deleted {file}.')
111
+ else:
112
+ print('Only {} backups found, no need to clean.'.format(len(backups)))
 
 
 
113
  " 2>&1
114
 
115
+ rm -f "/tmp/${backup_file}"
116
  else
117
+ echo "Data directory does not exist yet, waiting for next sync..."
118
  fi
119
 
120
  SYNC_INTERVAL=${SYNC_INTERVAL:-600}
121
  echo "Next sync in ${SYNC_INTERVAL} seconds..."
122
+ sleep $SYNC_INTERVAL
123
  done
124
  }
125
 
126
  # 启动同步进程
127
+ sync_data &
128
+
129
+ # 启动 Cloudreve
130
+ sleep 30
131
+ exec /opt/cloudreve/cloudreve -c /opt/cloudreve/config.ini