Home|Cudeso|Linux|*NIX / BSD|Misc|
 

Backup and restore

Contents

1. Introduction

2. Backup

3. Restore

4. Maintenance





1. Introduction

This document is not intended as a "total" guide for safeguarding your computer. It's only a brief description of how I manage my personal backups. Hopefully this could safe you losing valuable data.

2. Backup

A standard installation of GNU/Linux has a built-in backup-tool by the name of tar.

You can make use of this command for creating your own backups (and of course restoring them). The syntax is quite straightforward. This is an example of my back-up script.
  tar -zcvpf /archive/backup-`date '+%d-%B-%Y'`.tar.gz
   --directory /home --exclude=*.iso \
   --exclude=proc --exclude=tmp \
   --exclude=bufferdir .
First there is the tar command itself.
Secondly, you need to provide the different options for tar. I use these
  • z   compress
  • c   create new archive
  • v   verbose
  • p   keep the existing permissions
  • f   the following argument is the filename
The third argument is the archive file that needs to be created. This is because I use the f option with tar. As you can see, I create a filename that contains the current date. By this it's easy to differentiate between the different backup-files that exist. You are not obliged to use the .tar.gz extension (in fact, you can use whatever extension you want) but to keep things simple I advice you to use the default .tar.gz extension.

The fourth argument is the startingdirectory.
--directory /home
With the next arguments you can specify wich files or directories need to be excluded from the archive. This can easliy been done with this directive
--exclude=proc
Finally (and this is often forgotten) you need to specify a dot . to specify that everything else needs to be backuped.

Instead of creating a new archive you can also add files to the end of an existing archive. For this you don't need to specify c (create) but r (append).

Another useful way is updating the archive with only those files that are newer. This is handled by specifying u (update).

So the main three actions for creating or updating archive files are :
  • c   create
  • r   append
  • u   update

3. Restore

After you have cheerfully backuped your precious files you would like to restore them, don't you? Now here it goes.
tar -zxvpf test-backup.tar.gz --directory /home/test/
This command restores the file 'test.backup.tar.gz' into the directory '/home/test/'.

  • z   compress
  • x   extract files
  • v   verbose
  • p   replace permissions with those in the backup-file
  • f   the following argument is the filename
Please beware when executing this command! Any existing file that also exist in the archive will be overwritten.

Instead of using the x for extracting, you could also use t to check the archive (get a complete list of the files that are in the archive). If you're not sure wether a file exists in the backup this is easily done with
tar -ztvpf test-backup.tar.gz | grep "checkforthisfile

4. Maintenance

I've placed the different backup jobs in a cron-job. This way the backup is done every night. After a while though, you will end up with a whole bunch of backups that are out-dated. You can remove the unwanted old files with this command (thanks to Damian O'Hara for the hint) ; this will remove all files not accessed in 7 days :
find /archive/backup* -mtime +7 -exec rm -r {} \
or you could use this for files modified more than 30 days ago (thanks to Charles at integritycomputers.co):
find /archive -name 'backup-*' -mtime +30 | xargs rm -f
Copyleft 2002-2007 - cudeso.bewebmaster@cudeso.betop