Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Backup all MySQL databases and upload them to Amazon S3
- # Note-This version needs debugged.
- # Check loading credentials
- # Check negated default table exclusions
- main() {
- ##### User Settings #####
- # MySQL Database Server
- dbserver=127.0.0.1
- # Backup temp folder
- bakdir=/tmp/
- # Compression program command (xz/gzip/lrzip/zip)
- comp_pgm='xz -9e'
- comp_ext=xz
- # S3 Bucket (s3tools.org 'apt-get install s3cmd' remaining settings in ~/.s3cfg)
- bucket=backupbucket
- ##### No changes below this line #####
- # Load username and password
- #dbuser=backupuser
- #dbpassword=backupuserpassword
- if [[ -r $passfile ]] ; then
- . ~/.mysqlbackup.conf
- else
- echo "ERROR - Password file not found or not readable."
- echo "Create file ~/.mysqlbackup.conf"
- echo " with dbuser= and dbpassword= lines."
- exit 1
- fi
- # Loop through every MySQL database
- for tbl in `mysql -h $dbserver -u $user -p$password -Bse 'show databases;' `
- do
- # Skip the internal databases
- if ![ "$tbl" == "information_schema" ] && ![ "$tbl" == "performance_schema" ] && ![ "$tbl" == "mysql" ] && ![ "$tbl" == "test" ]
- echo "Backing up database: $tbl"
- mysql_backup "$tbl"
- # Upload the database to S3
- s3cmd --encrypt put $filename s3://$bucket
- rm $filename
- done
- }
- # Create a compressed copy of $1 MySQL database
- function mysql_backup {
- # Build backup filename
- filename=$bakdir$1-`hostname`-`/bin/date +\%Y\%m\%d`.sql.$comp_ext
- # Backup the database
- mysqldump
- --no-create-db \
- --opt \
- --add-drop-table \
- -Q \
- -h $dbserver \
- -u $dbuser \
- -p$dbpassword \
- $1 | \
- $comp_pgm > \
- $filename
- }
- main "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement