Backup all MySQL databases


/ Published in: Bash
Save to your folder(s)

Script to backup all MySQL databases and delete copies after X days


Copy this code and paste it in your HTML
  1. #! /bin/sh
  2.  
  3. # -------------------------------------
  4. # Script to do a full databases backup
  5. # and store it X days
  6. #
  7. # A Navalla Suiza 08/03/2010
  8. #
  9. # This script is freely distributed under the GPL
  10. # -------------------------------------
  11.  
  12. # Configuration variables
  13. # -------------------------------------
  14.  
  15. USER='MySQL-USER'
  16. PASS='MySQL-PASSWORD'
  17. HOST='localhost'
  18. BACKUPDIR='/var/backups/mysql'
  19.  
  20. # Script execution
  21. # -------------------------------------
  22.  
  23. data=`date +%Y%m%d`
  24.  
  25. for i in `mysql -u $USER -h $HOST -p$PASS -Bse 'show databases'`; do
  26. echo 'Processing '$i
  27.  
  28. mysqldump --add-drop-table -u$USER -p$PASS $i | gzip -9 > $BACKUPDIR/$data-$i.sql.gz
  29. done
  30.  
  31. find $BACKUPDIR -type f -mtime +7 -exec rm -f {} \;

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.