Use this Easy Cron Job to Backup your Website

Introduction

Using cron as a quick and easy way to backup your website often goes overlooked. Website owners who use WordPress as a content management system often use WordPress plugins that can bog down their installation and make their website less responsive.

What are Cron Jobs?

Cron is a daemon in linux that schedules jobs on your system to be completed at intervals that you specify. Cron jobs are the commands that you ask the cron daemon to execute on the system.

In addition to using it to backup your system, it can also be used to execute any command or shell script that you specify.

Using Cron to Backup Your Website Files

Since cron can be used to execute commands on a server, it is logical to use it as a simple way to manage your website backups. All you have to do is add a command to your crontab.

In order to add a cronjob to your system type the following command:

crontab -e

If this is the first time that you’ve used this command, you will be asked to choose your favorite text editor to make the changes.

no crontab for pi - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.tiny
  3. /bin/ed

Choose 1-3 [1]: 

I recommend using nano, it’s perfect for beginners. Once you select 1 for the nano editor and hit enter, you will be greeted with a screen similar to the screenshot below.

Cron Job List

At 3:00 AM, every day, the following command will compress the public_html directory on your server and name the compressed backup file after today’s date (04.28.20.tar.gz). It will then place the file in the folder that you specify. You need to change [PATH2BACKUP] to the path to the folder on your server that you would like to place your daily backup files. In addition, you will need to change [PATH2] to the path leading to your public_html folder.

0 3 * * * tar -zcf [PATH2BACKUP]/"$(date '+\%%m-\%%d-\%%y').tar.gz" [PATH2]/public_html >/dev/null 2>&1

Place the line in the bottom of your crontab file as show below.

Use this Easy Cron Job to Backup your Website 1

Once you have the line added, you can exit the editor by typing CTRL+X on your keyboard and typing the y key to save.

Success!

That’s it! A reboot is not required. If you would like to view the status of your cron job you can run the following command. You can run this command to check the status of your cron job AFTER it runs.

grep CRON /var/log/syslog

Remember to occasionally remove old backups from your server as they can take up valuable server space. You should also periodically download the backup files from your server to ensure that you have an offsite backup.

If you are using a content management system such as WordPress, remember to also backup your database. A post about using cron jobs to backup your website database will be posted in the next few days.

If you have any questions or suggestions about using cron jobs to backup your website, please leave them in the comments below.

The post, Use this Easy Cron Job to Backup your Website, first appeared on Codeopolis.