A script for easy local or remote backups using rsync

Here is a very simple script I wrote which a number of folks have written to say they find useful. It performs incremental backups to a local disk or to a remote computer somewhere else in the world. Also useful when travelling.

It uses the rsync utility, which is very fast. When used in remote mode, it encrypts all traffic to prevent rapscallion interception.Only the files which have been modified since the last backup are sent, further speeding things up. To run the script, just cut an paste it into a text file called 'go-backup'. Then from the command prompt, type './go-backup'.

Or you can set it to run automatically at certain times. I did this for myself to backup files on my home machine to to a server running in another country. To do this I just created a two line cron job (there are nice menu systems to help with this if you do not know how) to run the thing daily, weekly, monthly, every three months, and every six months backing things up unattended and securely to the remote server.

Enjoy.

 

#!/bin/bash

# **** go-backup ****
#
# A simple incremental script to backup your system to a local or remote system
# Copyright (c) peter.ca. All rights reserved.

# This script uses the very reliable and fast rsync utility
# to backup just a few files or an entire system
# it is very fast after it has been run once, as
# it backs up only those files or directories
# which have changed since this script was last executed.
#
# to run the script, open a terminal window, and type:
# ./go-backup

clear
 
echo "Please select the type of backup you wish to perform:"
# In this example I have set up a weekly, monthly, once every three months, and twice a year
# backup system. The backup is done once a week to a disk called 1week, once a month to a
# disk called '1month', and so on. It is easy to automate this so that it operates without
# human interaction, but for this example I set things so that a human can choose the type
# of backup she wishes from a simple menu

OPTIONS="1week 1month 3months 6months Quit"
select opt in $OPTIONS; do
    if [ $opt = "Quit" ]; then
        echo done
        exit
    elif [ $opt = "1week" ]; then
        echo "PERFORMING $opt BACKUP to /$opt/"
    elif [ $opt = "1month" ]; then
        echo "PERFORMING $opt BACKUP to /$opt/"
    elif [ $opt = "3months" ]; then
        echo "PERFORMING $opt BACKUP to /$opt/"
    elif [ $opt = "6months" ]; then
        echo "PERFORMING $opt BACKUP to /$opt/"

    fi
#
# this will backup to a local disk or set of disks
# comment it out and uncomment the section below
# to perform your backups to a remote computer
# somewhere else in the world
    rsync -arvluzR \
        /home/some-directory \
        /home/another-directory \
        /home/and-another-direcory \
        /home/and-so-on \
        /some-directory/some-file \
        /some-directory/another-file \
        /some-directory/and-so-on \
        /$opt/
#
# uncomment this to backup to a remote computer system
# somewhere else in the world
# the backup is automatically done over an encrypted channel
# to stop evil people from viewing your data
# to backup to a remote machine. You will be prompted for your password
# on the remote computer if you or your administrator has not set up
# secure no-password public keys for you
# rsync -arvluzR -e "ssh -l your-login-name-on-the-remote-machine -p 22" \
#        /home/some-directory \
#        /home/another-directory \
#        /home/and-another-direcory \
#        /home/and-so-on \
#        /some-directory/some-file \
#        /some-directory/another-file \
#        /some-directory/and-so-on \
#         login-name@10.0.10.100:/$opt/

    echo "========= done ========"
    echo " "
    echo " "
    echo "setting date for /$opt"
    touch /$opt
    echo "backup written to:"
    ls -lh /$opt
    echo "done"
done