How to create script for BACKUP and use crontab on Ubuntu (Server) 14.04 LTS Step-by-Step

One of the most standard ways to run tasks in the background on Linux machines is with cron jobs. They’re useful for scheduling tasks on the VPS and automating different maintenance-related jobs. “Cron” itself is a daemon (or program) that runs in the background. The schedule for the different jobs that are run is in a configuration file called “crontab.” I’ll be logged in as root.

Step 1 – Create file and paste backup script in it.

root@mail:/# touch /root/backup_script.sh

Step 2 – Make file executable.

root@mail:/# touch chmod u+x /root/backup_script.sh

Step 3 – Open file with the following commands:

root@mail:/# nano /root/backup_script.sh

Step 4 – Add backup script. For example I will show you how to backup your folder every day in different folders. If you want you can make backup on every hour in the same or in different folder. Add the following basic script. This script will create folder every day with the ‘date name’.

#!/bin/bash
mkdir -p /home/backupcontentfolder/`date +%d-%m-%y-%H`/
rsync -raz /youfolder /home/backupcontentfolder/

Step 5 – Open crontab. If you open crontab with nano you will not have any problems but if you open crontab with vi or vim you should see sintaxis.

root@mail:/# sudo crontab -e

Step 6 – Make the following script executable every day or every hour or every two hours. I will show you the various options.

#m h dom mon dow
#Оnce at night
* 1 * * * /root/backup_script.sh
#0nce every two hours
* */2 * * * /root/backup_script.sh
#Once every five minutes every two hours
*/5 */1 * * * /root/backup_script.sh
#Once fifteen minutes past seven
5 7 * * * /root/backup_script.sh

9 ThemeHow to add second hdd (additional hard drive) in Ubuntu 14.04 (Linux) Step-by-step

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*
Website