Create a regular backup of the database ubuntu regular tasks
First, the command file
Creating db_back.sh
#!/bin/bash echo "start backup" /usr/lib/postgresql/10/bin/pg_dump -h 127.0.0.1 -p 5432 -U username -c -f /data/db_backup/demo_$(date "+%Y%m%d_%H_%M_%S").sql dbname echo "backup finished"
Second, create a scheduled task
By crontab to add a scheduled task command.
Enter the command: crontab -e
Then add in the bottom of the file:
00 2 1 * * dp_user /home/dp_user/db_backup.sh
Third, related commands
cron is a Linux timed execution tool, you can run the job without the need for manual intervention. In Ubuntu server, cron is installed by default and starts. Viewed through / etc / crontab file.
ununtu by calling the run-parts command, timed runs all four scripts directory.
- /etc/cron.hourly, script every hour in the directory so that once, run at 2 minutes past the hour;
- /etc/cron.daily, the script directory will let a day once, run every day at 0:17 time-sharing;
- /etc/cron.weekly, the script directory will be executed once a week to make, run in the seventh day of the week 3:56 time-sharing;
- /etc/cron.mouthly, the script directory will be executed once a month to make, run at 5:32 timeshare number of 19 per month;
Of course, these are the default time period can be modified according to their needs.
1, the start and stop of the cron service
- Start Service
service cron start
- Close Service
service cron stop
- Restart Service
service cron restart
- Reload the configuration
service cron reload
- Check whether the service is running (if run, will return process id)
pgrep cron
2, crontab command
crontab command is used to install, remove or list the tables used to drive the cron daemon. In other words, the user needs to perform a sequence of commands into the crontab file to obtain the execution, each user can have their own crontab file.
- Set a user's cron service
crontab -u
- Set out in detail the contents of a user cron services
crontab -l
- Delete a user's cron service
crontab -r
- Edit a user's cron service
crontab -e
Parameter name Meaning:
- -l displays the contents of the user's Crontab file crontabl -l
- -I before deleting the user's Crontab file to prompt crontabl -ri
- -r remove a user from the directory Crontab Crontab file crontabl -r
- -e to edit the user's Crontab file crontabl -e
3, regular Task Syntax
By editing / etc / crontab file, or the command crontab -e to edit a scheduled task.
Timing task syntax is as follows :
* * * * * command
Timeshare weeks sun and the moon command
E.g:
00 2 1 * * dp_user /home/dp_user/db_backup.sh
For example as follows:
5 * * * * ls / * specified hour to perform a first 5 minutes ls command * /
30 * * * ls. 5 / day designated * 5:30 ls command * /
30 * * ls. 7. 8 / * specified for each No. May 8, 7:30 pm ls command * /
50 * 7 * * root RUN-Parts /etc/cron.daily / day * 7:50 /etc/cron.daily execute all executable directory as root file*/