HowTo Make & Schedule Daily & Weekly Backup Scripts
General Overview




Scheduling Backups

Overview:

The simplest way to schedule backup scripts, such as the examples that I have used here, is to use the cron daemon. Since you are making backups, you want the jobs to run under the root account. This way they are garanteed to have access to all of the files that you are trying to backup.

The Work:

On FreeBSD, at least 3.4.x and up, the root cron file is /etc/crontab.

To schedule the three example script you simply need to add three lines. The first commented line in the sample ones given below, is a description of the columns in the crontab file. The commented line preceding each entry, is the description of what each command does. You can ommit the comments, the column descriptions are by default at the begining of your crontab file, the others, are just there so that you can remember what the command is doing without having to read the whole command. The Backslash "\" at the end of the lines simply means that the command is continued on the next line. The sendmail command e-mails the log files to your servers root account.

To further explain, I will break down the Daily Backups entry. The first column being the minutes column tells the server to run the job 15 minutes after the hour. The second column being hours, tells the server to run the job on the second hour of the day, or 2am. The third and fourth columns tell the server to run the job every day of the month and every month of the year, respectively. The fifth column tells the server to run the job on the first through the fifth day of the week, or Sunday through Friday. This is the tricky column, since both 0 and 7 refer to Sunday. In all of the columns you can use ranges, like 0-5 that is used here for the day of the week, or you can use just one value, or a group of values such as 1,3,5 to run the job on Monday, Wednesday and Friday, if it was in the fifth column. The sixth column specifies the user to run the job as, this can only be specified in the root crontab file. Finally the seventh column, is the command. In this example the command runs the daily backup script that you created, you need to call the script file using its full path name, which may be different than the ones I used here. The "|" character is a shell command that takes the terminal output of the job and runs it through the following command. In this case the sendmail command, which I am passing root as an argument that tells sendmail to then mail that output to the root user account. This leaves you with a backup log that is e-mail to the root account.

# Min	Hour	M-Day	Month	W-Day	User	Command 

# Run Daily Backups Sunday-Friday
15	2	*	*	0-5	root	/root/backups/scripts/daily.sh \
| sendmail root

# Run mySQL Backups Every Day
0	3	*	*	*	root	/root/backups/scripts/mySQL.sh \
| sendmail root

# Run Weekly Backups Every Saturday
30	5	*	*	6	root	/root/backups/scripts/weekly.sh \
| sendmail root

Notes:

The tar command by default gives warnings when it strips the leading "/" from each directory. This is done to allow you to restore the files to another directory, not just the one that they were backed up from. This will cause the previously described cron jobs to cause your root account to recieve a message with this warnings in it. This will occure in the root account no matter what account you e-mail the log files to, and even if you send the output to a file instead of an e-mail. I didn't wish to tell the tar command to not strip the leading "/", nor do I know how to suppress these messages from being logged, if anyone knows how to do this please e-mail me and let me know.