21 March 2013

How to: Full Hard-drive backup using the tar command

It's a good idea to do a full backup of the hard-drive after the initial installation as well as when you finally get your server set up the way you want. Having a snapshot of your system right after the initial installation gives you something to revert back to should you want to reconfigure your server without starting from scratch. Linux has many backup utilities but the old standard is still the favorite of admins because of the flexibility offered by its myriad of options.


tar commands can become quite complex. It's easier to enter the command in a text file and make it a shell script. We also need to create a directory to hold the backup file. We'll use a separate directory so we can exclude that directory from the backup (we don't want tar trying to backup a file it's in the process of creating). Enter the following commands:

cd /home
mkdir backups
cd backups


Then use the nano editor to create our shell script file with the command:

nano serverbackup.sh

and enter the following command into it (and don't miss that period at the end of the command):

tar -cvpf /home/backups/serverbackup.tar --directory=/ --exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/mnt --exclude=/media --exclude=/dev --exclude=/home --exclude=/tmp --exclude=/boot .

Enter everything on the same line when entering the command in the editor. Once entered, exit the editor (Ctrl-X) saving the file.

OR, if you want to do it manually, go first to root directory by typing:

cd /

then:
tar -cvpf serverbackup tar --exclude=/serverbackup.tar --exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/mnt --exclude=/media --exclude=/dev --exclude=/home --exclude=/tmp --exclude=/boot /


The c option creates the backup file.

The v option gives a more verbose output while the command is running. This option can also be safely eliminated.

The p option preserves the file and directory permissions.

The f option needs to go last because it allows you to specify the name and location of the backup file which follows next in the command (in our case this is the /backups/fullbackup.tar file).

The --directory option tells tar to switch to the root of the file system before starting the backup.

We --exclude certain directories from the backup because the contents of the directories are dynamically created by the OS. We also want to exclude the directory that contains are backup file.

Many tar references on the Web will give an exclude example as:

--exclude=/proc

However, this will often result in excluded directories still being backed up because there should not be the leading / (slash) character in front of the directory names. If you were to use the above example, tar would back up the proc directory which would result in errors saying that files had changed since they were read.

In addition to holding your backup file, the /backups directory now holds your script file also. You have to make your script executable before you can run it. To do that and run it enter the following two commands:

chmod 750 /home/backups/serverbackup.sh
./home/backups/serverbackup.sh

To restore your "tar ball" you need to copy it to the top-most point in the file tree that it backed up. In our case the --directory option told tar to start backing up from the root of the file system so that's where we would want to copy the tar ball to. Then simply replace the c option with the x parameter line so:

tar -xvpf /serverbackup.tar

Having a backup file is nice but not of much use if the hard-drive itself fails. If your server is running the wu-ftpd FTP server daemon, you should FTP the tar ball off of your server so that it is stored on a different system.

Note that with the use of the --exclude statements when doing the backup, the tar backup file (tar ball) isn't something you could use to do a "bare metal" restore (because the excluded directories would be missing). However, it does work very well if you do an initial vanilla install of the OS and then un-tar your tar ball on top of that.

Saving Space With Compression 

You can compress your backup files to save storage space. Because a lot of Linux files are text files the space savings can be significant.

To enable the compression you have to add the z switch to both the tar-ing and untar-ing commands. You also have to change the file name to indicate the compression. Our tar-ing command becomes:

tar -zcvpf /home/backups/serverbackup.tar.gz --directory=/ --exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/mnt --exclude=/media --exclude=/dev --exclude=/home --exclude=/tmp --exclude=/boot .

OR, if you want to do it manually, go first to root directory by typing:
cd /

then:
tar -zcvpf serverbackup.tar.gz --exclude=/serverbackup.tar.gz --exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/mnt --exclude=/media --exclude=/dev --exclude=/home --exclude=/tmp --exclude=/boot /

Then move the compressed file inside the directory /backups using the syntax:
mv -vi /path-to-oldfile /path-to-newfile
for example:

mv -vi serverbackup.tar.gz /home/backups/serverbackup.tar.gz

Our untar-ing command becomes:

tar -zxvpf /serverbackup.tar.gz


Restore Your Backup

You can manually restore your backup from the /home/backups directory, by typing:

tar -zxvpf /home/backups/serverbackup.tar.gz -C /

A brief explanation:

x - Tells tar to extract the file designated by the f option immediately after. In this case, the archive is /home/backups/serverbackup.tar.gz
-C <directory> - This option tells tar to change to a specific directory before extracting. In this example, we are restoring to the root "/" directory.

Final step: Recreate directories

Once extraction is completed, recreate directories that were not included in the original archive. In our example, these would include /proc, /lost+found, /sys, /mnt, /media and /dev. This can be done with the following command in our example:

mkdir proc
mkdir lost+found
mkdir mnt
mkdir sys
mkdir media
mkdir dev

OR, in just one line:

mkdir /proc /lost+found /mnt /sys /media /dev

-----------------------

No comments: