Friday, September 18, 2009

Faster Backup Under Linux Just using LiveCD

There are a lot of tools to make a full image of your hard drive and save it to an external storage or in a remote host, but the true is we can create a image of our hard drive just with a linux live CD and the dd command.

BACKUP

1. Just boot with a linux live CD, if you lack of a CD-ROM Drive, then you can create a USB bootable stick with a linux image and boot from it.
2. Mount you external storage, for instance mount -t ext3 /dev/sdb1 /media/images. NOTE: just the destination drive have to be mounted, not the source drive.
3. Once you are running the Linux OS, if you found that your hard drive is /dev/sda for instance, then run:
#dd if=/dev/sda of=/media/images/disksda.img
#cd /media/images/
#bzip2 disksda.img
<- if you want to compress it.
Now if you want is to create a image just from a partition then change the if=/dev/sda by if=/dev/sdaX, where X is the number of the partition, for instance:
#dd if=/dev/sda1 of=/media/images/disksda1.img
#cd /media/images
#bzip2 disksda1.img

Sometimes we want to move this image directly to other remote host with linux and ssh running on it and a user called "user", the we can do this:
#dd if=/dev/sda of=/dev/stdout bs=1M | bzip2 | ssh user@IPofRemoteHost "cat - > disksda.img.bz2"
this read the /dev/sda hard drive, the send this data to the standar output and compress it in the RAM, take the standart ouptut compressed and using the ssh write it in the remote host in a file called disksda.img.bz2

RESTORE,

1. Just boot with a linux live CD, if you lack of a CD-ROM Drive, thenyou can create a USB bootable stick with a linux image and boot from it.
2. Mount you external storage, for instance mount -t ext3 /dev/sdb1 /media/images. NOTE: just the source drive have to be mounted, not the destination drive.
3. Once you are running the Linux OS, found your hard drive name and restore on it, for instance /dev/sda.

to restore from the remote host do this:
#ssh user@remotehost "cat - > disksda.img.bz2" | bzip2 -dc | dd if=/dev/stdin of=/dev/sda bs=1M
to restore from the external storage:
#dd if=/media/images/disksda.img of=/dev/sda
or to restore the partiton image doing:
#dd if=/media/images/disksda1.img of=/dev/sda1
remember to uncommpress the bzip2 file first if it is compressed.

Sometimes the first hard drive is called /dev/sda, sometimes /dev/hda thats why you have to check how the linux call your hard drive.
To clone a hard drive into a second one, just us of=/dev/sdb or whatever the name of the second drive is, without mount any drive.
Partition table is created, MBR is created, but the external drive in size have to be equal or bigger than the drive to be copied.
If you have a question about it, please first read this:
LinuxQuestion.org, dd for backups.


Reference:
Backup HOWTO
My delusional dream
linuxquestions.org