Data Networking Blog
Blog for Admins

Backup with rsync

Standard

There are multiple ways to backup your systems and data and the one you choose to implement depends on many different factors such as the OS. Today we are looking at rsync utility to backup important data. I am not going to perform a full backup of my system as it is easier for me to recreate my VPS than to transfer giga bytes of data over internet. I will only backup important configuration files so I can bring back my services to a working condition in case of  a disaster. To achieve this I will do the following:

  • Install rsync
  • Create a text file with all files/folders I want backed up
  • Invoke rsync through cron to backup all files/folders from the text file

rsync – Remote Sync is an amazing utility that allows you to copy files/folders to remote hosts over any remote shell like ssh. You can also use it as a copy command to copy files/folders on the same host. It allows you to perform a full or differential backup, compress data transmitted over internet, retain file ownership information, include symlinks and much more. It will take you a good hour if you go through the complete manual page. I am going to include some options that I believe are relevant to most scenarios.

If rsync is not already installed then a simple yum command can be used to install it.

Like I said this utility has a lot of options that I will not need so I am going to mention a few useful ones  here.

-r Recurse through a directory so we can backup sub directories

-t Preserve modification times. This is important as rsync by default uses time stamps and file sizes to decide if a file needs to be backed up since it does differential backups.

-z Compress data during transfer which can be very useful over slow connections.

-o preserve owner information

-g preserve group information

-h Make the numbers more human readable. For example, if stats shows the files size in bytes, you can use this option to make it display this information in KB/MB/GB depending on the actual size.

-l Copy symlinks

-p Preserve file/folder permissions.

-D Copy devices information and recreate on the destination.

—-log-file=FILE Log information in FILE.

—log-file-format=FORMAT Define a format for the log file.

—stats Displays information about the files transferred such as number of files, file size etc.

—progress Display progress of the transfer which is useful if transferring large files.

—files-from=FILE Read the file/folders to be backup from a file.

Well that is a lot of options to remember. Don’t worry you can replace most of these with a single option.

-a Archive is equal to provide -rlptgoD (no -H,-A,-X)

 

Alright now we know enough about rsync to start using it. I will first show you a simple copy to local system so let’s create a text file that has paths to all files/folders I need backed up.

Add paths to files/folders you want backed up.

Create a folder to use as a backup destination.

Now let’s run rsync with some options specified above. First let’s just type rsync and hit enter. You will see a lot of output but the important thing to remember is the usage.

So you can add options and then provide source followed by destination. Keeping that in mind look at the example below:

Notice the / between –files-from and destination directive. This is important as without defining a source you will get errors from rsync about usage. You may be wondering why provide a source when I am already providing that through the text file. In this case the rsync utility takes all sources defined in the text file as being relative to the / root directory. So in the actual text file you could omit the preceding / and it will still work.

After running this command if you ls /root/test-backup you will find that rsync has copied the folder structure as below:

Nice so we now have a backup on the same machine. To backup to a remote system simply use the following:

rsync will create the folders if they are missing on the destination system so you really don’t need to create one manually.

Now let’s schedule this command through cron and we are done.

And schedule the backup to run every day at midnight.

Again I am backing up to local system but you can change it to what ever you like. Hope this help 🙂

February 6, 2015 Backup, Linux, Systems, Unix Jd

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Font Size
Decrease Size Default Size Increase Size
Select Skin
Select Underlay Background
Select Overlay Background
Scheme Switcher Toggle