Backup with rsync

I decided to finally get into the habit of backing up my home folder regularly. Till now, I just copied everything to an external hard drive every couple of weeks, which isn’t the smartest way. Therefore, I wanted to do things in a somewhat ‘smarter’ manner.

Sure, I knew about rsync but I had never used it…

I have an external hard disk with a backup folder where I made a sub-folder called SYNC, for rsync to use. I decided to mount it in /media/rsync as root, in order to avoid the crazy address path when I do it as a regular user. I created /media/rsync and mounted the external drive (sdd1 is the device name in my case):

mount /dev/sdd1 /media/rsync

So, now I have the following path where I am going to sync my home folder:

/media/rsync/backup/SYNC

OK, time to synchronize from /home/username to /media/rsync/backup/SYNC:

rsync -av --exclude=".*" /home/username /media/rsync/backup/SYNC

In the example above:

  • -a | archive mode, allows copying files recursively, preserves symbolic links, file permissions, user/group ownership and timestamps
  • -v | verbose
  • --exclude=".* | excludes files or folders starting with a dot. I did not want to sync hidden files and folders

As a start, this basic synchronization works just fine. However, in the process of my work, I move/delete files or folders, rename others, etc. I want these to be removed accordingly from the SYNC directory, upon syncing. Therefore, add the --delete option:

rsync -av --delete --exclude=".*" /home/username /media/rsync/backup/SYNC

It turned out that some of the sub-folders had hidden files, left from the time I used KDE that stored information about the folder itself, such as custom icon, etc. These became a problem, when I wanted rsync to synchronize deletion of folders. Therefore, I added the --delete-excluded option:

rsync -av --delete --delete-excluded --exclude=".*" /home/username /media/rsync/backup/SYNC

Synchronizing over ssh makes things convenient. Let’s say I have a PC and a laptop in the same local network. In both computers I have the same username. Therefore:

rsync -avh --delete --progress --delete-excluded --exclude=".*" -e ssh /home/username/* username@XXX.XXX.XXX.XXX:/home/username

In the example above:

  • -h | human readable output
  • –progress | display the files transferred and remaining time
  • XXX.XXX.XXX.XXX | IP address of the remote computer

The man page of rsync provides extensive information. Other hints are from here and here. Read the links’ contents and decide for yourself what options you need.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s