Advertisement
Guest User

Untitled

a guest
Jun 10th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Backup all MySQL databases and upload them to Amazon S3
  4. # Note-This version needs debugged.
  5. # Check loading credentials
  6. # Check negated default table exclusions
  7.  
  8. main() {
  9. ##### User Settings #####
  10. # MySQL Database Server
  11. dbserver=127.0.0.1
  12. # Backup temp folder
  13. bakdir=/tmp/
  14. # Compression program command (xz/gzip/lrzip/zip)
  15. comp_pgm='xz -9e'
  16. comp_ext=xz
  17. # S3 Bucket (s3tools.org 'apt-get install s3cmd' remaining settings in ~/.s3cfg)
  18. bucket=backupbucket
  19.  
  20. ##### No changes below this line #####
  21.  
  22. # Load username and password
  23. #dbuser=backupuser
  24. #dbpassword=backupuserpassword
  25.  
  26. if [[ -r $passfile ]] ; then
  27. . ~/.mysqlbackup.conf
  28. else
  29. echo "ERROR - Password file not found or not readable."
  30. echo "Create file ~/.mysqlbackup.conf"
  31. echo " with dbuser= and dbpassword= lines."
  32. exit 1
  33. fi
  34.  
  35. # Loop through every MySQL database
  36. for tbl in `mysql -h $dbserver -u $user -p$password -Bse 'show databases;' `
  37. do
  38. # Skip the internal databases
  39. if ![ "$tbl" == "information_schema" ] && ![ "$tbl" == "performance_schema" ] && ![ "$tbl" == "mysql" ] && ![ "$tbl" == "test" ]
  40.  
  41. echo "Backing up database: $tbl"
  42. mysql_backup "$tbl"
  43. # Upload the database to S3
  44. s3cmd --encrypt put $filename s3://$bucket
  45. rm $filename
  46. done
  47. }
  48.  
  49. # Create a compressed copy of $1 MySQL database
  50. function mysql_backup {
  51.  
  52. # Build backup filename
  53. filename=$bakdir$1-`hostname`-`/bin/date +\%Y\%m\%d`.sql.$comp_ext
  54.  
  55. # Backup the database
  56. mysqldump
  57. --no-create-db \
  58. --opt \
  59. --add-drop-table \
  60. -Q \
  61. -h $dbserver \
  62. -u $dbuser \
  63. -p$dbpassword \
  64. $1 | \
  65. $comp_pgm > \
  66. $filename
  67. }
  68.  
  69. main "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement