Oct 19

How to Automate File Back Up in Linux

Christopher Casanova

File Backup is a key component to your computing experience, whether it is used for personal or business use. There have been plenty of scenarios, where someone has lost countless of hours of work due to their system crashing, power outage, etc. By backing up your files, you will be able to quickly recover from any such scenario and save crucial information from being lost.

Linux gives you plenty of control over your computer and provides many options on how to back up your files. A common way to back up a file in a Linux system is through the tar command. The tar command allows you to take multiple files and place them into one file. A concern is that making backups of files takes up space on the machine and using the tar command by itself doesn’t compress the files. Fortunately, the is a -z option that allows you to compress and extract the tar file into a zipped tar file.

Now, the hassle of making backups is that it takes time for you to go into a specific directory, backup the files and then move them to your desired location. The benefit of a Linux system is that it allows you automate this process through the use of scripts and cron jobs. A script is a set of commands that you would enter to perform an automated task such as back up of files. A cron job allows to to perform such a script on a timely matter. This would allows you to perform such an automated task at 1 am every weekday for example.  I will now explain on how to perform such a process.

We will be writing our script using the perl programming language. Now, you must select the directory that you wish to back up and the location of where you wish to store the backup. In this example, we will be backing up the directory called “/home” and we will be storing this backup in a directory called “/backup_tars”. The following is the code  for the script:

      #!/usr/bin/perl -w
      $dir_in = '/home';
      print "Archiving $dir_in/* ...\n";
      use POSIX qw(strftime);
      $now_string = strftime "%m-%d-%Y_%H%M", localtime;
      $tarname= "httpd_".$now_string.".tar.gz";
      $execstring="tar -zpscf /backup_tars/".$tarname." ".$dir_in."/*";

We will save this script in the same directory where we will store our back ups under the name “create_backups.pl”. So the location of this file would be “/backup_tars/create_backups.pl”. Now, in order for this script to be executed automatically, we must set a cron job for it. In order to do that, we must edit our cron file by typing the following command in our Linux shell prompt:

$ crontab -e

Then you would select the time frame that you would wish to run the cron. The syntax is as follows:

1 2 3 4 5 /path_to_command arg1 arg2


  • 1: Minute (0-59)
  • 2: Hours (0-23)
  • 3: Day (0-31)
  • 4: Month (0-12 [12 == December])
  • 5: Day of the week(0-7 {7 or 0 == Sunday})
  • /path_to_command – Script or command name

In this example, we will have our cron run at 3am everyday. To do this enter the following command when you are editing the cron file:

0 3 * * * /backup_tars/create_backups.pl

That’s all there is to it. With the cron job set to run the script everyday at 3am, you will no longer have to worry about manually creating backups for files.

Christopher Casanova is a systems engineer for websoul . To view further information and helpful tips on system administration, view his blog at Chris’ websoul blog.

Leave a Reply