CyberJack

Mysql Backup Script

Apr 12th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.77 KB | None | 0 0
  1. #!/bin/bash
  2. #-------------------------------------------------------------------------------
  3. # MySQL Database backupscript.
  4. #
  5. # Author: CJ
  6. # Licence: http://opensource.org/licenses/MIT MIT License
  7. # CopyRight: (c) 2013 - CJ
  8. #
  9. #-------------------------------------------------------------------------------
  10. # Installation
  11. #-------------------------------------------------------------------------------
  12. # 1. Place this script in the /usr/local/bin directory
  13. # 2. change the configuration in this script
  14. # 3. Add databases to the ignore list
  15. # 4. Add a cron job for daily backups
  16. #
  17. # Example cron:
  18. # 30 00 * * * /usr/local/bin/mysqlbackup 2>&1 >> /var/log/mysqlbackup.log
  19. #
  20. #-------------------------------------------------------------------------------
  21. # Configuration
  22. #-------------------------------------------------------------------------------
  23. # Backup destination
  24. BACKUPDIR="/db_backups"
  25.  
  26. # Howmany backups should be kept
  27. DAYS_TO_KEEP=7
  28.  
  29. # Mysql Information
  30. MUSER="";                       # MySQL username
  31. MPASS="";                       # MySQL password
  32. MHOST="localhost";              # MySQL hostname/ip
  33. MPORT="3306";                   # MySQL port
  34.  
  35. # DO NOT BACKUP these databases
  36. IGNOREDB="
  37. information_schema
  38. test
  39. "
  40. #-------------------------------------------------------------------------------
  41. # Linux bin paths
  42. # Change this if it can not be autodetected via which command
  43. #-------------------------------------------------------------------------------
  44. MYSQL="$(which mysql)"
  45. MYSQLDUMP="$(which mysqldump)"
  46.  
  47. #-------------------------------------------------------------------------------
  48. # Do not change anything below this line
  49. #-------------------------------------------------------------------------------
  50. # Get date in dd-mm-yyyy format
  51. NOW="$(date +"%d%m%Y")";
  52.  
  53. # Get the seven days ago date in dd-mm-yyyy format
  54. LAST="$(date -d "$DAYS_TO_KEEP days ago" +"%d%m%Y")";
  55.  
  56. # Create required directories
  57. if [ ! -e "$BACKUPDIR" ]; then
  58.     mkdir -p "$BACKUPDIR"
  59. fi
  60.  
  61. # Get all database list first
  62. DBS="$(mysql -u $MUSER -p$MPASS -h $MHOST -P $MPORT -Bse 'show databases')"
  63.  
  64. # Backup the database
  65. for db in $DBS
  66. do
  67.     DUMP="yes"
  68.     if [ "$IGNOREDB" != "" ]; then
  69.         for i in $IGNOREDB
  70.         do
  71.             if [ "$db" == "$i" ]; then
  72.                 DUMP="no";
  73.             fi
  74.         done
  75.     fi
  76.  
  77.     if [ "$DUMP" == "yes" ]; then
  78.         # Check for yesterday's backup and delete
  79.         LAST_FILE="$BACKUPDIR/$db""_$LAST.sql.bz2"
  80.         if [ -f "$LAST_FILE" ]; then
  81.             rm "$LAST_FILE";
  82.         fi
  83.        
  84.         # Create today's backup
  85.         FILE="$BACKUPDIR/$db""_$NOW.sql.bz2"
  86.         $MYSQLDUMP --add-drop-database --opt --lock-all-tables -u $MUSER -p$MPASS -h $MHOST -P $MPORT $db | bzip2 > $FILE
  87.     fi
  88. done
Advertisement
Add Comment
Please, Sign In to add comment