mirror of
https://github.com/myronblair/tomtomgames
synced 2026-06-30 17:51:08 -05:00
Add automated backup system
- api/backup.php: list/create/download/delete backups; streams zip directly for downloads; 7-backup rolling prune on each create - Each backup is a single zip containing all of public_html + a full mysqldump of tomt_ttg_db - Cron at 2 AM daily via /usr/local/bin/ttg-backup.sh (already installed) - Admin UI: 💾 Backups nav item under System section; shows backup list with date/size, Download + Delete per row; Create Backup Now button with live status; auto-loads when section is opened Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
#!/bin/bash
|
||||
# TomTomGames automated backup — runs daily at 2 AM via cron
|
||||
# Cron entry: 0 2 * * * /usr/local/bin/ttg-backup.sh >> /home/tomtomgames.com/backups/backup.log 2>&1
|
||||
|
||||
BACKUP_DIR="/home/tomtomgames.com/backups"
|
||||
SITE_DIR="/home/tomtomgames.com/public_html"
|
||||
DB_NAME="tomt_ttg_db"
|
||||
DB_USER="tomt_ttg_user"
|
||||
DB_PASS='q#q+mrOcozsa7I6J'
|
||||
DATE=$(date +%Y-%m-%d_%H-%M-%S)
|
||||
SQL_FILE="/tmp/ttg_db_${DATE}.sql"
|
||||
ZIP_FILE="${BACKUP_DIR}/ttg_backup_${DATE}.zip"
|
||||
|
||||
mkdir -p "$BACKUP_DIR"
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting backup..."
|
||||
|
||||
# Export database
|
||||
/usr/bin/mysqldump -u "$DB_USER" "-p${DB_PASS}" "$DB_NAME" > "$SQL_FILE" 2>&1
|
||||
if [ $? -ne 0 ] || [ ! -s "$SQL_FILE" ]; then
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: Database export failed"
|
||||
rm -f "$SQL_FILE"
|
||||
exit 1
|
||||
fi
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Database exported ($(du -sh "$SQL_FILE" | cut -f1))"
|
||||
|
||||
# Create zip archive (site files + db dump)
|
||||
/usr/bin/zip -r "$ZIP_FILE" "$SITE_DIR" "$SQL_FILE" -x "*/backups/*" > /dev/null 2>&1
|
||||
RC=$?
|
||||
rm -f "$SQL_FILE"
|
||||
|
||||
if [ $RC -ne 0 ] || [ ! -f "$ZIP_FILE" ]; then
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: Archive creation failed"
|
||||
rm -f "$ZIP_FILE"
|
||||
exit 1
|
||||
fi
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Archive created: $(basename "$ZIP_FILE") ($(du -sh "$ZIP_FILE" | cut -f1))"
|
||||
|
||||
# Keep only the 7 most recent backups
|
||||
ls -t "${BACKUP_DIR}"/ttg_backup_*.zip 2>/dev/null | tail -n +8 | while read old; do
|
||||
rm -f "$old"
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Pruned old backup: $(basename "$old")"
|
||||
done
|
||||
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Backup complete."
|
||||
Reference in New Issue
Block a user