Jun
7
2011

Backup your Linux System

When was the last time you had a copy of your important data? How often do you backup your data?

Last week, while creating a swap partition, I accidentally deleted some of my important documents. Even though I perform a weekly backup of my system, I was not able to retrieve those documents as I did not have any back-up copy of those documents on my computer. Fortunately enough, I had the sense to store my documents in the cloud (in Dropbox ), so no harm done there.

We learn our lesson only when we get the burn. So after going through this post, I hope you will be able to back up your system and get used to it.

 

Types of Backup

a. Full – back up all the files of the target

b. Incremental – backup all the files that have changed since the last backup.

c. Differential - backup the data changed since last full backup

Some notable Backup Utilities available: TAR, rsync, grync, BackupPc.

In this article, I have used TAR ( a CLI backup utility ) and sbackup( a GUI backup utility). Since this article is written on the basis of Ubuntu distribution, this article is based on the GNU version of TAR. It also does not mean that this is only applicable for Ubuntu distributions.

For more information on TAR, refer our previous stone http://www.fortystones.com/40-linux-shell-commands-beginners/ . I highly recommend you to go through manual page of TAR if you have any doubt.

 

Full Backup

Let us backup our root folder

 

# tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz --exclude=/proc
--exclude=/lost+found --exclude=/sys --exclude=/mnt
--exclude=/media --exclude=/dev /

/proc and /sys are virtual filesystems that provide windows into variables of the running kernel, so we exclude them.

/dev is a tmpfs whose contents are created and deleted dynamically by udev, so we exclude it.

Similary, we exclude /lost+found, /mnt, /media

 

If compression is important to you, use bzip2.

 

# tar -cvpjf backup.tar.bz2 --exclude=/backup.tar.bz2 --exclude=/proc
 --exclude=/lost+found --exclude=/sys --exclude=/mnt
 --exclude=/media --exclude=/dev /

 

Incremental Backups

It’s a wise step to create a full backup and then continue with Incremental Backups.

 

Step 1. Create a full backup

# tar -cvpzf backup1.tar.gz --listed-incremental=/var/log/test.snar
/home/rabi/

( backup all the contents of /home/rabi in backup.tar.gz./var/log/test.snar file will be used in our incremental backup. This is a level 0 backup )

 

Copy this snapshot file /var/log/test.snar for differential backup.

 

# cp /var/log/test.snar /path/to/test_new.snar


Once you have made a full backup, you don’t need to perform this step again for Incremental Backups.

 

Step 2.  Incremental Backup

 

# tar -cvpzf backup2.tar.gz --listed-incremental=/var/log/test.snar
/home/rabi/

( this is a level 1 backup ).

 

You can associate tar with find command to backup the files which have changed since the last 24 hrs.

 

# tar cvf /media/myUSB `find / -mtime -1 -type f -print`

( For more information on find , refer http://www.fortystones.com/master-find-command-linux/ ).

 

or, you can use the option –newer

 

# tar -cvf /media/myUSB/backup.tar.gz  --newer '10 May 2011' /usr/src

 

Differential Backups

We will be using the snapshot file that we had created before ( in Incremental Backup ) to perform Differential Backups.

 

# tar -cvpzf backup3.tar.gz --listed-incremental=/path/to/test_new.snar
/home/rabi

 

Splitting the Archive

Now, if you need to split the archive into various files of your desired size, use split command. You can split the archive during creation by removing the f option of tar ( i.e. redirecting the output of the archive to standar output ) and then using the pipe command .

For e.g. to backup my home folder and split the files in the size of 4000 Mb during the archive creation,

 

$ tar -cvpz --exclude=/home/rabi/backup_home.tar.gz /home/rabi | split -d
-b 4000m - /home/rabi/backup_home.tar.gz.

If you have an archive file and you want to split it into several files of your desired size,

 

$ split -d -b 4000m /path/to/backup.tar.gz /where/tosplit/backup.tar.gz.

To add Date to your splitted files, use

 

$ split -d -b 4000m /path/to/backup.tar.gz /where/tosplit/backup-`date '
+%d-%B-%Y'`.tar.gz.

 

Backup Using sbackup ( GUI backup utility )

Installation

From terminal,

 

# sudo apt-get install sbackup

You can also install sbackup from Ubuntu Software Center or Synaptic Package Manager

 

Screenshots

Subscribe to fortystones.
Follow @fortystones on Twitter.
Get updated from our Facebook Fanpage.

Share

Related Posts

About the Author: Rabi C Shah

Rabi C Shah, pursuing his under-graduate program with a major in Information Technology in Indian Institute of Information Technology Allahabad (IIIT-A), is a passionate programmer. A Photoshop enthusiast, he is interested in Linux, C and C++. Rabi has completed many mini college projects in java and php. He is the Linux guy of our fortystones team.

7 Comments + Add Comment

  • If you only want to keep some documents, pictures and videos Dropbox is a great way of securing them. I guess it’s not a good alternative if you’ve got tons of gigabytes you want to back up.

    • Dropbox is awesome for backing up important documents. I usually store my works and documents in Dropbox as it has a decent storage space (2 Gb).. but you really need a REAL Backup for all othr files

  • I recently started using SpiderOak for backups. I can confirm it works well on Ubuntu 10.04
    Good detailed article laying out options though.

  • I recommend Duplicity – encrypted, incremental backups.

  • [...] In my previous post on Backup your Linux System, a brief introduction on Backup and the three types of Backup were explained. Once we have backups of our important documents, we should be able to restore those backups. Ergo, in this post, I will explain how to restore backups using the GNU version of ‘tar’ Backup Utility. [...]

  • [...] simple shell script which on execution will back up your linux system. If you are not familiar with Backup or Restore, I highly recommend you go through our previous stones on Backup your Linux System, [...]

  • [...] on the series of linux backup stones, I give you Tips for Backup and Restore. These are short but essential tips on Backup and Restore in [...]

Leave a comment

*