today i had a challenge, a customer wanted his back ups to be done at least once a month. but i thought i would be too much maintenance for me to have to login to the system every month and to do the backups manually, i thought there's gotta be a better way to do this automatically. so i remember during my linux classes, the teacher showed us about cron jobs. so in this little tutorial im going to show you how you create a simple cron job. what is a cron job? cron is a Linux system process that executes a script or a program at a determined time. ok, im my example lets say i have to create backups every month. well to make backups, i have to enter a command on the linux shell. so the first thing to do is to create an executable script, then eschedule that executable script to be run every 1st of the month. so this is how i would do it... 1. login to your linux machine using the shell 2, go to the directory where you want to create a new cron file called backups.cron in my example, i will be creating in in my html directory so i would send this command:
cd /var/www/html
the next step is to create a script to do the backups. so ill create an executable. im going to create a file called backups:
cat > backups
now type the command you want to be executed: I would Type the following lines:
tar -pczf backup_file_name_date_.tar.gz /var/www mysqldump --opt -u root -p password --all-databases | zgip > mysql_backup_file_name_date_.sql.gz
* with this command im backup up my www directory and all mysql databases in one how script Press ENTER to move the cursor to a new line and press CTRL-D. At this point, the file new_script contains a series of shell commands. Examine the file to be cetain it is correct:
cat backups
it should contain exactly the shell command you want to execute now we need to make it executable:
chmod +x backups
3. create a cron file in the /var/www/html. i will be using nano text editor. most people use VI for their unix text editor, i find that using NANO is much easier. so that's why i use it, but if you feel more confortable using vi, go ahead, but for this example, i will be using NANO. so im going to create a file called backups.cron, to do this, send this command:
nano backups.cron
4. once you have executed the command, it will look like this:
GNU nano 1.3.8                 File: backups.cron







^G Get Help    ^O WriteOut    ^R Read File   ^Y Prev Page   ^K Cut Text    ^C Cur Pos
^X Exit        ^J Justify     ^W Where Is    ^V Next Page   ^U UnCut Txt   ^T To Spell
5. ok so to make my cron job work every month at midnight i use this table to determine what time to put on my cron file.. this is the syntax Crontab syntax :- A crontab file has five fields for specifying day , date and time followed by the command to be run
at that interval. * * * * * command to be executed - - - - - | | | | | | | | | +----- day of week (0 - 6) (Sunday=0) | | | +------- month (1 - 12) | | +--------- day of month (1 - 31) | +----------- hour (0 - 23) +------------- min (0 - 59)
* in the value field above means all legal values as in braces for that column. The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range). Note: The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed . Crontab Example _______ A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.
30     18     *     *     *         rm /home/someuser/tmp/* 
* note the the hour is used in military time so 1800 = 6pm 6. so to make my crontab for once a month i will put the following in my backups.cron file so type the following:
GNU nano 1.3.8                 File: backups.cron

0 0 1 * * /var/www/html/backups





^G Get Help    ^O WriteOut    ^R Read File   ^Y Prev Page   ^K Cut Text    ^C Cur Pos
^X Exit        ^J Justify     ^W Where Is    ^V Next Page   ^U UnCut Txt   ^T To Spell
so to save the file with nano, all you do is hit control + x, you will be prompted: Save Modified buffer? type: Y then you will be prompted: File to Write: backups.cron... just hit your ENTER key now you will be back to the linux shell prompt now the you have created the cron file, execute the corntab command execute the following command to list any jobs that are currently loaded int crontab
crontab -l
If none are listed, its ok to continue loading your cron job. To execute load the cron job we just created, type the following command:
crontab /var/www/html/backups.cron
Now execute the crontab -l command again and your cron job should be listed, otherwise, you did something wrong. try again. done * this tutorial was provided courtesy of www.webune.com thanks