b3BlbndlYnVp / sync_data.sh
eecsclub
first
6139f7d
Raw History Blame Contribute Delete
3.72 kB
# Check required environment variables
if [ -z "$G_NAME" ] || [ -z "$G_TOKEN" ]; then
echo "Missing required environment variables G_NAME or G_TOKEN"
exit 1
fi
# Parse repository name and username
IFS='/' read -r GITHUB_USER GITHUB_REPO <<< "$G_NAME"
# Build GitHub repository clone URL with token
REPO_URL="https://${G_TOKEN}@github.com/${G_NAME}.git"
mkdir -p ./data/github_data
# Clone repository
echo "Cloning repository..."
git clone "$REPO_URL" ./data/github_data || {
echo "Clone failed, please check if G_NAME and G_TOKEN are correct."
exit 1
}
if [ -f ./data/github_data/webui.db ]; then
cp ./data/github_data/webui.db ./data/webui.db
echo "Successfully pulled from GitHub repository"
else
echo "webui.db not found in GitHub repository, will push during sync"
fi
# Define sync function
sync_data() {
while true; do
# 1. Sync to GitHub
echo "Starting sync"
# Enter repository directory
cd ./data/github_data
# Configure Git user info
git config user.name "AutoSync Bot"
git config user.email "autosync@bot.com"
# Ensure on correct branch
git checkout main || git checkout master
# Copy latest database file
if [ -f "../webui.db" ]; then
cp ../webui.db ./webui.db
else
echo "Database not yet initialized"
fi
# Check for changes
if [[ -n $(git status -s) ]]; then
# Add all changes
git add webui.db
# Commit changes
git commit -m "Auto sync webui.db $(date '+%Y-%m-%d %H:%M:%S')"
# Push to remote repository
git push origin HEAD && {
echo "GitHub push successful"
} || {
echo "Push failed, waiting to retry..."
sleep 10
git push origin HEAD || {
echo "Retry failed, abandoning GitHub push."
}
}
# Return to parent directory
cd ..
cd ..
# 2. Sync to WebDAV
if [ -z "$WEBDAV_URL" ] || [ -z "$WEBDAV_USERNAME" ] || [ -z "$WEBDAV_PASSWORD" ]; then
echo "WebDAV environment variables missing, skipping WebDAV sync."
else
echo "Syncing to WebDAV..."
FILENAME="webui_$(date +'%m_%d').db"
# Check if file exists for upload
if [ -f ./data/webui.db ]; then
# Upload file using curl
curl -T ./data/webui.db --user "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" "$WEBDAV_URL/$FILENAME" && {
echo "WebDAV upload successful"
} || {
echo "WebDAV upload failed, waiting to retry..."
sleep 10
curl -T ./data/webui.db --user "$WEBDAV_USERNAME:$WEBDAV_PASSWORD" "$WEBDAV_URL/$FILENAME" || {
echo "Retry failed, abandoning WebDAV upload."
}
}
else
echo "webui.db file not found, skipping WebDAV sync"
fi
fi
else
# Return to parent directory
cd ..
cd ..
echo "GitHub: No database changes detected"
fi
# 3. Wait for sync interval
SYNC_INTERVAL=${SYNC_INTERVAL:-7200} # Default interval is 7200 seconds
echo "Current time: $(date '+%Y-%m-%d %H:%M:%S')"
echo "Waiting ${SYNC_INTERVAL} seconds before next sync..."
sleep $SYNC_INTERVAL
done
}
# Start sync process in background
sync_data &