How to Backup Your Files Automatically (for Linux users)

In the two previous articles I introduced two common used utilities in Linux operating system, tar and cron. "tar" is used for creating archive files and "cron" is a time-based scheduling utility. In this article, I will combine "tar" with "cron" to solve the problem: how to backup your files automatically?

Some people think that "backing up" is a boring work. I agree with them. But when an accidence happens, the backup files is very important. So for lazy users, it would be better to let the computer do the boring work.

Have you remembered how to use "tar" to create an archive file?

Tips: tar -cf <archive file> <file or directory>... or tar -czf <archive file> <file or directory> ... to create a compressed archive file.

The task is: Dobby (a Linux user) is a writer and all his documents are located in /home/dobby/documents. He wants to backup his documents to /mnt/disk2 every day. And the backup files' name should be "document_MM-DD-YYYY.tar.gz".

For example, if a backup file's name is "document_08_31_2008.tar.gz", that means it was created in August 31st, 2008.

However, the second problem is that it will generate lots of files. So he wants to delete the old backup files automatically.

Let's solve the problem step by step.

Step 1. You should get the current date. You can use "date" command to do it. The command is:

date '+%m-%d-%Y'

You can test it in the terminal window. The result may be "08-31-2008". Here '+%m-%d-%Y' is a format string. A format string's first character must be '+'. '%m' means the month in two-digits format, '%d' and '%Y' means 'date of month' and 'year' respectively.

Step 2. Combine "date" with "tar". Lets create an archive file using "date" and "tar". "date" is used for getting the current date, which is a part of the archive file name.

tar czf /mnt/disk2/documents_`date '+%m-%d-%Y'`.tar.gz /home/dobby/documents

You see that I put the "date" command into a pair of backslashes '`' which means using its result to replace itself. In this way we can construct the archive file name.

Step 3. Edit /etc/crontab to let "cron" execute the command on time. Use your favorite text editor to open /etc/crontab. Add this line:

0 15 * * * root tar czf /mnt/disk2/documents_`date '+%m-%d-%Y'`.tar.gz /home/dobby/documents

Save it and quit. "cron" will execute this command as root account at 15:00 every day. If you want "cron" to read its configure file immediately, you should restart cron:

/etc/init.d/cron restart (be sure you have enough permissions)

Otherwise, cron will know this task after you reboot the computer.

Step 4. Solve "how to delete old files automatically". To delete the old files, first you should know which files is old, and what the "old" means. For example, if a file was created in one week ago, we think it's "old". You can use "find" to find the old archive files and delete them.

find /mnt/disk2 -name 'document_*.tar.gz' -mtime -7 -delete

It means finding the files whose names match the pattern 'document_*.tar.gz' in /mnt/disk2 directory and they were last modified 7 days ago. Then delete them.

Now add the command to /etc/crontab, let "cron" execute this command at 15:30 everyday:

30 15 * * * root find /mnt/disk2 -name 'document_*.tar.gz' -mtime -7 -delete

All things done. From now on, I think Dobby won't hate "backing up" any more.

Tags: