Advertisement
Azelphur

Untitled

Nov 16th, 2011
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.37 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # MySQL username
  4. USER="root"
  5. # MySQL Password
  6. PASS="password"
  7.  
  8. # Location to store backups including trailing /
  9. STORAGE="/home/yourusername/backups/"
  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" )
  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.     # Create the directories if needed
  44.     mkdir -p $STORAGE`date +%Y/%B/%d`
  45.  
  46.     # Perform the backup
  47.     echo "Backing up $DATABASE to $STORAGE`date +%Y/%B/%d`/$DATABASE.sql.gz"
  48.     START=`date +%s`
  49.     mysqldump -u$USER -p$PASS $DATABASE | ionice -c 3 gzip -c > $STORAGE`date +%Y/%B/%d`/$DATABASE.sql.gz
  50.     echo "Backed up $DATABASE to $STORAGE`date +%Y/%B/%d`/$DATABASE.sql.gz in $((`date +%s`-$START)) seconds"
  51. done
  52. echo "Backup complete at `date`"
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement