Yash commited on
Commit
b191695
·
verified ·
1 Parent(s): 308cf5b

Update data-sync.sh

Browse files
Files changed (1) hide show
  1. data-sync.sh +14 -91
data-sync.sh CHANGED
@@ -1,5 +1,4 @@
1
  #!/bin/bash
2
- # Note: Ensure that date, git, curl and other tools are installed in the system when the script is executed, and set the TZ timezone environment (can be temporarily specified in each date command)
3
 
4
  # Check necessary environment variables
5
  if [ -z "$G_NAME" ] || [ -z "$G_TOKEN" ]; then
@@ -12,7 +11,7 @@ IFS='/' read -r GITHUB_USER GITHUB_REPO <<< "$G_NAME"
12
 
13
  # Build GitHub repository clone URL with token
14
  REPO_URL="https://${G_TOKEN}@github.com/${G_NAME}.git"
15
- mkdir -p ./data/github_data
16
 
17
  # Clone repository
18
  echo "Cloning repository..."
@@ -28,112 +27,36 @@ else
28
  echo "webui.db not found in GitHub repository, will push during sync"
29
  fi
30
 
31
- # Define sync function, according to Beijing time 08:00~24:00 (including hourly sync) requirements
32
  sync_data() {
33
  while true; do
34
- # Use Asia/Shanghai timezone to get current time and its components
35
- CURRENT_TS=$(TZ=Asia/Shanghai date +%s)
36
- CURRENT_DATE=$(TZ=Asia/Shanghai date '+%Y-%m-%d')
37
- CURRENT_HOUR=$(TZ=Asia/Shanghai date +%H) # 00~23
38
- CURRENT_MIN=$(TZ=Asia/Shanghai date +%M)
39
- CURRENT_SEC=$(TZ=Asia/Shanghai date +%S)
40
-
41
- # Calculate next sync target timestamp (Beijing time)
42
- # If current time is before 08:00, target is today 08:00
43
- if [ "$CURRENT_HOUR" -lt 8 ]; then
44
- TARGET_TS=$(TZ=Asia/Shanghai date -d "${CURRENT_DATE} 08:00:00" +%s)
45
- # If between 08:00 to 22:59, next hour is on the same day
46
- elif [ "$CURRENT_HOUR" -ge 8 ] && [ "$CURRENT_HOUR" -lt 23 ]; then
47
- # If exactly at the hour (seconds and minutes are 0) then sync immediately
48
- if [ "$CURRENT_MIN" -eq 0 ] && [ "$CURRENT_SEC" -eq 0 ]; then
49
- TARGET_TS=$CURRENT_TS
50
- else
51
- NEXT_HOUR=$((10#$CURRENT_HOUR + 1))
52
- TARGET_TS=$(TZ=Asia/Shanghai date -d "${CURRENT_DATE} ${NEXT_HOUR}:00:00" +%s)
53
- fi
54
- # If current time is between 23:00~23:59, next target is next day 00:00 (which is 24:00 sync)
55
- else # CURRENT_HOUR == 23
56
- if [ "$CURRENT_MIN" -eq 0 ] && [ "$CURRENT_SEC" -eq 0 ]; then
57
- TARGET_TS=$CURRENT_TS
58
- else
59
- TOMORROW=$(TZ=Asia/Shanghai date -d "tomorrow" '+%Y-%m-%d')
60
- TARGET_TS=$(TZ=Asia/Shanghai date -d "${TOMORROW} 00:00:00" +%s)
61
- fi
62
- fi
63
-
64
- # Calculate wait time (if exactly at sync time then sleep_time is 0)
65
- SLEEP_TIME=$(( TARGET_TS - CURRENT_TS ))
66
- if [ "$SLEEP_TIME" -gt 0 ]; then
67
- echo "Time until next sync: ${SLEEP_TIME} seconds (Beijing time next sync: $(TZ=Asia/Shanghai date -d "@$TARGET_TS" '+%Y-%m-%d %H:%M:%S'))"
68
- sleep "$SLEEP_TIME"
69
- fi
70
-
71
- # Output current Beijing time during sync
72
- CURRENT_TIME=$(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S')
73
- echo "Current time $CURRENT_TIME"
74
 
75
- # ---- Start sync process ----
76
-
77
- # 1. Sync to GitHub
78
- echo "Starting GitHub sync..."
79
  cd ./data/github_data || { echo "Failed to change directory"; exit 1; }
80
  git config user.name "AutoSync Bot"
81
  git config user.email "autosync@bot.com"
82
-
83
- # Ensure on main branch, if switch fails try master branch
84
  git checkout main 2>/dev/null || git checkout master
85
 
86
- # Copy latest database file to repository directory
87
- if [ -f "../webui.db" ]; then
88
- cp ../webui.db ./webui.db
89
- else
90
  echo "Database not yet initialized"
91
- fi
92
 
93
- # Check if there are changes
94
  if [[ -n $(git status -s) ]]; then
95
  git add webui.db
96
- git commit -m "Auto sync webui.db $(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S')"
97
- git push origin HEAD && {
98
- echo "GitHub push successful"
99
- } || {
100
- echo "Push failed, waiting for retry..."
101
- sleep 10
102
- git push origin HEAD || {
103
- echo "Retry failed, abandoning GitHub push."
104
- }
105
- }
106
  else
107
  echo "GitHub: No database changes detected"
108
  fi
109
- # Return to main directory
110
- cd ../..
111
-
112
- # 2. Sync to WebDAV (if environment variables are complete)
113
- if [ -z "$WEBDAV_URL" ] || [ -z "$WEBDAV_USERNAME" ] || [ -z "$WEBDAV_PASSWORD" ]; then
114
- echo "WebDAV environment variables missing, skipping WebDAV sync."
115
- else
116
- echo "Starting WebDAV sync..."
117
- FILENAME="webui_$(TZ=Asia/Shanghai date +'%m_%d').db"
118
- if [ -f ./data/webui.db ]; then
119
- curl -T ./data/webui.db --user "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" "$WEBDAV_URL/$FILENAME" && {
120
- echo "WebDAV upload successful"
121
- } || {
122
- echo "WebDAV upload failed, waiting for retry..."
123
- sleep 10
124
- curl -T ./data/webui.db --user "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" "$WEBDAV_URL/$FILENAME" || {
125
- echo "Retry failed, abandoning WebDAV upload."
126
- }
127
- }
128
- else
129
- echo "webui.db file not found, skipping WebDAV sync."
130
- fi
131
- fi
132
-
133
- # ---- Sync process complete, next loop will automatically calculate wait time based on current Beijing time ----
134
 
 
 
 
135
  done
136
  }
137
 
138
  # Start sync process in background
139
- sync_data &
 
1
  #!/bin/bash
 
2
 
3
  # Check necessary environment variables
4
  if [ -z "$G_NAME" ] || [ -z "$G_TOKEN" ]; then
 
11
 
12
  # Build GitHub repository clone URL with token
13
  REPO_URL="https://${G_TOKEN}@github.com/${G_NAME}.git"
14
+ mkdir -p ./data/github_data
15
 
16
  # Clone repository
17
  echo "Cloning repository..."
 
27
  echo "webui.db not found in GitHub repository, will push during sync"
28
  fi
29
 
30
+ # Sync GitHub every 2 minutes using America/Detroit time
31
  sync_data() {
32
  while true; do
33
+ CURRENT_TIME=$(TZ=America/Detroit date '+%Y-%m-%d %H:%M:%S')
34
+ echo "Current time $CURRENT_TIME - Starting GitHub sync..."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
 
 
 
 
36
  cd ./data/github_data || { echo "Failed to change directory"; exit 1; }
37
  git config user.name "AutoSync Bot"
38
  git config user.email "autosync@bot.com"
 
 
39
  git checkout main 2>/dev/null || git checkout master
40
 
41
+ if [ -f "../webui.db" ]; then
42
+ cp ../webui.db ./webui.db
43
+ else
 
44
  echo "Database not yet initialized"
45
+ fi
46
 
 
47
  if [[ -n $(git status -s) ]]; then
48
  git add webui.db
49
+ git commit -m "Auto sync webui.db $(TZ=America/Detroit date '+%Y-%m-%d %H:%M:%S')"
50
+ git push origin HEAD && echo "GitHub push successful" || echo "GitHub push failed"
 
 
 
 
 
 
 
 
51
  else
52
  echo "GitHub: No database changes detected"
53
  fi
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
+ cd ../..
56
+ echo "Waiting 2 minutes until next sync..."
57
+ sleep 120 # Wait 2 minutes
58
  done
59
  }
60
 
61
  # Start sync process in background
62
+ sync_data &