This article contains a simple shell script which on execution will back up your linux system. If you are not familiar with Backup or Restore, I highly recommend you go through our previous stones on Backup your Linux System, Restore your Backups and Tips on Backup and Restore. Since, the entire posts on Backup was based on GNU tar, the following script will be using tar utility to backup.

You might be wondering why to through such a script when I can simply do it with a single command. The reason for doing so is to show you the power of shell script where a simple script can be used with cron utility to automate your backup whenever you want. So, all you have to do is open an editor ( ed, nano, vi or gedit ), copy the following script and save it to ( or anything that you prefer )

The Script

​backup_source=“/home   /var/www/   /etc  /root  /boot  /opt”
​date=`date '+%d-%B-%Y'`
​hostname=$(hostname -s)
​echo “Backing Up your Linux System”
​tar cvpzf   $backup_dest/$filename  $backup_source
​echo “Backup finished”

How do I execute it ?

Open the terminal and type

​sudo bash


I have used 5 variables : $backup_source, $backup_dest, $date, $hostname, $filename.

However, you can modify the value of the variables as per your requirement.

For Example:

$backup_source can be simply your home folder ( $backup_source=“/home” ) , $backup_dest can have other path as well. ( $backup_dest=“/media/backup” ).

Similarly, you can use other options of tar as well.

For Example:

To list all files in the archive file after backup you can use:

 tar tvf  $filename 

What more … ?

Now, we automate the script execution using cron utility. The cron utility allows the execution of scripts, or commands, at a specified time and date.

Open a terminal and type

​sudo crontab -e

Select your preferred editor, and type the following after this line

# m h dom mon dow  command
0   6   *   *   1   bash   /usr/local/bin/

The script will now be executed at 6:00 am every week.

Here, /usr/local/bin/ is the path of file. You can change the script path appropriately.The -e option enable you to edit the user’s crontab. Similarly you can edit user’s crontab using -u option.

For Example

sudo crontab -u rabi -e

Reference : ​Ubuntu documentation, cron manual page

Subscribe to fortystones.
Follow @fortystones on Twitter.
Get updated from our Facebook Fanpage.