gallyga commited on
Commit
e359b99
·
verified ·
1 Parent(s): 1a4cf84

Update sync_data.sh

Browse files
Files changed (1) hide show
  1. sync_data.sh +33 -27
sync_data.sh CHANGED
@@ -3,7 +3,6 @@
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 java ${JVM_OPTS} -jar /opt/halo/halo.jar
7
  exit 0
8
  fi
9
 
@@ -15,7 +14,7 @@ if [ -n "$WEBDAV_BACKUP_PATH" ]; then
15
  fi
16
 
17
  # 激活虚拟环境
18
- source /opt/venv/bin/activate
19
 
20
  # 下载最新备份并恢复
21
  restore_backup() {
@@ -33,7 +32,7 @@ options = {
33
  'webdav_password': '$WEBDAV_PASSWORD'
34
  }
35
  client = Client(options)
36
- backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('halo_backup_')]
37
  if not backups:
38
  print('没有找到备份文件')
39
  sys.exit()
@@ -47,13 +46,13 @@ with requests.get(f'$FULL_WEBDAV_URL/{latest_backup}', auth=('$WEBDAV_USERNAME',
47
  print(f'成功下载备份文件到 /tmp/{latest_backup}')
48
  if os.path.exists(f'/tmp/{latest_backup}'):
49
  # 如果目录已存在,先删除它
50
- if os.path.exists(os.path.expanduser('~/.halo2')):
51
- shutil.rmtree(os.path.expanduser('~/.halo2'))
52
- os.makedirs(os.path.expanduser('~/.halo2'), exist_ok=True)
53
 
54
  # 解压备份文件
55
  with tarfile.open(f'/tmp/{latest_backup}', 'r:gz') as tar:
56
- tar.extractall(os.path.expanduser('~/.halo2'))
57
 
58
  print(f'成功从 {latest_backup} 恢复备份')
59
  else:
@@ -67,28 +66,38 @@ with requests.get(f'$FULL_WEBDAV_URL/{latest_backup}', auth=('$WEBDAV_USERNAME',
67
  echo "Downloading latest backup from WebDAV..."
68
  restore_backup
69
 
 
 
 
 
 
 
70
  # 同步函数
71
  sync_data() {
72
  while true; do
73
  echo "Starting sync process at $(date)"
74
 
75
- if [ -d ~/.halo2 ]; then
76
- timestamp=$(date +%Y%m%d_%H%M%S)
77
- backup_file="halo_backup_${timestamp}.tar.gz"
 
78
 
79
- # 压缩数据目录
80
- tar -czf "/tmp/${backup_file}" -C ~/.halo2 .
81
 
82
- # 上传新备份到WebDAV
83
- curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "/tmp/${backup_file}" "$FULL_WEBDAV_URL/${backup_file}"
84
- if [ $? -eq 0 ]; then
85
- echo "Successfully uploaded ${backup_file} to WebDAV"
86
- else
87
- echo "Failed to upload ${backup_file} to WebDAV"
88
- fi
89
 
90
- # 清理旧备份文件
91
- python3 -c "
 
 
 
 
 
 
 
 
92
  import sys
93
  from webdav3.client import Client
94
  options = {
@@ -97,7 +106,7 @@ options = {
97
  'webdav_password': '$WEBDAV_PASSWORD'
98
  }
99
  client = Client(options)
100
- backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('halo_backup_')]
101
  backups.sort()
102
  if len(backups) > 5:
103
  to_delete = len(backups) - 5
@@ -108,11 +117,8 @@ else:
108
  print('Only {} backups found, no need to clean.'.format(len(backups)))
109
  " 2>&1
110
 
111
- rm -f "/tmp/${backup_file}"
112
- else
113
- echo "Data directory does not exist yet, waiting for next sync..."
114
- fi
115
-
116
  SYNC_INTERVAL=${SYNC_INTERVAL:-600}
117
  echo "Next sync in ${SYNC_INTERVAL} seconds..."
118
  sleep $SYNC_INTERVAL
 
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
 
 
14
  fi
15
 
16
  # 激活虚拟环境
17
+ source $HOME/venv/bin/activate
18
 
19
  # 下载最新备份并恢复
20
  restore_backup() {
 
32
  'webdav_password': '$WEBDAV_PASSWORD'
33
  }
34
  client = Client(options)
35
+ backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('alist_backup_')]
36
  if not backups:
37
  print('没有找到备份文件')
38
  sys.exit()
 
46
  print(f'成功下载备份文件到 /tmp/{latest_backup}')
47
  if os.path.exists(f'/tmp/{latest_backup}'):
48
  # 如果目录已存在,先删除它
49
+ if os.path.exists('$HOME/data'):
50
+ shutil.rmtree('$HOME/data')
51
+ os.makedirs('$HOME/data', exist_ok=True)
52
 
53
  # 解压备份文件
54
  with tarfile.open(f'/tmp/{latest_backup}', 'r:gz') as tar:
55
+ tar.extractall('$HOME/data')
56
 
57
  print(f'成功从 {latest_backup} 恢复备份')
58
  else:
 
66
  echo "Downloading latest backup from WebDAV..."
67
  restore_backup
68
 
69
+ # 等待30秒后启动程序
70
+ sleep 30
71
+
72
+ # 启动程序
73
+ ./app server &
74
+
75
  # 同步函数
76
  sync_data() {
77
  while true; do
78
  echo "Starting sync process at $(date)"
79
 
80
+ if [ ! -d $HOME/data ]; then
81
+ mkdir -p $HOME/data
82
+ echo "Data directory created."
83
+ fi
84
 
85
+ timestamp=$(date +%Y%m%d_%H%M%S)
86
+ backup_file="alist_backup_${timestamp}.tar.gz"
87
 
88
+ # 压缩数据目录
89
+ tar -czf "/tmp/${backup_file}" -C $HOME/data .
 
 
 
 
 
90
 
91
+ # 上传新备份到WebDAV
92
+ curl -u "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" -T "/tmp/${backup_file}" "$FULL_WEBDAV_URL/${backup_file}"
93
+ if [ $? -eq 0 ]; then
94
+ echo "Successfully uploaded ${backup_file} to WebDAV"
95
+ else
96
+ echo "Failed to upload ${backup_file} to WebDAV"
97
+ fi
98
+
99
+ # 清理旧备份文件
100
+ python3 -c "
101
  import sys
102
  from webdav3.client import Client
103
  options = {
 
106
  'webdav_password': '$WEBDAV_PASSWORD'
107
  }
108
  client = Client(options)
109
+ backups = [file for file in client.list() if file.endswith('.tar.gz') and file.startswith('alist_backup_')]
110
  backups.sort()
111
  if len(backups) > 5:
112
  to_delete = len(backups) - 5
 
117
  print('Only {} backups found, no need to clean.'.format(len(backups)))
118
  " 2>&1
119
 
120
+ rm -f "/tmp/${backup_file}"
121
+
 
 
 
122
  SYNC_INTERVAL=${SYNC_INTERVAL:-600}
123
  echo "Next sync in ${SYNC_INTERVAL} seconds..."
124
  sleep $SYNC_INTERVAL