Advertisement
Azelphur

Untitled

Feb 13th, 2012
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.47 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # MySQL username
  4. USER="root"
  5. # MySQL Password
  6. PASS=""
  7.  
  8. # Location to store backups including trailing /
  9. STORAGE="/root/backups/sql/"
  10.  
  11. # List of databases to ignore. Ignore "Database" as it's the table header and not an actual database, and information_schema as it isn't lockable and is just metadata
  12. IGNORELIST=( "Database" "information_schema" "mysql" )
  13.  
  14. # You don't need to edit anything beyond this point
  15.  
  16. # Where is the mysql executable? (usually /usr/bin/mysql)
  17. MYSQL=($which mysql)
  18.  
  19. # Get a list of databases
  20. DATABASES=`echo "SHOW DATABASES;" | $MYSQL -u$USER -p$PASS`
  21.  
  22. function isignored() {
  23.     for IGNORED in "${IGNORELIST[@]}"
  24.     do
  25.         if [ "$1" == "$IGNORED" ]
  26.         then
  27.             return 0
  28.         fi
  29.     done
  30.     return 1
  31. }
  32.  
  33. echo "Backup started at `date`"
  34. # Loop through list of databases
  35. for DATABASE in $DATABASES
  36. do
  37.     # Check if we are ignoring this database
  38.     if ( isignored "$DATABASE" )
  39.     then
  40.         continue
  41.     fi
  42.  
  43.     TABLES=$(mysql -u$USER -p$PASS -e "SHOW TABLES IN $DATABASE;"  | sed -e 1d -e "s/^| (.*) |$/\0/")
  44.     echo "Backing up $DATABASE"
  45.     START=`date +%s`
  46.     for TABLE in $TABLES
  47.     do
  48.         # Create the directories if needed
  49.         mkdir -p "$STORAGE`date +%Y/%B/%d/`$DATABASE/"
  50.         FILE="$STORAGE`date +%Y/%B/%d/`$DATABASE/$TABLE.sql.bz2"
  51.  
  52.         # Perform the backup
  53.         mysqldump -u$USER -p$PASS $DATABASE $TABLE | nice -n 19 ionice -c 3 bzip2 -cq9 > $FILE
  54.     done
  55.     echo "Backed up $DATABASE in $((`date +%s`-$START)) seconds"
  56. done
  57. echo "Backup complete at `date`"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement