Cron job are used to schedule commands to be executed periodically. You can setup commands or scripts, which will repeatedly run at a set time.
PATH TO POSTGRES/bin/pg_dump -U USERNAME DBNAME > PATH FOR BACKUP FOLDER/$(date '+%Y%m%d')-bkp.sql
0 17 * * * /PATH TO SCRIPT/scriptname.sh
- To edit your crontab file, type the following command at the UNIX / Linux shell prompt:
- Syntax of crontab (Field Description)
1 2 3 4 5 /path/to/command arg1 arg2
OR
1 2 3 4 5 /root/backup.sh
Where,
1: Minute (0-59)
2: Hours (0-23)
3: Day (0-31)
4: Month (0-12 [12 == December])
5: Day of the week(0-7 [7 or 0 == sunday])
/path/to/command - Script or command name to schedule
For example if you want to take a daily backup of your postgres database you can follow the following steps:
- Make entry for user USERNAME for database DATABASENAME as following in the file pg_hba.conf
- local DATABASENAME USERNAME trust
- * make sure that the user has select permission on the database.
- Write a script as following (vi scriptname.sh )
PATH TO POSTGRES/bin/pg_dump -U USERNAME DBNAME > PATH FOR BACKUP FOLDER/$(date '+%Y%m%d')-bkp.sql
- Create a cron job (Take a daily backup at 5 pm)
0 17 * * * /PATH TO SCRIPT/scriptname.sh
No comments:
Post a Comment