Executing from a Terminal
The simplest way of executing the above backup script is to copy and paste the contents into a file. backup.sh for example. Then from a terminal prompt:
sudo bash backup.sh
This is a great way to test the script to make sure everything works as expected.
The cron utility can be used to automate the script execution. The cron daemon allows the execution of scripts, or commands, at a specified time and date.
cron is configured through entries in a crontab file. crontab files are separated into fields:
# m h dom mon dow command
-
m: minute the command executes on between 0 and 59.
-
h: hour the command executes on between 0 and 23.
-
dom: day of month the command executes on.
-
mon: the month the command executes on between 0 and 12.
-
dow: the day of the week the command executes on between 0 and 7.
-
command: the command to execute.
To add or change entries in a crontab file the crontab -e command should be used. Also, the contents of a crontab file can be viewed using the crontab -l command.
To execute the backup.sh script listed above using cron. Enter the following from a terminal prompt:
sudo crontab -e
|
|
| Using sudo with the crontab -e command edits the root user's crontab. This is necessary if you are backing up directories only the root user has access to. |
Add the following entry to the crontab file:
# m h dom mon dow command
0 0 * * * bash /usr/local/bin/backup.sh
The backup.sh script will now be executed every day at 12:00 am.
|
|
| The backup.sh script will need to be copied to the /usr/local/bin/ directory in order for this entry to execute properly. The script can reside anywhere on the file system simply change the script path appropriately. |
For more in depth crontab options see the section called “References”.