Saturday, January 05, 2013

Backup and Restore in Ubuntu

There are various strategies for a backup depending on the method that suits your needs. Lastly the “best backup policy” will be the one that is tested and works as per your needs.

METHOD-1 (Manual)
Here we will see how to backup the data, and applications installed from the repositories of Ubuntu.

Step-1 Backup of List of All Applications
Assume if for some reason your system is not able to boot and you dont have any choice other than reinstalling the operating system. There is least possibility for anyone to remember the whole lot of packages and applications that are installed on the system. In that case there is a quick way to store a list of all installed applications and packages in a text file. This text file can be used to install all your packages automatically. This method will help you save an incrdible amount of time, especially when you need to repeat the same task multiple number of times.

So, first you have to retrieve all your installed packages to a list. You can do so by redirecting the output to a file, example – applist.txt. You need to save this file to some place so that you can use it in future for the restore process.

Follow the below mentioned steps to proceed further.
Creating backup
    1. Open the terminal and execute the below command to backup the list of your installed applications.
$ sudo dpkg –get-selections > applist.bkp
Now you can move the applist.txt file to some safe location (CD, DVD, Pendrive, etc.)

Restoring backup
You can restore your packages after a new/fresh installation of Ubuntu by moving into the folder containing your text file and follow the below mentioned steps.
$ sudo apt-get install aptitude
$ sudo dpkg –clear-selections
$ sudo dpkg –set-selections <applist.bkp>
$ sudo aptitude install
Then follow given prompts.
APTonCD, and easy GUI tool serves the same purpose.

Step-2 Backup your Data.
Most of us normally keep our data in /home. Other customizations and settings would also reside in /home. If there is some place other than /home where you keep your data, that should also be included respectively.

Step-3 Backup the System Settings.
It is a good practice to keep a copy of all system critical files like /etc/fstab, /etc/passwd, /etc/shadow, /etc/group, /etc/sudoers, /etc/hostname, and /etc/hosts. As you can see all these files are very important and are all contained in /etc, so it is more convenient to backup all of /etc directory.

Step-4 Backup Partition Table
Partition table backup proves useful in case where we need to replace a damaged hard disk and to replace the old partition table onto the new disk.
To create a copy of partition table using the below mentioned command.
$ sudo fdisk -l > fdisk.bkp
$ sudo sfdisk -d /dev/xyz > /<path_to_file>/sfdisk.bkp

To restore the partition table follow the below mentioned steps
$ sudo sfdisk /dev/xyz < /<path_to_file>/sfdisk.bkp
GParted Live-CD can also be very useful to backup the partition table.

Step-5 Backup your MBR
MBR is located in the first sector of hard disk and is commonly known for finding the bootable partitions in the partition table. It loads the boot sector i.e. boot loader. A damaged or corrupted MBR means that the operating system will fail to load. MBR resides in 512 byte boot sector, where 446 bytes is reserverd for bootstrap, 64 bytes for partition table and 2 bytes for signature.
MBR Backup
$ sudo dd if=/dev/xyz of=/<path_to_file>/mbr.bkp bs=512 count=1
Use “bs=446” to copy just the MBR.
Destroy MBR
To clear the MBR including the partition table use the below command. In case if you can to just kill the MBR and keep the partition table intact then simply change the value 512 to 446.
$ sudo dd if=/dev/zero of=/dev/xyz bs=512 count=1
MBR Restore
You can copy the MBR backup file to some pendrive and boot the machine using a live-CD and execute the below command to restore the old MBR.
$ sudo dd if=/<path_to_file>/mbr.bkp of=/dev/xyz bs=512 count=1
Replace xxx with the actual device name such as /dev/sda or /dev/hda. Actual device name can be found using the below command.
$ sudo sfdisk -l
Reinstallation of GRUB using the install CD in rescue mode will also give the same effect, without touching the partition table. There are many other ways to restore the MBR. There are many tools and live-CDs available to do so such as UbuntuSecureRemix, Boot-Repair-CD and TestDisk. You can google out many different ways and implement as per your convenience.

Step-6 Final backup
Finally you can also backup some additional directories like /usr/local, /opt or /usr/share, it may be anything that you have customized and put everything into an archive.
Create an archive
$ sudo tar -cvzfp backup.tar.gz /home /root /etc /<path_to_file>/mbr.bkp /<path_to_file>/sfdisk.bkp /<path_to_file>/applist.bkp
Restore archive
$ sudo tar xvzfp backup.tar.gz -C /

METHOD-2 (Using images)
Backing up data using images is very easy with the help of some tools like Partimage and Clonezilla. Even command line tools like dd, tar and rsync have proved very efficient in backups containing large data. The only disadvantage of image backups is its large size. Since it is very simple to just copy and compress the data to an image, it does backup everything on the partition. Normally everyone needs small backup containing only the data and settings on their machine, because of which this method is rarely used.

Backup tools like deja-dup, sbackup, Pybackpack and Grsync are few that are used widely for simple backup administration.

Note: Always test your backup before you implement it.

Any suggestions are more than welcome.

No comments:

Post a Comment