HOWTO: Backup your data to an external usb hard drive with rsync

Linux has a wide range of applications to perform backups, some of them very powerful. In this howto, I’m going to introduce you to rsync, a powerful yet simple command line application. With rsync we can perform exact copies of directories, partitions or whatever, mantaining the file permissions, soft links, and all. So, you have an exact copy. Running rsync daily allows us to mantain a synchronized copy of a partition to an external hard drive. If something wrong happens, we will have the latest copy of that partition saved in an external device. In my case, I always have an exact synchronized copy of my /home directory. If I have to upgrade my OS, I install the latest OS and then just copy the files to my /home directory without losing permissions, obviously if I mantain the same username.

Backing up with rsync


rsync -av – -stats /sourcedirectory /targetdirectory

With this command, I’ll have an exact copy of sourcedirectory in targetdirectory. Take into account if you delete some file in sourcedirectory and it has been backed up previously it will not be deletec in the targetdirectory in future backups. To delete it, just type:

rsync -av – -delete – -stats /sourcedirectory /targetdirectory

If you want to exclude a directory from being backed up, then use the – -exclude command like follows:

rsync -av – -exclude=directorytoexclude – -delete – -stats /sourcedirectory /targetdirectory

where directorytoexclude is the directory’s name. It is not necessary to be the whole path.

About jordilin

I'm a Senior Software Engineer with an MSc in Telecommunications Engineering. I've got a range of interests including Linux, a very powerful and limitless operating system, travelling, gastronomy and technology.
This entry was posted in HOWTOs. Bookmark the permalink.

8 Responses to HOWTO: Backup your data to an external usb hard drive with rsync

  1. ste says:

    Gracias! Very useful and fast.

  2. Eamann O Ruairc says:

    Hola Jordi!

    Desgraciadament no functiona per me:

    eamann@eamann-laptop:~$ rsync -av – -stats /home /lacie
    rsync: -: unknown option
    rsync error: syntax or usage error (code 1) at main.c(1108)

    (Fa mults anys vaig pasar una temporada a Barcelona i vaig apprendre una mica de catala.)

    I’m a newbie and perhaps I haven’t given the proper address for the directories?


  3. jordilin says:

    Hi Eamann,
    There must be a space between stats and sourcedirectory, i.e. rsync -av –stats /source /target (all with spaces in between the commands)

  4. jordilin says:

    In the last post of mine is – -stats. I don’t know why it’s typed wrong when you write two dashes 😉

  5. Dilawar says:

    Reblogged this on Dilawar's Notes and commented:
    A nice post on how to backup your data in linux using rsync

  6. Pingback: Moving Local Files with Rsync - Cup of Voodoo

  7. Pingback: Moving Local Files with Rsync | Daniel W. Robert

  8. Pingback: Moving Local Files with Rsync – DWR.IO

Leave a Reply

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

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

Google photo

You are commenting using your Google 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