humpty promotes - Linux - Lubuntu Tips

Lubuntu linux Tips How to Clone your system to a backup drive

Clone your root partition to a backup stick.
Make it bootable.
Use it as a backup image.
Use it as a recovery stick.
or Use it as an alternative system version
(e.g for a different family member).

Linux is basically a file system that starts from 'root' i.e '/'.
You can backup your whole root partition by copying all the files to another drive and making it bootable.
But you have to avoid problems like special attributes, permissions and folders you don't need.
The basic command is 'rsync', which copies files that are different and deletes those that no longer exist.

First you need to install a simple boot loader "extlinux" using your regular package manager (e.g synaptic).
An example extlinux config file is found at the end of this article. Put extlinux.conf in the directory /boot/extlinux/extlinux.conf.

Below are two scripts, one to do the copying, and the other to make the backup drive bootable.

You do need one more file, the excludes.txt, containing a list of temporary directories that copy-root ignores.


Whether you want the last three lines to be ignored is up to you. Generally I don't need to backup unneccesary large files.


# Makes a copy of the root partition files excluding all temporary directories
# copy-root <to_partition> e.g copy-root sda1

if [ "$1" == "" ]; then
 echo please specify device e.g sdb1; exit;
if [ `whoami` != root ]; then
    echo Please run as root or sudo; exit
mkdir /mnt/$1
umount /dev/$1
mount /dev/$1 /mnt/$1

START=$(date +%s)
rsync -vaAxX /* /mnt/$1/ --delete  --exclude-from=excludes.txt
FINISH=$(date +%s)
echo "took $(( ($FINISH-$START) / 60 )) min:$(( ($FINISH-$START) % 60 )) sec."

Make sure you know the target device name eg. sdc1. You can find out by typing 'mount'.

Depending on how fast your drive is, the first copy should take between 5 to 30 mins.

The first time you use it will be longer. subsequent times will be much quicker because rsync won't need to copy files already there. This can be as fast as 30 secs.

Use the prime-boot script if you intend to boot the backup drive  (which you will do if you want a complete restore) and only if you have installed extlinux (usually already installed in Lubuntu).

# prime-boot <to_partition> e.g prime-boot sda1

# Readies a partition for booting
# 1. priming attributes
# 2. copying over a valid mbr (optional)
# 3. copying /etc/fstab and opening to edit
# 4. copying /boot/extlinux and opening to edit extlinux.conf
# 5. displaying the disk uuid
# 6. displaying instructions to run extlinux

if [ "$1" == "" ]; then
 echo please specify device e.g sdb1; exit;
if [ `whoami` != root ]; then
    echo Please run as root or sudo; exit
mkdir /mnt/$1 2>/dev/null
umount /dev/$1 2>/dev/null
mount /dev/$1 /mnt/$1
chmod 755 /mnt/$1
chown 999:999 /mnt/$1
echo not modifying MBR..
#cat mbr.bin > /dev/sdx

cp -f --parents /etc/fstab /mnt/$1/

chattr -i /mnt/$1/boot/extlinux/ldlinux.sys 2>/dev/null
rm -r /mnt/$1/boot/extlinux/* 2>/dev/null
cp -f --parents /boot/extlinux/* /mnt/$1/

#----- ls -l /dev/disk/by-uuid | grep $1 | cut -d\  -f9-12
blkid | grep $1 | cut -d\  -f1-2

leafpad /mnt/$1/etc/fstab &
leafpad /mnt/$1/boot/extlinux/extlinux.conf &

cd /mnt/$1/boot/extlinux

echo if no MBR, e.g "cat mbr.bin > /dev/sdb"
echo edit fstab and extlinux.conf,  change the uuid as above.
echo then cd /mnt/$1/boot/extlinux and  do extlinux -i .

# to rm ldlinux.sys , first chattr -i ldlinux.sys

Most drives will usually already have an MBR so that part is commented out.

(If you installed extlinux into the wrong directory and need to delete ldlinux.sys, first chattr -i ldlinux.sys.)

After changing the UUIDs in /etc/fstab and /mnt/<dev>/boot/extlinux.conf, you can update EXTlinux; i.e
cd /mnt/sdb1/boot/extlinux
extlinux -i .

(see installing extlinux)

Example Output
Backup to empty card.

Toshiba 8G microSD in
Buffalo stick-type card reader.

     mkfs.ext4 -L "" /dev/sdd1

./copy-root sdd1

Upon 2nd copy, it has little to do
and takes just 22 secs.

sdd1 has used up less than 3G
 for the backup.

./prime-boot  sdd1

(using blkid cmd to get uuid)

[ extlinux -i . ]
updates the VBR

Example fstab

# /etc/fstab: static file system information.
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    nodev,noexec,nosuid 0       0
# / was on /dev/sda3 during installation
UUID=92c948c4-fcf7-4a98-86cb-a752678c5a40 /               ext4    errors=remount-ro 0       1

Example extlinux.conf

LABEL linux
  SAY Now booting the kernel using EXTLINUX...
  KERNEL /boot/vmlinuz-3.2.0-23-generic-pae
  APPEND ro root=UUID=92c948c4-fcf7-4a98-86cb-a752678c5a40 initrd=/boot/initrd.img-3.2.0-23-generic-pae

After a while you may want archive your backup disks.

More Lubuntu Tips

Leading Cloud Surveillance, Recording and Storage service; IP camera live viewing

Leading Enterprise Cloud IT Service; cloud file server, FTP Hosting, Online Storage, Backup and Sharing

Powered by, a division of DriveHQ, the leading Cloud IT and Cloud Surveillance Service provider since 2003.