Skip to main content

A script to mirror directories locally via rsync

Nothing really fancy here! I just wrote a simple rsync script which can be used to do a mirror of a set of directories from one place to the other. I use this to do incremental mirror of local Debian repositories from my machine, to my external USB hard-disk, so that I can give it to somebody who needs the entire Debian repo.

Of course, it’s application is not limited to copying just a single directory but, you can have an entire system backup by giving space separated list of directories to mirror. This script creates a directory corresponding to the basename of each of the source directories. For example, if the source directory is /opt/mystuff/projects and the destination root directory(named as RTARGET in the script) is /media/disk/backup, the script will create a directory called ‘projects’ under /media/disk/backup and mirrors the contents from /opt/mystuff/projects to /media/disk/backup/projects.

Note: This script comes with absolutely no warranty of any kind. You may use it at your own risk :-) Remember to change the values of SOURCES and RTARGET according to your need. You can save the shell script below into a file called and place it in /usr/local/sbin directory. Don’t forget to set its executable bits (chmod 755).

# -- secure local backup using rsync.

export PATH=/usr/local/bin:/usr/local/sbin:$PATH

# Directories to backup. Separate with a space.
#Exclude trailing slash!
SOURCES="/opt/mystuff1 /opt/mystuff2"

# Directory to backup to on the remote machine.
#This is where your backup(s) will be stored
# Exclude trailing slash!

echo "Verifying Sources..."
for source in $SOURCES; do
    echo "Checking $source..."
    if [ ! -x "$source" ]; then
        echo "Error with $source!"
        echo "Directory either does not exist, or \
        you do not have proper permissions."
        exit 2

echo "Sources verified. Running rsync..."
RBAKOPTS="--delete -zavl"

# Now, do current week's incremental backup
for source in $SOURCES; do
    mkdir -p $RTARGET/$(basename $source)
    rsync $RBAKOPTS $source/ $RTARGET/$(basename $source)/

exit 0


Popular posts from this blog

Using USB CDMA modem to setup a home/private WiFi network

The idea here is to give a ‘how-to’ on setting up a a home WiFi network using mobile, CDMA based USB modem, hoping that bandwidth that you obtain from this service is satisfactory :-) Hardware Requirements: Linux PC/Notebook/Netbook Wifi Access point connected to ethernet port of the Linux box CDMA-1x USB Modem (Device used: Reliance M880) Software/OS Requirements: Linux Operating System with “usbserial” and “cdc_acm” device driver modules “pppd”, “wvdial”, “iptables”, and “dnsmasq” applications and their dependencies installed on the System Setup: The steps followed in setting up this CDMA based connectivity can be split into 3 steps, as follows. Configuring kernel modules for auto-loading at bootup Configuring “wvdial” application for dialup / ppp connection Initializing / starting the CDMA modem connection Setting up “iptables” for NAT forwarding Setting up “dnsmasq” for resolving DNS and as DHCP server Configuring kernel modules for auto-loading at bootup Open

Debian Squeeze and Mac OSX Lion - Dual boot

With Mac OSX Snow Leopard, things were much simpler to install and run a Linux operating system just by using rEFIt boot manager utility, on my Mac Mini(4,1 Mid-2010 model). With the introduction of Lion, Apple has introduced a hidden, OS recovery partition. In total, there will be 3 partitions for Mac OSX alone, for Lion. They are ESP (EFI System Partition), Mac OSX installation partition and, the OS recovery partition. Also, as per the GUID Partition Table(GPT) which Mac uses, there can be maximum of 4 primary partitions, with no scope for Extended partitions like in typical MSDOS Partition Table setup. It is also recommended to follow GPT when using Mac. With all these constraints, I'm actually left with one partition where I can install and run GNU/Linux (in this case, Debian Squeeze). I didn't try resizing an existing Mac OSX installation partition and creating an Ext4 partition for Linux. Rather, I choose to go for fresh installation. You can try with the resizing metho

Using git over proxy

I was trying to clone Android repository at office when this problem of “Connection refused” started coming when running the ‘repo’ tool. On subsequent googling, I got this really useful link (thanks to Emil Sit) which explained how git can be used over http proxy for those git servers which don’t allow http method as an alternate/bypass to git protocol (As it looks is the case with Android git repository). Ok, in short, here is what I have done; Typed in the below lines (quoted) into a shell script called gitproxy and put it in $(HOME)/bin directory. Of course, its executable bits has to be set with chmod a+x $(HOME)/bin/gitproxy. My $(HOME)/bin directory is already part of PATH so that I can access my custom scripts easily. #!/bin/sh # Use socat to proxy git through an HTTP CONNECT firewall. # Useful if you are trying to clone git:// from inside a company. # Requires that the proxy allows CONNECT to port 9418. # # Save this file as gitproxy somewhere in your path # (e.