Complete Backup and Restore Using “tar” Command

We’ve all made mistakes during our configuration and at some point wish we could go back in time before the mistake. Now that you have the ultimate setup that you’ve worked so hard on, how are you going to be sure you have the same setup after a system or hard drive crash? I’ll explain methods I use to maintain my setup and configuration files without the loss of any data or personal settings. Once you have recovered from the crash, the restoration can be as simple as typing a single command using the Live CD and a terminal.
The first, and most important, is that the backup medium is not located on the same drive as your Linux setup. I personally use an external USB drive for all my backup data (which can be purchased for next to nothing nowadays), although you could use another remote drive within your system unit, even a non bootable drive. These techniques are based on my favorite flavor Ubuntu and can be applied to any version of GNU/Linux.
The easiest method would be to use a simple “tar” command (such as):

Flags used by the “tar” command aid in the backup procedure by allowing us to include or exclude files or directories. With the “exclude” flag we can exclude system folders or individual files which are not necessary for the backup and can be recreated after the restore procedure (as I’ve demonstrated below). This is just a simple backup using the “tar” command but can be included in a more sophisticated approach.
Restoring the above backup procedure is as simple as becoming “root” (sudo su) going to “/” (root) and typing on a command line:

WARNING!! This will overwrite every single file on your partition with the one in the backup archive!
To complete the restore just create the directories that you excluded in the above backup command using the “exclude” flag :

By using the “tar” command to backup your data, you have the ability to extract any file or directory out of the “backup.tgz” file for recovery, for instance, if you have a corrupt or mis-configured “fstab” file you want to recover, you could simply issue the command:

If your in the “/” root directory this will extract the “/etc/fstab” file to the original location. A far safer solution is to restore the desired files under a different directory (for example, your home directory), and then compare, move, or update the files to their original locations afterward. This works the same with complete directories as well.
In conclusion:
By using the method described above you can backup and restore a basic Linux install. We can use this method to give us a more sophisticated backup scheme by using a bash script and a few other backup details. Watch for upcoming write-ups explaining how we can save all configuration, system information, MBR and complete backup using such a scheme. No matter how complex your system, I find the “tar” procedure to be the most widely used and most reliable for any backup solution.
iEntry 10th Anniversary
LinuxHaxor
WH
MH