Skip to content

Instantly share code, notes, and snippets.

@Niksac
Last active September 7, 2016 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Niksac/9232d6e1b51db45b1eb7964aa790a1fe to your computer and use it in GitHub Desktop.
Save Niksac/9232d6e1b51db45b1eb7964aa790a1fe to your computer and use it in GitHub Desktop.
Bash script to incrementally backup a folder from a remote server via rsync over ssh.

Remote server incremental backup

This is a simple bash script to backup a folder on a remote server via rsync over ssh. Files are beinged transfered and stored incrementally (handlinks).
Mysql backup is included.
Backupped files will be removed after a specified number of days.

#target backup directory
BUDIR=/home/backups/examplecom
#remote directory
REMOTEDIR=/my/target/folder
#number of days backup files are being kept
BUDAYS=30
#mysql root pw on remote maschine
MYSQLROOTPW=SECRETSECRETSECRET
echo "starting backup process"
#cd into target bu directory
cd $BUDIR
NOW=`date +"%y%m%d_%k%M"`
#sql backup
echo "starting mysql backup"
ssh user@example.com "mysqldump -u root -p$MYSQLROOTPW --all-databases | gzip" > sql/$NOW.gz
echo "deleting old mysql backups"
find sql/ -name "*.gz" -type f -mtime +$BUDAYS -print -delete
#file backup
echo "starting file backup"
cd files
LATEST=`ls -td */ | head -1`
echo "latest latest found backup is $LATEST"
rsync -vaz --link-dest=../$LATEST user@example.com:$REMOTEDIR/ $NOW/
touch $NOW #touch backup target folder as he will not have -now- as mtime by default
echo "deleting old backups"
find -maxdepth 1 -type d -mtime +$BUDAYS -exec rm -r "{}" \;
echo "process finished"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment