How to Create Automated Website Backup With Shell Script?

In this article, we will be talking, How to create Automated Backup for website files with a shell script?


With the help of shell script and crontab, you can take automated backup in the server with the help of following the document.

Archive the content

Backing up your files using tar is very simple using the following command:

# tar -cvpzf /backup/backupfilename.tar.gz /data/directory

A real example would be backing up the HTML folder for your website, my case:

# tar -cvpzf /backup/file_name.tar.gz /var/www/html

Tar command explained

tar = Tape archive

c = Create

v = Verbose mode will print all files that are archived.

p = Preserving files and directory permissions.

z = This will tell tar that to compress the files.

f = It allows tar to get the file name.

Now create a backup script with the help of vi editors.

Create Backup Script

Now let’s add tar command in a bash script to make this backup process automatic. Also, it is good to add some dynamic value in the name to make sure there is no overwriting of backup files.

E.g. Create the file using the vi editor and paste the below script.

# vi /

Paste the following script and change your details.


TIME=`date +%b-%d-%y` # This Command will read the date.$TIME.tar.gz # The filename including the date.

SOURCE=/var/www/html # Source backup folder.

DESCT=/backup # Destination of backup file.


Note: The only risk that can occur is to get out of disk memory if the source folder is big, but you can configure it by removing file after 30 days or any number of days with the help of the find command.

find /backup -type f -name ‘*.gz’ -mtime +15 -exec rm {} \;


In Linux, we can easily use the cron jobs in order to schedule tasks and perform the above backup task easily. The cron jobs line has 6 parts see below explanation: Minutes Hours Day of Month Month Day of Week Command 0 to 59 0 to 23 1 to 31 1 to 12 0 to 6 Shell Command Open crontab editor utility:
crontab -e

Note: the edit rules are similar to vi editors.
Paste the following text in the editor:
00 04 * * * /bin/bash /
This will run the script every day at 04:00:00.
Check the rules of crontab with the help of the following command:
Crontab -l


After the above configuration, you can take backup automatically and remove it in some specific period of time.

Learn how to scale, manage, and optimize your applications with a SLB. Read our solution brief "Get More from Your Enterprise Network".


Get started with CloudMinister Today