1

I would like to create a copy of my running Linux installation onto a USB 16 GB pen drive. The current system is a basic ATX PC used as a headless server on a remote location. I would like to have a backup option in case of a hard drive failure. Basically someone would shutdown the computer, plug in the USB containing the exact copy of the system and the system would boot and run off the USB drive for as long as needed, that is until I can get to the location with the new proper HDD replacement.

The current HDD is 120 GB. Used space is around 5 GB. So the questions are:
1. How to create a exact(bootable) copy of that, onto a smaller 16 GB usb drive?
2. How to copy everything back onto a bigger drive(i.e. 250 GB or 500 GB) when I do a new HDD install? Preferably expanding the file system back to the whole disk size, because the rest of the free space is used occasionally as time-lapse photography storage.

Here are some more details about the current disk configuration:

fdisk -l

Disk /dev/sda: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xac46573c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64       14594   116707328   8e  Linux LVM

Disk /dev/mapper/VolGroup-lv_root: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/VolGroup-lv_swap: 1979 MB, 1979711488 bytes
255 heads, 63 sectors/track, 240 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/VolGroup-lv_home: 63.8 GB, 63837306880 bytes
255 heads, 63 sectors/track, 7761 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

df -BG

Filesystem           1G-blocks      Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root
                           50G        3G       45G   5% /
tmpfs                       1G        0G        1G   0% /dev/shm
/dev/sda1                   1G        1G        1G  20% /boot
/dev/mapper/VolGroup-lv_home
                           59G        1G       55G   2% /home
2

Generic tool for low level work against hard drives

DISCLAIMER Playing with low-level tool may harm your system! Don't ask me for anything about potential damages you've made!

For this kind of jobs, I use a personal version of Debian-Live, built with all needed disk tools

    gsmartcontrol
    smartmontools
    partclone
    ntfs-3g
    lvm2
    mdadm

Backing-up

There is few steps to backing your machine up:

  • Copying partition structure For this, you could use any of parted, sfdisk, gparted or other cfdisk... Followed by mdadm and/or lvm2 tools.
  • Copying datas Copying datas could be done by following command: tar -cpC /sourcePath . | tar -xpC /destPath. For backing-up mounted partitions with sub-mount active, I use the following workaround (sample backing-up root directory /):

    # Debian-live is automatically mounted to /media/DEBIAN-LIVE and /media/persistance
    mkdir /media/persistance/root/Backup
    mount --bind / /mnt
    tar -zcpC /mnt . >/media/persistance/root/Backup/root.tgz
    umount /mnt
    
  • Make system bootable. This is more subtle: Assuming you've booted on Debian-Live you have to build your target structure, chroot into them, than run grub-install:

    # mount /dev/mapper/VolGroup-lv_root /mnt
    # mount dev/sda1 /mnt/boot
    # # /home is useless for installing grub
    # for bind in proc sys dev{,/pts};do mount --bind /$bind /mnt/$bind;done
    # chroot /mnt
     # /usr/share/mdadm/mkconf >/etc/mdadm/mdadm.conf
     # update-initramfs -u -k all
     # grub-install
     # exit
    # umount /mnt/{dev{/pts,},sys,proc,}
    

Than (In the hope all work fine) I reboot.

Alternative multi-os using partclone

There is an overall solution for backing whole partitions, but as you store byte-by-byte each partitions, you need a destination bigger or with same size as your source: (This could be stored on small USB key anyway).

The basis is quite same, built your own Debian live with all needed tools, but partclone.

Than to store a whole multi-boot disk sharing WinXP and Linux on same disk (sample):

mkdir ReleventDirectoryName
cd $_
SOURCE=sdA 
dd if=/dev/$SOURCE count=1 | gzip >bblock.gz
sfdisk -d /dev/$SOURCE >sfdisk.dump
partclone.ntfs -c -s /dev/${SOURCE}1 | xz >part1-ntfs.pclone.xz
partclone.ext4 -c -s /dev/${SOURCE}2 | xz >part2-ext4.pclone.xz
partclone.ext4 -c -s /dev/${SOURCE}5 | xz >part5-ext4.pclone.xz

and so on...

to restore, you only have to invers the process:

cd ReleventDirectoryName
DEST=sdA
zcat bblock.gz | dd of=/dev/$DEST
sfdisk /dev/$DEST <sfdisk.dump
partclone.ntfs -r -o /dev/${DEST}1 < <(xzcat part1-ntfs.pclone.xz)
partclone.ext4 -r -o /dev/${DEST}2 < <(xzcat part2-ext4.pclone.xz)
partclone.ext4 -r -o /dev/${DEST}5 < <(xzcat part5-ext4.pclone.xz)

Than... reboot... !

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.