Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. #
  3. # The backup script for GretaThemes VPS
  4. # - Automatic backup all databases in format $database-$Y-$m-$d.sql.gz
  5. # - Send backups to Dropbox
  6. # - Send customers.csv from Theme Updater to Dropbox
  7. #
  8. # Written by: Tran Ngoc Tuan Anh <rilwis@gmail.com>
  9. # Last update: October 1st, 2016
  10.  
  11. # Exit when a command fails
  12. set -o errexit
  13.  
  14. # Fail fast and be aware of exit codes
  15. set -eo pipefail
  16.  
  17. # The main function
  18. main() {
  19. local folder=/path/to/backup/folder/
  20. local today=$(date +%F)
  21. local user="YOUR MYSQL USERNAME"
  22. local password="YOUR MYSQL PASSWORD"
  23. local dump="mysqldump --skip-extended-insert --force"
  24. local file
  25. local database
  26.  
  27. echo "Start backup databases";
  28.  
  29. # Get list of all databases
  30. local databases=$(echo "SHOW databases" | mysql -u $user -p"$password")
  31.  
  32. for database in $databases; do
  33.  
  34. # Skip if this is the default built-in MySQL database
  35. if [ "$database" = "Database" ] || [ "$database" = "information_schema" ] || [ "$database" = "mysql" ] || [ "$database" = "performance_schema" ] || [ "$database" = "sys" ]
  36. then
  37. continue
  38. fi
  39.  
  40. # Backup
  41. echo "Backup the database $database";
  42. file="$folder/$database-$today.sql.gz"
  43. $dump -u $user -p"$password" $database | gzip -9 > $file
  44.  
  45. # Upload to Dropbox
  46. echo "Upload the database $database to Dropbox";
  47. upload $file $today/$database-$today.sql.gz
  48.  
  49. done
  50.  
  51. echo "Cleaning up the backup folder"
  52. cleanup $folder
  53.  
  54. echo "Notify admin that the back up is completed"
  55. notify $today youremail@domain.com
  56. }
  57.  
  58. # Upload a file to Dropbox
  59. #
  60. # @param $1 File to upload
  61. # @param $2 File to store on Dropbox
  62. upload() {
  63. /path/to/dropbox_uploader.sh upload $1 $2
  64. }
  65.  
  66. # Cleanup the backup folder, remove backups older than 30 days
  67. cleanup() {
  68. find "$1"/* -mtime +30 -exec rm {} \;
  69. }
  70.  
  71. # Notify admin that the back up is completed
  72. #
  73. # @param $1 Date
  74. # @param $2 Email address
  75. notify() {
  76. echo "Backup for server on $1 is completed." | mail $2 -s "Daily server backup on $1"
  77. }
  78.  
  79. # Call the main function
  80. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement