File size: 1,903 Bytes
a5e200d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash

# This script assumes that start.sh has already cloned the repo and set up SSH.

# --- Paths ---
# The git repo is cloned into /tmp/repo by start.sh
GIT_REPO_DIR="/tmp/repo"
LOG_FILE="/tmp/sync.log"

# Live files to be backed up
XUI_DB_PATH="/tmp/x-ui.db"
XRAY_CONFIG_PATH="/usr/local/x-ui/bin/config.json"

# Destination for the backed up files inside the git repo
TARGET_DIR="${GIT_REPO_DIR}/x-ui-configs"

# Git commit message
COMMIT_MESSAGE="Automatic sync of x-ui configs"

# --- Functions ---

log() {
  echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}

# --- Main ---

log "--- Starting Hourly Sync ---"

# Navigate to the Git repository
if [ ! -d "$GIT_REPO_DIR/.git" ]; then
  log "Error: Git repository not found at $GIT_REPO_DIR. Exiting sync."
  exit 1
fi
cd "$GIT_REPO_DIR" || exit 1

# Configure git user for this operation
git config user.email "igor04091968@gmail.com"
git config user.name "igor04091968"

# Pull latest changes first to avoid conflicts
log "Pulling latest changes from remote..."
git pull --rebase

# Ensure the target directory for configs exists
mkdir -p "$TARGET_DIR"

# Copy live files into the git repo
log "Copying live db from ${XUI_DB_PATH} and config from ${XRAY_CONFIG_PATH} into git repo..."
cp -f "${XUI_DB_PATH}" "${TARGET_DIR}/x-ui.db"
cp -f "${XRAY_CONFIG_PATH}" "${TARGET_DIR}/config.json"

# Add, commit, and push
log "Adding changes to git..."
git add "$TARGET_DIR/x-ui.db" "$TARGET_DIR/config.json"

# Commit only if there are changes
if ! git diff-index --quiet HEAD; then
  log "Found changes, committing..."
  git commit -m "$COMMIT_MESSAGE"
  log "Committed changes."
  
  log "Pushing changes to remote..."
  if git push; then
    log "Successfully pushed changes to the remote repository."
  else
    log "Error: Failed to push changes."
  fi
else
  log "No changes to commit."
fi

log "--- Hourly Sync Finished ---"