How to Backup and Restore your Home directory


If you don’t keep your data separate from your operating system, i.e. on another volume, then when you “distro hop” you need to be able to backup you Home folder before you install your new OS. This is the problem I’ve recently faced when moving from XFCE based Xubuntu to CrunchBang.

If you have a second hard disk (internal or external) then this is relatively easy, alternatively assuming you have space you cold use Gparted to create an additional partition on your hard drive and use this, although as it is all on the same physical disk it is more risky and therefore not my preference.

Whilst you can use rsync, TAR is the traditional way of backing up in Linux / Unix / MAC OSX and ships as standard with all Linux distros.

For my purposes I have used the rather helpful instructions on Ubuntu forums http://ubuntuforums.org/archive/index.php/t-93193.html provided by James Fryman. Although dated, they still work. I’ve distilled the forum post below

To Backup

In terminal type:

tar cf <name of archive>.tar.gz /home/<username>

In addition, when doing a tar backup, it’s also good to add the following flags : p & (z/j)

-p will preserve the original file permissions

-z will compress using gzip (medium cpu usage, but less space)

-j will compress using bzip2 (lots of cpu, even less space)

-v verbose output (optional)

So, a full backup would look something like this:

tar c(z/j)vf <name of archive>.tar.gz /home/<username>

In place of the .gz file name extension you could also use bz2.

For more info on tar command parameters, from Terminal enter man tar.

To Extract (restore the backup)

In terminal type:

tar x(z/j)vf <name of archive>.tar.gz

In place of the .gz file name extension you could also use bz2.

A Real Example

To backup my home directory to a tar file located in the root of a second hard disk I did the following:

a. Find out what disks and partitions exist:

sudo fdisk -l

This will display something similar to the screen-shot below

Change to the media directory

cd /media

b. Make a directory called disk1

sudo mkdir disk1

c. Mount my second hard disk (SDB1) to the disk1 directory

sudo mount /dev/sdb1 /media/disk

d. Change to the mounted disk

cd disk1

e. Enter the following to backup the home directory called myhomedirectory to a TAR file called mybackup.tar.gz:

tar czvf mybackup.tar.gz /home/myhomedirectory

this creates a file called mybackup.tar.gz in the /media/disk1 directory which is a backup of the home directory called myhomedirectory.

To restore, all I did under Crunchbang was to mount the second disk (as above) and from my Home directory enter:

tar xzvf mybackup.tar.gz

This restores the contents of the tar file into the root of my home directory. It includes the folder structure from root, but that is fine as it then allows me to copy over what I need for my new distro. I can then delete what is not needed. Furthermore, file permissions are maintained.

For more information on the tar file format go to http://en.wikipedia.org/wiki/Tar_%28file_format%29

=-=-=-=-=
Powered by Bilbo Blogger

7 thoughts on “How to Backup and Restore your Home directory

  1. Very good description. I too moved to #! and just run it on an external USB chip. It’s perfect.

    In terms of backup, I use REDO (found here- http://redobackup.org/)

    It’s so easy to use and avoids the dread and complexities of using Clonezilla.

  2. Maybe not the best way for large /home/ folders. I started a backup at 9pm and aborted at 9am because it was taking soo loong.

  3. That is the best weblog for anybody who desires to search out out about this topic. You understand so much its nearly laborious to argue with you (not that I really would need…HaHa). You positively put a brand new spin on a topic thats been written about for years. Great stuff, simply great!

Leave a comment