SHARE
TWEET

MySQL database backup script - QWeb Ltd

a guest Aug 15th, 2013 507 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. # Create the directory /home/database-backups
  4. # Then run this script on a daily cron to dump all databases
  5. # Should work on any Plesk server as-is.
  6.  
  7. # Original script courtesy of Sonia Hamilton
  8. # http://www.snowfrog.net/2005/11/16/backup-multiple-databases-into-separate-files/
  9.  
  10. # Modified by QWeb Ltd to:
  11. # - work more securely on Plesk servers
  12. # - keep existing backups for 2 days
  13. # http://www.qweb.co.uk/
  14.  
  15. # Plesk renames root to admin
  16. USER="admin"
  17.  
  18. # Plesk stores the admin password here
  19. PASSWORD="`cat /etc/psa/.psa.shadow`"
  20.  
  21. # mkdir this folder if it doesn't yet exist
  22. OUTPUTDIR="/home/database-backups"
  23.  
  24. MYSQLDUMP="/usr/bin/mysqldump"
  25. MYSQL="/usr/bin/mysql"
  26.  
  27. # Clean up older backups (save space)
  28. rm "$OUTPUTDIR/*bak2" > /dev/null 2>&1
  29.  
  30. # Get a list of databases
  31. databases=`$MYSQL --user=$USER --password=$PASSWORD \
  32.  -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
  33.  
  34. # Dump each database in turn
  35. for db in $databases; do
  36.     # maintain backups for 2 days to prevent complete loss if server dies during backup
  37.     mv "$OUTPUTDIR/$db.bak" "$OUTPUTDIR/$db.bak2"
  38.  
  39.     $MYSQLDUMP --force --opt --user=$USER --password=$PASSWORD \
  40.      --databases $db > "$OUTPUTDIR/$db.bak"
  41. done
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top