Tutorial :In linux, how do I create/restore an image snapshot of my entire drive?



Question:

I'm too spoiled by Windows utilities that take a digital snapshot of your entire drive, which you can then restore from in the event of a drive crash. (e.g. like Time Machine for Mac OS X).

Is there a similar way of doing this in Linux?


Solution:1

The tool you want (which most closely resembles Time Machine) is called rsnapshot. Unlike normal backup tools it copies only things that have changed, and it allows you easily to travel to many points in time. You can tell it how much disk space it is allowed to have, and it adjusts the number of snapshots kept to stay within that limit. A very nice tool.


Solution:2

PartImage runs from the command line or as a text GUI. It has one important advantage over dd:

Partimage will only copy data from the used portions of the partition. For speed and efficiency, free blocks are not written to the image file. This is unlike the 'dd' command, which also copies empty blocks.


Solution:3

You can simply do "dd if=/dev/sda of=/path/to/target/backup" if you know you have the room where you are trying to put it. You could also do "dd if=/dev/sda | bzip2 > /path/to/target/backup" to compress on-the-fly. This might take some time though.

Or, you could do "tar --preserve-permissions -cjf backup.tar.bz2 /what/to/backup" as a user that has all the neccessary read permissions. Take your pick.

Addendum: I recently bought a new laptop, preinstalled with Vista. I figured I wanted to preserve vista somewhere, in case I wanted to try it out at some point, so the first time I booted the computer (with a linux livecd), I did this:

dd if=/dev/sda | ssh 10.0.0.1 "bzip2 > ~/vistadrive.bz2"  

This was on a fast local network, of course. Otherwise it would have been more prudent to compress before transmission:

dd if=/dev/sda | bzip2 | ssh 10.0.0.1 "cat > ~/vistadrive.bz2"  

Hope you find some of this useful.

Edit: Oops. Changed bb to dd...


Solution:4

I use a two-fold strategy: #1 is LVM snapshots and number two is rsnapshot, which I use to make point-in-time backups to a dedicated external hard disk drive. The external hard disk drive is placed so that I can easily yank it on my way out the door should something bad happen. I don't (yet) use an off-site mechanism other than periodically burning a snapshot to a series of encrypted DVD discs and shipping them to a friend's place in another city for storage.

I highly recommend rsnapshot because of the way it does the snapshots, using hard links for things that haven't changed. If your home directory goes bonkers, you can just take an rsnapshot copy and tarpipe or rsync it back to your new home partition after reinstalling or recovering from drive failure.


Solution:5

Several. There's the old fashioned dd(1), which is kind of arcane; there's dump(8) and restore(8); there are several open-source products around, like Amanda; and if you want, you can install ZFS and use a cron script to take periodic snapshots, giving you something effectively identical to Time Machine (see Tim Foster's weblog.)

Wikipedia has an article with a list of many options.


Solution:6

If you're wanting a file-level backup like time machine, then I highly recommend rsnapshot as Norman suggested.

If you're wanting a full image backup and restore type solution, like norton ghost, then I've found CloneZilla to work quite well. I boot the live CD, and then use another machine as an SSH server to backup/restore the image(s) to/from.

(I even managed to get the live version working via PXE boot for an old tablet laptop I had with no CD drive and no booting of USB, but it was a lot of fiddling.)

And you can use it for windows machines too.


Solution:7

I always worried about using dd when the drives were differently sized. Or had different numbers of heads/sectors/tracks/etc.

dd is very useful when snapshotting and reflashing the same drive. Especially USB drives or windows partitions. But when going from a 500Meg to a 1500Meg drive, I get concerned.

Besides, when my drive fails, I like to be able to use it as an excuse to upgrade to a newer OS.


All that said, good old tar does a nice job! GNU-tar includes diff-tar-against-files, only-update-files-that-have-changed, and only-archive-files-newer-than-date options.

(I should mention: I did run into a bug a few years back that required me to download and install a newer version of GNU-tar. (Something to do with very long pathnames and multiple volumes.) No big deal. Just be aware that's always an option.)

You should probably exclude /proc, /sys, and perhaps /var/log/lastlog or /mnt. (I usually back up to an external drive mounted under /mnt. Backing up the backup -- not so good!)

Including /dev can be worthwhile. gnu-tar will handle devices properly (mknod). Though you probably won't use the backed up /dev during a restore. (Occasionally its nice to see what permissions or links you had set up before.)

Also, you might want to dump fdisk & rpm info before backing up. E.g.

/sbin/sfdisk -l /dev/sda > /....../info_sda  rpm -qa | sort > /...../info_rpms  rpm -qa --qf "%{ARCH} \t %{NAME}-%{VERSION}-%{RELEASE}\n" | sort -k2,2 > /........./info_rpms_arch  

As well as mounting any "optional" user-mounted filesystems.


Solution:8

Here's the full steps for another method, works for moving from one hard drive to another, and even cloning a machine if the hardware is similar.

First, create an image of a good working system. Do this as root.

# cd /   # tar cpzf hostname.tgz / --exclude=hostname.tgz --exclude=proc --exclude=lost+found --exclude=mnt --exclude=sys --exclude=home --exclude=usr/src  

Backup the kernel images

# mount /boot  # cd /boot  # tar cpzf boot.tgz *  

Boot the machine you want the image deployed on with a live cd/gentoo cd. Mount that hard drive and partition it appropriately:

# fdisk /dev/hda  /dev/hda1  /boot  /dev/hda2  swap  /dev/hda3  /tmp  /dev/hda4  /  

Add filesystems

# mke2fs /dev/hda1  # mkswap /dev/hda2  # mke2fs -j /dev/hda3  # mke2fs -j /dev/hda4  

Mount drives:

# mount /dev/hda4 /mnt/new_root  # mkdir /mnt/new_root/tmp  # mkdir /mnt/new_root/boot  # mkdir /mnt/new_root/proc  # mkdir /mnt/new_root/sys  # mount /dev/hda3 /mnt/new_root/tmp  # mount /dev/hda1 /mnt/new_root/boot  # swapon /dev/hda2  # mount -t proc proc /mnt/new_root/proc  

Copy over the hostname.tgz file onto this machine.

Extract it into the root of the new machine /mnt/new_root

# tar -xzf hostname.tgz  

Chroot into the new environment.

# chroot /mnt/new_root /bin/bash  # env-update && source /etc/profile  

Verify /boot/grub/grub.conf and /etc/fstab

Setup grub on the hard drive:

# grep -v rootfs /proc/mounts > /etc/mtab  # grub-install /dev/hda  

Exit and umount drives, reboot the machine.


Solution:9

You can have snapshots on Linux using LVM, the logical volume manager.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »