Automatic Rsync Backup Script
#!/bin/bash
############################################
# Automatic Rsync Backup Script #
# Created by: Aevum Decessus #
# Contact: aevum(dot)decessus(at)gmail.com #
# Licensed under GPL version 2 #
# http://www.gnu.org/licenses/gpl-2.0.txt #
############################################
################################################################################
# This scripts uses the configured variables below to simply, and #
# create snapshot backups of any directory on a remote ssh server, to a local #
# machine, and can be easily automated through cron-jobs. #
################################################################################
################################################################################
# To Run Backups of Multiple Directories: #
# Add the following variable definitions: #
# server_b #
# name_b #
# copy the entire script, from line 40 to 67, and replace all instances #
# of server_a with server_b, and name_a, with name_b. You can add as many #
# backups as you need this way, or you can just customize the script #
# twice, once for each backup, and simply run the 2 separate scripts then. #
################################################################################
#SSH usernamen and server
server=user@server.com
#Directory to recursively backup for backup - trailing slash is a must!
server_a=/SERVER/DIR/TO/BACKUP/
#Date string to be used in directory for previous day's backup
c=$(date +%Y_%m_%d -d -1day)
#name to be used in all backups.
name_a=beta
#Date string to be used for removing old backups (7 days in this case)
#Simply change the number at the end to change how long to keep backups
keep=$(date +%Y_%m_%d -d -7day)
#Directory to store backups in - will be created if it does not exist
dir="/home/USER/backup"
#Checks to see if the first backup has been run
if [ -d $dir/backup.$name_a ]
then
#makes directory for yesterday's backup, and moves it into there
echo "$dir/backup.$name_a exists"
echo "moving yesterday's $name_a backup"
mkdir -p $dir/$name_a.$c
mv $dir/backup.$name_a/* $dir/$name_a.$c
echo "getting today's $name_a backup"
#Uses rsync and the link-dest option to onle download changed files, otherwise links them
rsync -a --delete -e ssh --link-dest=$dir/$name_a.$c/ $server:$server_a $dir/backup.$name_a
echo "$name_a backup complete"
else
#Creates first backup directory, this will be used from now on.
echo "$dir/backup.$name_a does not exist. creating now"
mkdir -p $dir/backup.$name_a
echo "getting first $name_a backup"
rsync -a --delete -e ssh $server:$server_a $dir/backup.$name_a
echo "$name_a backup complete"
fi
#Repeat the first loop for the second backup, this entire if-then can be removed if there is only one backup.
echo "All backups complete"
#if a 7-day old backup exists, remove it.(change the keep date to change this interval
if [ -e $dir/$name_a.$keep ]
then
echo "deleting $name_a $keep backup"
rm -rf $dir/$name_a.$keep
echo "$name_a $keep backup removed"
fi