Skip to content

Instantly share code, notes, and snippets.

@tiwiex
Created September 24, 2025 13:38
Show Gist options
  • Save tiwiex/8fb9b7b1eda61091a0655cd368357ddd to your computer and use it in GitHub Desktop.
Save tiwiex/8fb9b7b1eda61091a0655cd368357ddd to your computer and use it in GitHub Desktop.
backup_lms.sh
#!/bin/bash
BACKUP_DIR="/home/frappe/frappe-backups"
DATE=$(date +%Y%m%d_%H%M%S)
DB_NAME="lms"
MAX_BACKUPS=2
BACKUP_FILE="$BACKUP_DIR/lms_backup_$DATE.sql.gz"
ZIP_FILE="$BACKUP_DIR/lms_backup_$DATE.zip"
source $HOME/.mysql_creds
# Backup database and compress immediately
echo "Backup in progress for lms database .....")
mysqldump -u root -p$MYSQL_PASSWORD $DB_NAME | zip -q $ZIP_FILE -
# Verify backup was created
if [ -f "$ZIP_FILE" ]; then
echo "Backup successful: $ZIP_FILE"
echo "File size: $(du -h $ZIP_FILE | cut -f1)"
else
echo "Backup failed!"
exit 1
fi
# Function to keep only last N backups
rotate_backups() {
# Get list of backup files sorted by time (newest first)
backups=$(find . -name "lms_backup_*.zip" -type f -printf "%T@ %p\n" | sort -nr | cut -d' ' -f2-)
count=0
echo "$backups" | while read backup; do
count=$((count + 1))
if [ $count -gt $MAX_BACKUPS ]; then
echo "Removing old backup: $backup"
git rm "$backup"
fi
done
}
# Add the new backup file
git add $ZIP_FILE
git add backup_lms.sh
# Rotate old backups (keep only last 5)
rotate_backups
# Commit the changes
git commit -m "LMS Backup rotation: $DATE (keeping last $MAX_BACKUPS backups)"
git push origin frappe-live
cd /home/lms/public_html/opigno
git add .
git commit -m "Website Backup from Script: $DATE"
git push origin main
git push origin dev
echo "LMS backup complete"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment