Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- #
- # The backup script for GretaThemes VPS
- # - Automatic backup all databases in format $database-$Y-$m-$d.sql.gz
- # - Send backups to Dropbox
- # - Send customers.csv from Theme Updater to Dropbox
- #
- # Written by: Tran Ngoc Tuan Anh <rilwis@gmail.com>
- # Last update: October 1st, 2016
- # Exit when a command fails
- set -o errexit
- # Fail fast and be aware of exit codes
- set -eo pipefail
- # The main function
- main() {
- local folder=/path/to/backup/folder/
- local today=$(date +%F)
- local user="YOUR MYSQL USERNAME"
- local password="YOUR MYSQL PASSWORD"
- local dump="mysqldump --skip-extended-insert --force"
- local file
- local database
- echo "Start backup databases";
- # Get list of all databases
- local databases=$(echo "SHOW databases" | mysql -u $user -p"$password")
- for database in $databases; do
- # Skip if this is the default built-in MySQL database
- if [ "$database" = "Database" ] || [ "$database" = "information_schema" ] || [ "$database" = "mysql" ] || [ "$database" = "performance_schema" ] || [ "$database" = "sys" ]
- then
- continue
- fi
- # Backup
- echo "Backup the database $database";
- file="$folder/$database-$today.sql.gz"
- $dump -u $user -p"$password" $database | gzip -9 > $file
- # Upload to Dropbox
- echo "Upload the database $database to Dropbox";
- upload $file $today/$database-$today.sql.gz
- done
- echo "Cleaning up the backup folder"
- cleanup $folder
- echo "Notify admin that the back up is completed"
- notify $today youremail@domain.com
- }
- # Upload a file to Dropbox
- #
- # @param $1 File to upload
- # @param $2 File to store on Dropbox
- upload() {
- /path/to/dropbox_uploader.sh upload $1 $2
- }
- # Cleanup the backup folder, remove backups older than 30 days
- cleanup() {
- find "$1"/* -mtime +30 -exec rm {} \;
- }
- # Notify admin that the back up is completed
- #
- # @param $1 Date
- # @param $2 Email address
- notify() {
- echo "Backup for server on $1 is completed." | mail $2 -s "Daily server backup on $1"
- }
- # Call the main function
- main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement