MySQL Backup Script


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



Copy this code and paste it in your HTML
  1. #!/bin/bash
  2.  
  3. MyUSER=""
  4. MyPASS=""
  5. MyHOST=""
  6.  
  7. MYSQL="$(which mysql)"
  8. MYSQLDUMP="$(which mysqldump)"
  9. CHOWN="$(which chown)"
  10. CHMOD="$(which chmod)"
  11. GZIP="$(which gzip)"
  12.  
  13. DEST="/var/backups"
  14. MBD="$DEST/mysql"
  15. HOST="$(hostname)"
  16. NOW="$(date +"%d-%m-%Y")"
  17.  
  18. # File to store current backup file
  19. FILE=""
  20. # Store list of databases
  21. DBS=""
  22.  
  23. # DO NOT BACKUP these databases
  24. IGGY="example"
  25.  
  26. [ ! -d $MBD ] && mkdir -p $MBD || :
  27.  
  28. # Only root can access it!
  29. $CHOWN 0.0 -R $DEST
  30. $CHMOD 0600 $DEST
  31.  
  32. # Get all database list first
  33. DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"
  34.  
  35. for db in $DBS
  36. do
  37. skipdb=-1
  38. if [ "$IGGY" != "" ];
  39. then
  40. for i in $IGGY
  41. do
  42. [ "$db" == "$i" ] && skipdb=1 || :
  43. done
  44. fi
  45.  
  46. if [ "$skipdb" == "-1" ] ; then
  47. FILE="$MBD/$db.$HOST.$NOW.gz"
  48. # do all inone job in pipe,
  49. # connect to mysql using mysqldump for select mysql database
  50. # and pipe it out to gz file in backup dir :)
  51. $MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE
  52. fi
  53. done
  54.  
  55. find $MBD -type f -mtime +14 -exec rm {} \;

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.