Skip to content

Instantly share code, notes, and snippets.

@AlexPashley
Created July 3, 2013 20:30
Show Gist options
  • Save AlexPashley/5922527 to your computer and use it in GitHub Desktop.
Save AlexPashley/5922527 to your computer and use it in GitHub Desktop.
SHELL: Simple Shell Script to create database backups
# Backup mysql databases into seperate files
USER="mysql-username"
PASSWORD="mysql-password"
OUTPUTDIR="/path/to/db/backup/dir"
MYSQLDUMP="/usr/bin/mysqldump"
MYSQL="/usr/bin/mysql"
NOW=$(date +"%m-%d-%Y")
MSG="Just to let you know that a full backup of db has been made onto the VPS.Kind Regards"
# Remove previous backups
rm "$OUTPUTDIR/*bak" > /dev/null 2>&1
# get a list of databases
databases=`$MYSQL --user=$USER --password=$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
# dump each database in turn
for db in $databases; do
echo $db
$MYSQLDUMP --force --opt --user=$USER --password=$PASSWORD --databases $db > "$OUTPUTDIR/$db.bak"
done
# emaili reminder of backup
echo $MSG | mail -s "VPS Server Backup - Backup Created on $NOW" [email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment