my tutorials‎ > ‎

System backup/clone


Backup with tar command


Tar command
There are several good ways to backup a Linux system, and I'll start with the first one I ever heard of, compression to archive using tar.

it's easy to make a tar file of all system and user files with something like
sudu tar {tar options} {location for tar file} {options, excludes} {dir to backup}
e.g.
sudo tar cvzpf /home/Backup_1207.tgz --same-owner --exclude=/home/Backup_1207.tgz --exclude=/home/error.log --exclude=/proc/* --exclude=/media/* --exclude=/dev/* --exclude=/mnt/* --exclude=/sys/* --exclude=/tmp/* --exclude=/var/cache/apt/archives/* --exclude=$HOME/Downloads/* / 

which I adapted from http://linuxclues.blogspot.com/2... 
I added the excludes for /var/cache/apt/archives/ and $HOME/Downloads
Also it's a good idea to exclude $HOME/.cache or clean chromium's or firefox's cache before backup (e.g with bleachbit).
May want to make the backup on another partition too.

The 'z' option adds gzip to the command so that the tar file is compressed (tar alone just makes one file of everything).
another option is 'u' which updates the tar file, making changes, not sending everything all over again.

Adding the switch --one-file-system will exclude files from other partitions, e.g. for backup of system only:
sudo tar cvzpf /home/Backup_1207.tgz --same-owner --one-file-system --exclude=/dev/* --exclude=/mnt/* --exclude=/media/* --exclude=/proc/* --exclude=/sys/* --exclude=/tmp/* --exclude=/var/cache/apt/archives/* /


Using scripts
1. backup system (name it bkpsys for example)

#!/bin/bash
sudo tar cvzpf /your_backup_location/Backupsys_$(date +"%y%m%d").tgz --same-owner --one-file-system --exclude=/proc/* --exclude=/media/* --exclude=/dev/* --exclude=/mnt/* --exclude=/sys/* --exclude=/tmp/* --exclude=/var/cache/apt/archives/* /

2. backup home (name it bkphome for example)

#!/bin/bash
tar cvzpf /your_backup_location/Backuphome_$(date +"%y%m%d").tgz --same-owner  --exclude=$HOME/Downloads/* $HOME

Here's my more precise one:
#!/bin/bash
tar cvzpf /media/BKP/zsysbkp/Backuphome_$(date +"%y%m%d").tgz --same-owner  --exclude=$HOME/Downloads/* --exclude=$HOME/.thumbnails/* --exclude=$HOME/.local/share/Trash/* --exclude=$HOME/.local/share/marble/maps/* --exclude=$HOME/Projects/kdenlive/.backup/* --exclude=$HOME/Projects/kdenlive/proxy/* --exclude=$HOME/Projects/kdenlive/thumbs/* $HOME

Using "$HOME" will backup the logged-in user, whereas "/home" will backup all users.  Omit "sudo" here so as not to backup the root $HOME!
the resulting tar file will be time-stamped with the day's date.  
set up a cron job to run them automatically.

test the tar ball integrity with:
gunzip -c B....tgz | tar t > /dev/null
a good archive will give no errors


Update option
If you leave out the date-time parameter so that the file name is the same with each run of the script, and you add the update option 'u', then you'll be able to keep a copy of the system in a tar archive, and just keep updating it.  Then you compress it and rename it with the time stamp.  
tar cvupf /your_backup_location/Backupsys.tar --same-owner --one-file-system --exclude=/proc/* --exclude=/media/* --exclude=/dev/* --exclude=/mnt/* --exclude=/sys/* --exclude=/tmp/* --exclude=/var/cache/apt/archives/* / \
| gzip >Backupsys_$(date +"%y%m%d").tgz

Note that tar will append files to the archive, not replace them inline.  So it may be better to create a new archive each time.


Restoring the system from the backup tar file
use sudo tar xvzf /home/Backup_1207.tgz -C /location-of-new-system

WARNING ! this command will overwrite every file on specified target with the files from the backup tar file!
  • in a Live CD environment (or another running system) copy the tgz archives to a partition (e.g. sda1)
  • make the system partition (e.g. sda5), home (e.g. sda6) and swap (if there's no other Linux system installed)
su
umount /dev/sda1
umount /dev/sda5
mkdir /mnt/newsys
mkdir /mnt/temp
mount /dev/sda1 /mnt/temp
mount /dev/sda5 /mnt/newsys

mkdir /mnt/home
mount /dev/sda6 /mnt/home
  • tar xvzf /mnt/temp/Backupsys_130212.tgz -C /mnt/newsys/
  • tar xvzf /mnt/temp/Backuphome_130212.tgz -C /mnt/home/
  • update GRUB to include the new system
Note: if restoring to a different machine, you'd need to amend the fstab file, so that the system finds its disks, and chroot into the system and run update-grub (or use Boot restore disk).



Rsync

Using this command could be very useful, as it has an update option, -u, which means that you can run it frequently and subsequent runs will send less data to the target file system than the initial sync.  It won't compress though, but you just need to afford some space for the backup.

rsync -auv --"exclude=/home/*" --"exclude=/proc/*" --"exclude=/lost+found/*" --"exclude=/dev/*" --"exclude=/mnt/*" --"exclude=/media/*" --"exclude=/sys/*" --"exclude=/tmp/*" --"exclude=/etc/fstab*"    /   /media/backupos 



Partclone
use this to image any disk, and restore the image to another box.  You need to use this tool from a live CD (e.g. gparted or your own with partclone installed).

sudo apt-get install partclone

to clone a partition with compression:
sudo partclone.ext4 -c -s /dev/sd-- | gzip -c > /backup-dir/image-name_$(date +"%y%m%d").pcl.gz

to restore the image to a partition (with great care!):
zcat /backup-dir/image_....pcl.gz | sudo partclone.ext4 -r -o /dev/sd--

As this tool images whole partitions, without any excludes, you better tidy up the disk especially in user home.
Note: the image can only be restored to a part equal or larger than the source part.  I tried the option -C to skip checking part space (used if we know that all the data could fit) but the result was a fail.


This is the GUI partclone tool.  see /how-to-use-clonezilla-tutorial/
They recommend tuxboot (find the .deb here) to install the iso to USB- clonezilla iso

Making an image
  • 1st screen hit enter
  • select lang, keybd, and start CZ
  • select device-image
  • select type of image dest - local-dev for a disk or USB device
  • select disk (to mount on CZ's /home/partimag/)
  • select dir of image
  • select beginner/expert
  • select savedisk/saveparts
  • enter image name
  • select source (disk/parts) to image
  • check and run
Restore Image
  • Go through like above, select image location as before
  • select restoredisk/restoreparts
  • select image to use
  • select the target disk/parts
  • check and run
note: restoring an image from e.g. a 20Gb hdd source onto a 50Gb target, might render the target as a 20Gb disk - gparted will be needed in that case to expand the part to fill the 50Gb.

Make a Live USB
Clonezilla can make a Live CD iso of your cloned image, see here
you need an image already
  • go through the steps above to restore an image, but after select beginner, 
  • select recovery-iso-zip
  • select image
  • select target for restoring from the iso! - change to "ask_user"
  • select lang, keymap, 
  • select iso or zip

Burn the USB
try unetbootin or tuxboot with iso file

or use the zip file with a command like:
unzip image.zip -d /media/usb/

and make it bootable with:
cd /media/usb/utils/linux
bash makeboot.sh /dev/sd..




RefractaSnapshot

With this script written by fsmithred, we can make a full system backup of our working box, and if we are creative we can build a Live Debian that we can run or install anywhere we like.  It's smooth and easy to use [although I've hit trouble booting the Live CD a few times].  It's certainly very user-friendly - it has a GUI version.

download refractasnapshot and refractainstaller (base and gui if needed) from http://sourceforge.net/
cd to download dir
sudo dpkg -i refracta*

If there are unmet dependencies, sort it out with sudo apt-get install -f
for a new system I found
refractasnapshot-base needed squashfs-tools live-boot live-config live-boot-initramfs-tools live-config-sysvinit 
refractainstaller needed grub-pc grub-legacy

Also install a newer version yad (21.0), necessary for the snapshot installer to run properly, from here http://code.google.com/p/yad/downloads/list
then cd, ./configure, make, sudo make install

or get the deb from here.. http://debs.slavino.sk/pool/main/y/yad/


1a.  Config file
change the refracta working directory out of /home (to another partition), using the config file:
sudo leafpad /etc/refractasnapshot.conf 
edit the exclude list (to reduce the final iso size)
sudo leafpad /usr/lib/refractasnapshot/snapshot_exclude.list
I added 
- /home/*/Downloads/*
- /home/*/.googleearth/*
- /home/*/.local/share/gvfs*
- /home/*/.local/share/clipit/*
- /home/*/.local/share/marble/maps/*
- /home/*/.local/share/recently-used.xbel
- /home/*/.local/share/user*
- /home/*/.opera/cache
- /home/*/.sword/*
- /home/*/kdenlive/*
- /home/*/Projects/kdenlive/.backup/*
- /home/*/Projects/kdenlive/proxy/*
- /home/*/Projects/kdenlive/thumbs/*


1b. Post-scripts
these run after the install and before the unmount and reboot.
This one is in process, a script to ask whether a root dir should be moved to another partition - such as /var, or /usr.

it needs to go in /usr/lib/refractainstaller/post-install/
the source (dir to move) and dest (partition) can be added to the script

or can try using yad to ask for which dir to move (I haven't had time yet to test it)

source_dir="`yad --text='Enter the source directory name to be moved e.g. \/var' --entry --undecorated --width=400 --height=200`"
and
target_partition="/dev/`yad --text='Enter the correct disk name for the partition to move to, e.g. sda6' --entry --undecorated --width=400 --height=200`"


2. Disk clean up
I highly recommend running Bleachbit as root to clean up the system before running a snapshot back-up.  
  • Get the latest bleachbit from http://bleachbit.blogspot.com/
  • install a dependency: sudo apt-get install python-simplejson
  • install the bleachbit deb
  • run sudo bleachbit
  • select some things like apt cache clean and Localization (unwanted languages - all those LC_MESSAGES files -set which ones to keep in preferences)
  • hit preview and then clean
  • run bleachbit as normal user to clean up the web browser cache and whatever
I cleaned and freed 1.72 Gb of disk space!!!!


3. Build the snapshot
To make a snapshot of the installed system, either to backup or clone it to another PC, run in terminal:
sudo refractasnapshot

running it with the gui seems pointless to me, really
needed disk space = disk size used by the system files + room for the iso file (in my case with the proper excludes, 5.5Gb + 1.9Gb)
you can enable xz compression in the conf file, but it takes longer to make it then. just un-comment the line:
#mksq_opt="-comp xz"


Tip: to get the installer icon on the desktop when you boot the Live ISO on another machine, but not present on the desktop on a new install....
  1. copy the .desktop file to the desktop i.e. cp /usr/share/applications/refractainstaller.desktop ~/Desktop/
  2. sudo leafpad /usr/lib/refractainstaller/installer_exclude.list
  3. add the exclusion - /home/*/Desktop/refractainstaller.desktop
  4. thus when the installer copies the file system to the target machine, the install icon will be excluded (so it only appears in the Live environment)

4. refracta2usb
to install the package, get from http://sourceforge.net/
install the dependencies
sudo apt-get install fuseiso hwinfo libhd16 pmount live-boot live-config mtools

This allows installing a Live system to USB from a Live CD iso or from a running live session, and can also update the file-system on USB stick later with a newer one. (Unetbootin can be used if preferred)
refracta2usb complains if the usb is mounted (and Unetbootin complains if its not mounted!)
Don't eject the device, unmount with 
pumount /dev/sdb1 
pumount /dev/sdb2 
etc

Tip: before updating the ISO image on the USB stick, first erase the previous files.  It can happen that the stick gets full.  
If you can't boot with the stick, try to reformat the USB stick as a partition mess-up will prevent it booting.


5. refractainstaller
It's a pretty straight forward affair once you boot into the Live system you put on the USB stick.  I get my partitions ready before hand with gparted from Live.

It's very nice to see your snapshot system getting cloned onto another box!



FSArchiver is worth looking at too http://www.fsarchiver.org/Main_Page

Comments