If you have a Ubuntu server (or even a client), you should consider keeping periodical images of the whole systems as backups; This is, actually, very important in the case your system has been compromised or hacked, or you simply messed up something and can go back.
I don’t think I should be saying anymore about how important is backing up your system… I will cut the crap and go directly to the code:
#!/bin/sh cd / tar -cvpzf /root/backup/backup.tar.gz --exclude=/root/backup --exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/mnt --exclude=/media --exclude=/dev /
This is a shell script to run on unix systems (optimized for Ubuntu) to backup the whole system to a TAR file that can then be restored after a rebuild for the server.
To better understand the TAR command, here is the explanation for each element in the command:
tar – is the command that creates the archive. It is modified by each letter immediately following, each is explained bellow:
- c – create a new backup archive.
- v – verbose mode, tar will print what it’s doing to the screen.
- p – preserves the permissions of the files put in the archive for restoration later.
- z – compress the backup file with ‘gzip’ to make it smaller.
- f <filename> – specifies where to store the backup, /root/backup/backup.tar.gz is the filename used in this example.
- –exclude=/example/path – The options following this model instruct tar what directories NOT to backup. We don’t want to backup everything since some directories aren’t very useful to include. The first exclusion rule directs tar not to back itself up, this is important to avoid errors during the operation. After, we proceed to exclude the /proc, /lost+found, /sys, /mnt, /media and /dev directories in root. /proc and /sys are virtual filesystems that provide windows into variables of the running kernel, so you do not want to try and backup or restore them. /devis a tmpfs whose contents are created and deleted dynamically by udev, so you also do not want to backup or restore it.It is important to note that these exclusions are recursive. This means that all folders located within the one excluded will be ignored as well. In the example, excluding the /media folder excludes all mounted drives and media there.
Adding the date to the file name:
Sometimes, you may need to keep historical copies of your backup images, or you don’t want to override the last image every time you run the back-up script. A good practice in these cases, is to append the date to the name of the backup file.
This will allow you to keep historical copies of your system, allowing you the option of a time-machine-like mechanism. to do this just change the TAR command to be:
tar -cvpzf /root/backup/backup-`date '+%F'`.tar.gz --exclude=/root/backup --exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/mnt --exclude=/media /
Restoring the image to the disk drive:
Use the following code to restore you backup. However, please note that this will overwrite any file that currently exists with the one that was backed up in the TAR image. If the file no longer exists, or you are backup up to a blank (empty) disk, this will create the files from the images.
If a file was created after the last backup and you try to restore the image, the file will have no equivalent stored in the archive and thus will remain untouched.
The code:
tar -xvpzf /home/test/backup.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/test/backup.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.