Skip to content

Instantly share code, notes, and snippets.

@domtalbot
Created April 5, 2016 07:41
Show Gist options
  • Save domtalbot/8c3cc643688cbe6fd5fe130990f80b68 to your computer and use it in GitHub Desktop.
Save domtalbot/8c3cc643688cbe6fd5fe130990f80b68 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Original script courtesy of Sonia Hamilton
# http://blog.snowfrog.net/2005/11/16/backup-multiple-databases-into-separate-files/
# Modified by QWeb Ltd to:
# - work more securely on Plesk servers
# - keep existing backups for 2 days
# http://www.qweb.co.uk/
# backup each mysql db into a different file, rather than one big file
# as with --all-databases - will make restores easier
# Plesk renames root to admin
USER="admin"
# Plesk stores the admin password here
PASSWORD="`cat /etc/psa/.psa.shadow`"
# mkdir this folder if it doesn't yet exist
OUTPUTDIR="/home/database-backups"
MYSQLDUMP="/usr/bin/mysqldump"
MYSQL="/usr/bin/mysql"
# clean up older backups (save space)
rm "$OUTPUTDIR/*bak2" > /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
# maintain backups for 2 days to prevent complete loss if the server dies during this backup process, for example
mv "$OUTPUTDIR/$db.bak" "$OUTPUTDIR/$db.bak2"
$MYSQLDUMP --force --opt --user=$USER --password=$PASSWORD \
--databases $db > "$OUTPUTDIR/$db.bak"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment