Skip to content

Instantly share code, notes, and snippets.

@holachek
Created January 16, 2012 12:48
Show Gist options
  • Save holachek/1620729 to your computer and use it in GitHub Desktop.
Save holachek/1620729 to your computer and use it in GitHub Desktop.
rysnc incrementally copies specified local folder and mySQL database to a remote server
#!/bin/bash
#: Title : rsync backup to remote server
#: Date : 2012-01-16
#: Author : mh
#: Version : 1.0
#: Description : Incremental rsync backup of mySQL database and given path
# to a remote server. Run this script on the machine that
# has the files you want to backup to a remote server.
#: Options : Set the local/remote directory, backup folder name
############### Options
## the absolute path on the local machine you want to backup
local_backup_folder=/srv
## the remote backup server, format: username@hostname or username@IP
[email protected]
# [email protected]
## the remote backup folder path
remote_backup_folder=/backup
## the most recent backup is hardlinked to this directory
most_recent_backup_link=/backup/current
## the backup name (ex: 2012-01-16~14:00:00)
backup_folder_name=`date '+%Y-%m-%d~%T'`
## MySQL username
mysql_username=root
############### Start the program by getting current filesize of remote directory
current_remote_dir_size=`ssh $remote_backup_server du -hs /backup | awk '{ print $1 }'`
############### Backup MySQL database
echo "Backing up MySQL database into $local_backup_folder/mysql-${backup_folder_name}.sql.gz"
mysqldump -q -u${mysql_username} --all-databases | bar | gzip > $local_backup_folder/mysql-${backup_folder_name}.sql.gz
############### Backup local_backup_folder to remote_backup_folder
echo "Rsync-ing $local_backup_folder directory to backup server $remote_backup_folder"
rsync -ahe ssh --delete --progress --link-dest=${most_recent_backup_link}/ ${local_backup_folder} ${remote_backup_server}:${remote_backup_folder}/backup-${backup_folder_name}/ > ${local_backup_folder}/backup_logs/${backup_folder_name}.log
############### Create most current hand link
echo "Creating most current hard link on backup server $most_recent_backup_link"
ssh $remote_backup_server rm -rf ${most_recent_backup_link}
ssh $remote_backup_server cp -alv ${remote_backup_folder}/backup-${backup_folder_name}/ ${most_recent_backup_link}
############### Cleanup local MySQL database file
rm ${local_backup_folder}/mysql-${backup_folder_name}.sql.gz
############### Copy backup log file to remote server
echo "Copy log file to backup server"
scp ${local_backup_folder}/backup_logs/${backup_folder_name}.log ${remote_backup_server}:${remote_backup_folder}/logs/${backup_folder_name}.log
############### Hooray it worked! Now calculate the size of the backup
echo "Successfully backed up ${local_backup_folder} directory"
remote_dir_size=`ssh $remote_backup_server du -hs /backup | awk '{ print $1 }'`
delta_size=`echo $(( ${remote_dir_size/M/} - ${current_remote_dir_size/M/} ))`
echo "The ${remote_backup_folder} directory now is ${remote_dir_size} (originally ${current_remote_dir_size}, a ${delta_size}M difference)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment