Skip to content

Instantly share code, notes, and snippets.

@random-robbie
Created April 27, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save random-robbie/faf056609b27535f8962 to your computer and use it in GitHub Desktop.
Save random-robbie/faf056609b27535f8962 to your computer and use it in GitHub Desktop.
#!/bin/bash
################################################
#
# Backup all MySQL databases in separate files and compress those and also rsync a directory so all scripts are backed up
# Furthermore the script will create a folder with the current time stamp
# @author: Per Lasse Baasch (http://skycube.net) - Original Author
# @author: Robert Wiggins - Altered to include rysnc and removed timestamp from mysql-backup file names
# NOTE: MySQL and gzip installed on the system
# and you will need write permissions in the directory where you executing this script
# You will also need to run ssh-copy-id to the server you are backing up so that you do not need a password to do the backup.
################################################
# MySQL User
USER='root'
# MySQL Password
PASSWORD='ROOTPASS'
# Backup Directory - NO TAILING SLASH!
OUTPUT="."
BACKUPSERVER='theservertobackup.com'
WWWDIR='/var/www/'
LOCALBACKUPFOLDER='/var/www/backup/'
##### And
TIMESTAMP=`date +%Y%m%d_%H%M%S`;
mkdir $OUTPUT;
cd $OUTPUT;
echo "Starting MySQL Backup";
echo `date`;
rm -f *.gz
databases=`mysql --host=$BACKUPSERVER --user=$USER --password=$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
for db in $databases; do
if [[ "$db" != "information_schema" ]] && [[ "$db" != _* ]] ; then
echo "Dumping database: $db"
mysqldump -h $BACKUPSERVER --force --opt --user=$USER --password=$PASSWORD --databases $db > $OUTPUT/dbbackup-$db.sql
gzip $OUTPUT/dbbackup-$db.sql
fi
done
echo "Finished MySQL Backup";
echo "Backing Up WWW Dir";
rsync -P -ave 'ssh -p 22' root@$BACKUPSERVER:$WWWDIR/ $LOCALBACKUPFOLDER
echo "Completed Backup";
echo `date`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment