File size: 3,144 Bytes
63209b7
 
 
 
 
 
 
 
 
6a0b66b
63209b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a0b66b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63209b7
6a0b66b
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/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/restored
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"
}

# --- Backup Logic ---
do_backup() {
    log "--- Starting Backup ---"

    # 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 "--- Backup Finished ---"
}

# --- Restore Logic ---
do_restore() {
    log "--- Starting Restore ---"

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

    log "Pulling latest changes from remote..."
    git pull --rebase

    if [ -f "${TARGET_DIR}/x-ui.db" ] && [ -f "${TARGET_DIR}/config.json" ]; then
        log "Restoring files from git repo to live location..."
        cp -f "${TARGET_DIR}/x-ui.db" "${XUI_DB_PATH}"
        cp -f "${TARGET_DIR}/config.json" "${XRAY_CONFIG_PATH}"
        log "Restore complete. You may need to restart the x-ui service for changes to take effect."
    else
        log "Error: Config files not found in the repository at ${TARGET_DIR}. Cannot restore."
        exit 1
    fi

    log "--- Restore Finished ---"
}


# --- Main ---
case "$1" in
    backup)
        do_backup
        ;;
    restore)
        do_restore
        ;;
    *)
        log "No valid argument provided. Defaulting to backup."
        do_backup
        ;;
esac