Spaces:
Paused
Paused
Create sync.sh
Browse files
sync.sh
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
# This script assumes that start.sh has already cloned the repo and set up SSH.
|
| 4 |
+
|
| 5 |
+
# --- Paths ---
|
| 6 |
+
# The git repo is cloned into /tmp/repo by start.sh
|
| 7 |
+
GIT_REPO_DIR="/tmp/repo"
|
| 8 |
+
LOG_FILE="/tmp/sync.log"
|
| 9 |
+
|
| 10 |
+
# Live files to be backed up
|
| 11 |
+
XUI_DB_PATH="/tmp/x-ui.db"
|
| 12 |
+
XRAY_CONFIG_PATH="/usr/local/x-ui/bin/config.json"
|
| 13 |
+
|
| 14 |
+
# Destination for the backed up files inside the git repo
|
| 15 |
+
TARGET_DIR="${GIT_REPO_DIR}/x-ui-configs"
|
| 16 |
+
|
| 17 |
+
# Git commit message
|
| 18 |
+
COMMIT_MESSAGE="Automatic sync of x-ui configs"
|
| 19 |
+
|
| 20 |
+
# --- Functions ---
|
| 21 |
+
|
| 22 |
+
log() {
|
| 23 |
+
echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# --- Main ---
|
| 27 |
+
|
| 28 |
+
log "--- Starting Hourly Sync ---"
|
| 29 |
+
|
| 30 |
+
# Navigate to the Git repository
|
| 31 |
+
if [ ! -d "$GIT_REPO_DIR/.git" ]; then
|
| 32 |
+
log "Error: Git repository not found at $GIT_REPO_DIR. Exiting sync."
|
| 33 |
+
exit 1
|
| 34 |
+
fi
|
| 35 |
+
cd "$GIT_REPO_DIR" || exit 1
|
| 36 |
+
|
| 37 |
+
# Configure git user for this operation
|
| 38 |
+
git config user.email "igor04091968@gmail.com"
|
| 39 |
+
git config user.name "igor04091968"
|
| 40 |
+
|
| 41 |
+
# Pull latest changes first to avoid conflicts
|
| 42 |
+
log "Pulling latest changes from remote..."
|
| 43 |
+
git pull --rebase
|
| 44 |
+
|
| 45 |
+
# Ensure the target directory for configs exists
|
| 46 |
+
mkdir -p "$TARGET_DIR"
|
| 47 |
+
|
| 48 |
+
# Copy live files into the git repo
|
| 49 |
+
log "Copying live db from ${XUI_DB_PATH} and config from ${XRAY_CONFIG_PATH} into git repo..."
|
| 50 |
+
cp -f "${XUI_DB_PATH}" "${TARGET_DIR}/x-ui.db"
|
| 51 |
+
cp -f "${XRAY_CONFIG_PATH}" "${TARGET_DIR}/config.json"
|
| 52 |
+
|
| 53 |
+
# Add, commit, and push
|
| 54 |
+
log "Adding changes to git..."
|
| 55 |
+
git add "$TARGET_DIR/x-ui.db" "$TARGET_DIR/config.json"
|
| 56 |
+
|
| 57 |
+
# Commit only if there are changes
|
| 58 |
+
if ! git diff-index --quiet HEAD; then
|
| 59 |
+
log "Found changes, committing..."
|
| 60 |
+
git commit -m "$COMMIT_MESSAGE"
|
| 61 |
+
log "Committed changes."
|
| 62 |
+
|
| 63 |
+
log "Pushing changes to remote..."
|
| 64 |
+
if git push; then
|
| 65 |
+
log "Successfully pushed changes to the remote repository."
|
| 66 |
+
else
|
| 67 |
+
log "Error: Failed to push changes."
|
| 68 |
+
fi
|
| 69 |
+
else
|
| 70 |
+
log "No changes to commit."
|
| 71 |
+
fi
|
| 72 |
+
|
| 73 |
+
log "--- Hourly Sync Finished ---"
|