Skip to content

Instantly share code, notes, and snippets.

@lennartvdd
Last active August 29, 2015 14:13
Show Gist options
  • Save lennartvdd/2de465ac67779805ca50 to your computer and use it in GitHub Desktop.
Save lennartvdd/2de465ac67779805ca50 to your computer and use it in GitHub Desktop.
rsync backup over ssh
#!/bin/sh
# This script does backups to a remote server over ssh. the account running this script
# must have ssh access to the remote server using passwordless key authentication.
# perform:
# > ssh-keygen -q
# > ssh-copy-id [username]@[hostname]
# test with: `ssh [username]@[hostname]` & add script to cron
# > crontab -e
# #backup: every day @ 03:30 & log output.
# 30 3 * * * /root/backup.sh >> /tmp/system_backup.log 2>&1
# directories to backup (seperated by spaces)
BSRC="/etc/mysql/ /usr/local/mysql/data/BACKUP/"
# backup machine settings (data is stored under the BUSER's home dir in $BRFOLDER)
BSERVER=servername.domain
BUSER=backup_user
BRFOLDER=`hostname`
########################################################################
BACKUP_NAME=`date +%2u-%A`
BDEST=$BUSER@$BSERVER:$BRFOLDER/000-current
OPTS="-avzR --delete --backup --backup-dir=$BRFOLDER/$BACKUP_NAME"
export PATH=$PATH:/bin:/usr/bin:/usr/local/bin
# create backup root dir on remote server if it doesn't exist yet.
ssh $BUSER@$BSERVER "[ -d $BRFOLDER ] || mkdir -p $BRFOLDER"
# the following line clears the last weeks incremental directory
echo "Cleaning up backup: $BACKUP_NAME"
[ -d $HOME/emptydir ] || mkdir $HOME/emptydir
rsync --delete -a $HOME/emptydir/ $BUSER@$BSERVER:$BRFOLDER/$BACKUP_NAME
rmdir $HOME/emptydir
echo "Performing Backup: rsync $OPTS $BSRC $BDEST"
# now the actual transfer
rsync $OPTS $BSRC $BDEST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment