Saturday, February 1, 2014

[Linux] HOW TO: Backup your linux installation with TAR.


Hello again!

Today, I will show you how to backup your Linux installation using tar, step by step.

I am not responsible for any data damage!

(Tested on Linux MINT 16 - CINNAMON 64bit edition)

Backup

To backup system partition, we will use tar. Tar can store all files on disk into one file. Another good thing is that tar can preserve file permissions as well as user/group ownership. To save space, we will use gzip comprimation.
It is good to know that you don't have to make backup from live CD. But it is necessary to restore it from CD, if you can't boot your system. Don't put output file on the same partition! In this example, I save my backup file to second disk, connected to /media/Backup-Disk/.
Here is the command:

tar -zcpf /media/Backup-Disk/fullbackup.tar.gz --directory=/ --exclude=proc --exclude=dev --exclude=sys --exclude=mnt --exclude=media .
 tar parameters explained -> click


We exclude dev, proc and sys directories because system will restore them automatically.

Also, we exclude media and mnt directory because there is nothing to backup, and you can have connected device here to save your backup.

That's it, your backup file is now ready :)

Restore

If your system still works, you don't have to boot liveCD, to restore your backup. But remember that restoring backup won't remove any files! Only overwrite same files, and copy missing files. In my opinion, this only messes-up your system, and I don't really see any point in this.

Easiest way to restore is boot the live CD or USB stick, formate partition and restore backup. With this you get nice clean system as you had when you backed up.

1) First connect system partition you want to restore. For example I will use /dev/sda6 where is my linux partition located.

sudo mount /dev/sda6 /media/mounted-partition-to-restore/

2) Format partition
!!!WARNING!!!: This will erase all data on the partition! So be careful about it!


sudo rm -rf /media/mounted-partition-to-restore/*

3) Restore from backup.
NOTE: This will overwrite all files with same names on this partition!


sudo tar -xpzvf /path/to/backup.tar.gz -C /media/mounted-partition-to-restore --numeric-owner
tar parameters explained -> click


4) Create back missing directories in restored partition

cd /media/mounted-partition-to-restore
sudo mkdir proc
sudo mkdir sys
sudo mkdir dev
sudo mkdir mnt
sudo mkdir media
sudo mkdir /var/tmp
sudo chmod a+rwxt /tmp

5) If you need to restore grub, follow my "how to" here:

6 comments :

  1. I'm pretty sure you'd usually like to exclude /tmp from a backup , right ?

    ReplyDelete
    Replies
    1. Yes, you can exlude it without having any problems :)

      Delete
  2. Does Tar backup/restore symlinks?

    ReplyDelete
    Replies
    1. Yes, it should. At least I didn't have a problem with that.

      Delete
  3. is there a way the backup archive can be spanned over multiple files to allow burning to DVD's?

    ReplyDelete
    Replies
    1. When I had clean installation the system, whole image had about 1.2 Gigabytes.

      If you have more software installed, and you get to high numbers, you can split the archive.

      Just add " | split --bytes=4500MB - backup.tar.gz." after the command.
      So it should look something like:

      sudo tar -xpzvf /path/to/backup.tar.gz -C /media/mounted-partition-to-restore --numeric-owner | split --bytes=4500MB - backup.tar.gz.

      Hope this help.

      Delete