Ubuntu crontab scheduled backup postgres database and upload FTP server

Source: Internet
Author: User
Tags backup postgres database postgres database



Recently the company requested to back up the database, so we looked up the comparative information. Don't say much nonsense, into the subject.

ubuntu crontab


Objective: To regularly back up the Postgres database under Ubuntu, and to pack and upload to the designated FTP server.

how to edit crontab ubuntu


After finding the information, reload crontab ubuntu the workaround:



① Write a backup database, package compression, upload the specified FTP script.



② adds the script to the Ubuntu scheduled Tasks crontab.

restart crontab ubuntu


Below are the steps to perform:



① Write shelll script: bk.sh, for the shell script do not understand, you can Baidu W3cshool shell.



It is best to write with the root user: Su root.



After writing, add execute permissions, the newly written shel default does not have Execute permission, crontab on reboot ubuntu chmod 777 bk.sh. This will do.



Execute script: sh bk.sh or run directly./bk.sh.



Test the bk.sh first. See if there is a problem, there is already a comment inside.



Note: All paths involved in bk.sh are used according to the path. Because when executing in crontab, unlike in terminal execution, there is a temporary environment variable that records the current execution path when executed in the terminal.



This can be done more relative to the path, but not in crontab, so you can only use absolute paths.


1#!/bin/Bash2 3 #服务器参数配置4dump=/usr/lib/postgresql/9.3/bin/pg_dump #pg_dump备份文件执行路径5out_dir=/home/Postgres_data #备份存放路径6Linux_user=Root #系统用户名7Db_user=postgres #数据库账号 #注意: Non-root user--skip-lock-with backup parameterstables, or you may get an error.8ftp_ip=127.0.0.1#FTPIP9ftp_port= +#FTPPortTenFtp_usr=cz #FTPUser OneFtp_pwd=cz #FTPPassword Adays=7#DAYS =7 represents the deletion of a backup 7 days ago, that is, only the last 7 days of backup -  - CD $OUT _dir #进入备份存放目录 theDate= 'Date+%y_%m_%d ' #获取当前系统时间 -Out_sql="$DATE. SQL"#备份数据库的文件名 -Tar_sql="mysqldata_bak_$date.tar.gz"#最终保存的数据库备份文件名 -$DUMP-U$DB_USER-E utf-8>$OUT _sql #备份 + Tar-CZF $TAR _sql./$OUT _sql #压缩为.Tar. gz format - RM$OUT _sql #删除. SQL-formatted backup files + Chown$LINUX _user: $LINUX _user $OUT _dir/$TAR _sql #更改备份数据库文件的所有者 A Find$OUT _dir-name"mysqldata_bak_*"-type F-mtime + $DAYS-execRM {} \; #删除7天前的备份文件 atDeldate= 'Date-d-7day +%y_%m_%d ' #获取7天前的时间 - FTP-n<<! - Open $FTP _ip $FTP _port #打开ftp服务器. 21 for FTP Port - user $FTP _usr $FTP _pwd #用户名, password - binary #设置二进制传输 - #cd Mysqlbak #进入ftp目录 (this directory must be a real directory for FTP space) inlcd/home/postgres_data/#列出本地目录 - prompt toPut Mysqldata_bak_$date.Tar. GZ Mysqldata_bak_$date.Tar. GZ #上传目录中的文件 +Delete Mysqldata_bak_$deldate.Tar. GZ Mysqldata_bak_$deldate.Tar. GZ #删除ftp空间7天前的备份 - Close theBye!
Backup Shell





② writes the crontab file so that the bk.sh executes regularly. Note that it is the root user.



Vi/etc/crontab



In the same format, add: * * * * * * Root script absolute path.



Save As long as the system level is only saved to take effect. I have verified it.



Here are the problems and solutions that are encountered during the completion process:



Question 1:



There are two ways of Crontab, one is/etc/crontab. The other is crontab-e. Don't know the difference between the two?



Answer: The former system level, the latter is the user level. Best use the former, reason crontab detailed 1.



Add:


1. Using Crontab-e command the use of this command is relatively simple. Direct Input~# crontab-e will open an edit window, the first line has a content format hint: # m H Dom Mon Dow command specific meaning: Minute hour date month week order, a certain day of the month (Mon) or Days of the week (Dow) a few (h,24 hours) (m) Executing a command,*represents any time. For example:3* * * * */home/meng/hello.sh is: 03 o'clock per hour./home/meng/the hello.sh script below. After saving, follow the onscreen prompts to enter CTRL+x exit, you will be prompted to save, enter Y; prompt for the file name, and a temporary file name, because just test, direct carriage to save. Note: To restart the cron process after editing is complete:~#/etc/init.d/Cron Restart Observe the results of the run and will find that hello.sh will be executed every hour, at 03 ticks. When using this command, the biggest concern is whether the system will be able to execute smoothly after the reboot. I restarted the system and found everything was OK, so I dismissed this concern. However, there is still a problem, in general, the server is logged in after a reboot, and no user logged in. So if I'm doing crontab ,-e command, do not use the root account, then after the system restart will be successful? 2. Edit crontab file crontab located/ect/folder, in http://Wiki.ubuntu.org.cn/cronhowto has a detailed introduction to it, but I do not see too understand. Open the Crontab file, and if you haven't edited it, you can see something like this: #/etc/crontab:system-wide crontab# Unlike any and crontab you don'T has to run the ' crontab'# command to install theNewVersion when you edit Thisfile# and Filesinch/etc/CRON.D. These files also has username fields,# that's none of the other Crontabs Do. SHELL=/bin/Shpath=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin# m H Dom Mon Dow user command -* * * * Root CD/&& run-parts--report/etc/cron.hourly - 6* * * Root test-x/usr/sbin/anacron | | (CD/&& run-parts--report/etc/cron.daily) - 6* *7Root Test-x/usr/sbin/anacron | | (CD/&& run-parts--report/etc/cron.weekly) the 6 1* * Root test-x/usr/sbin/anacron | | (CD/&& run-parts--report/etc/cron.monthly) The meaning of each command cannot be explained in detail because of the limited knowledge of the script. In line 10th, the format of the file contents is also defined. Can see more than using Crontab-e command, one more user. It represents the user who executes the command, and if it is root, it indicates the system user. So, I added the following line:3* * * * * root/home/meng/hello.sh then save the file, I verified it, no need to restart. 
crontab detailed 1




about Crontab:crond and Crontabcron is a server that Linux provides to execute shell commands on a regular basis. Mainly consists of two parts of the Crond:cron service daemon, used to schedule the Crontab:cron provided by the UI, for editing scheduling crontab use of the method, you can man, such as: clip_image002 There are three main options: "-E "edit: With the Vim class Yes, but when you save the exit, he will check the task syntax for you;"-L "List all tasks;"-R "Delete all Tasks; p.s.: The current user-based crontab is currently described, and the system-level crontab is described later. The syntax for basic syntax cron is simple, as follows: the "dispatch frequency" shell command looks at an example: clip_image004 can see that the front is minutes, hours, days, months and weeks, and finally the shell command. The minimum frequency for cron scheduling is 1 minutes. The above schedule means: "Every 10:30, execute/some/path/do_something.sh ". The specific meaning of each piece in the above command is as follows: The meaning minute Hour day Month Week command range0- -0- at1- to1- A0-7shell command P.S.: The "Week" column of 0 and 7 represents Sunday P.S.: The last day of each month cannot be directly supported by crontab and needs to be judged by script, see here. P.S.: commands are best for absolute paths and some auxiliary symbols that can be used to write more flexible special characters to represent meaning*(asterisks) are accepted at all times, such as the example above, Day,month and week as "*, which means that no matter what month, the day, the week, the command is executed at 10:30. , (comma) parallel time. As an example:*3,6,9, A* * *command above means at 3, 6, 9 or 12, to execute the order. -(minus) continuous interval. As an example:*9- -* * *command above means from 9 to 17, every minute to perform this task./n (Slash) n represents an array, representing every n units, such as every 5 minutes, can be written as follows*/5* * * *command Some examples -  at  One  -* Mail Benben //every November 27, 1, 23 minutes, 59 seconds, send a love letter to Benben .0  -* *5Mail All_members < weekily_report_notify//55 points a week to remind all team members to send weeklysystem-level crontab system-level crontab only the root permission is authorized to edit, the crontab is a file, the location is/etc/crontab, the syntax for system-level crontab is slightly different from the above, requiring that command performers be added between frequencies and commands, as shown below, and that you can add some global variables to use in scheduling: clip_image006 detailed system-level scheduling can be found here. Here we need to emphasize/a few cron-related directories and files under the ETX directory, as shown below CLIP_ IMAGE008CRON.D: directory, in this directory and all the files in the sub-directory conforming to the schedule syntax will be executed Cron.deny and Cron.allow: This file records the rejected and allowed to execute the account number, Cron.allow priority is greater than cron.deny, it is recommended to leave only one can CRO N.daily/hourly/monthly/Weekly: All of these directories are scripts, which are executed at the specified time, respectively. Although the absolute path is crontab executed, it can be understood as a timed trigger for manual execution. However, the path in the execution process is still different, in many cases the manual execution succeeds, but the crontab is unsuccessful, mostly due to the relative path. Therefore, when using crontab, try to use absolute path. As a result, some practices summarize one lesson: If a command can be executed manually, but the crontab fails, then you need to focus on the relevant path problem. 
crontab detailed 2





Ubuntu crontab scheduled backup postgres database and upload FTP server


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.