my tutorials‎ > ‎

System backup


Backup with tar command

Tar command
There are many good ways to backup a Linux system (e.g. with Remastersys), but when you need to do it before you can learn how...

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/* / 

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.

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.


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
Noteif restoring to a different machine (which is probably not recommended) you'd need to ammend fstab, bootloader configuration (e.g. GRUB) and install the required drivers (which could be complicated), only then would you be able to boot into a working system.




RefractaSnapshot

With this app we can make a full system backup of our working box, or we can be creative and build a Debian install that we can then "snapshot" and clone onto other boxes.  It's smooth and easy to use.
download refractasnapshot and refractainstaller (base and gui of each) 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


1. 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!!!!


2. 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/*


3. Snap the snapshot
To make a snapshot of the installed system, once everything is sett up how you want it to be on the new install, 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 uncomment the line:
#mksq_opt="-comp xz"


Tip: to get the installer icon on the desktop when you boot the Live ISO on another machine....
  1. copy the .desktop file to the desktop i.e. cp /usr/shar/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