Tag: CentOS regularly backs up data through scheduled tasks
Centos regularly backs up data with scheduled tasks
This year the company bought a set of OA products, for the function is still in the development and configuration phase, as an administrator, I need to daily data and corresponding database for regular backup, because OA products are running on Linux, so we need to configure Shell script on Linux, Then, by scheduling the task to achieve a scheduled backup of the data, for the relevant script content can be seen, but the requirement is that the specified directory must be compressed and named after the date, and eventually moved to the specified path. The scheduler program on Linux is similar to that on Windows, On Windows we can just run taskschd.msc to open and create a new Task wizard, but on Linux it's easier than on Windows to just connect the corresponding parameters. The commands for scheduling tasks on Linux are crontab, which can be configured with the following parameters, and we are able to see the help through the man crontab.
-E: Edits the contents of a user's crontab file. If you do not specify a user, the crontab file for the current user is edited. -L: Displays the contents of a user's crontab file, or displays the contents of the current user's crontab file if no user is specified. -r: Deletes a user's crontab file from the/var/spool/cron directory and, if no user is specified, deletes the current user's crontab file by default. -I: Give a confirmation prompt when deleting a user's crontab file.
Similarly: the Crontal parameter format is * * * * * *
The first minute of an hour 0-59
Second * hours of the day 0-23
Day 1-31 of the third * one months
Fourth * months of the year 1-12
Fifth * Days of the week 1-7
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/84/6D/wKioL1eQeIiDab9qAABpOTSNdZY800.png-wh_500x0-wm_3 -wmp_4-s_1231603257.png "title=" 1.png "alt=" Wkiol1eqeiidab9qaabpotsndzy800.png-wh_50 "/>
We're done with the configuration, so let's get to work.
We view the current directory structure through LS–L
We have written the script, the script is called databackuper.sh, then we need to/data directory Weaver directory data compression back to the Waver_back directory, and then named after the date;
It is important to add execution permissions to the script when executing the shell script; chmod +x databackuper.sh Add Execute permission
We define two parameters in the script, $, $, so the definition is more flexible, when more than one file needs to be backed up, you can use this way, and then in the execution of the shell, we need to add two parameters after the shell script, one is the source, a target path;
./databackuper.sh/data/weaver/data/weaver_backup
#!/bin/bash source=$1 target=$2 cpexec=$ (which  CP) mkdirexec=$ (which mkdir) curdate=$ (date +%Y- %m-%d) bakdirtmp=${source%*/} bakdirname=${bakdirtmp##*/}$ curdate basedir=${bakdirtmp%/*}if [ ! -d "$SOURCE" ]; then echo "$ (date +%Y-%m-%d_%H:%M:% S) - The source $SOURCE is not existed you specified " > >/var/log/${bakdirtmp##*/}.log exit 2 fi if [ ! -d "$TARGET" ]; then echo " $ (date +%y-%m-%d_%h:%m:%s) - The target $TARGET is not existed you spec ified " >>/var/log/${BAKDIRTMP##*/}.log exit 22 fi # $MKDIREXEC -p $TARGET/$BAKDIR # $CPEXEC -rf $SOURCE/* $ target/$BAKDIR/cd $TARGET && tar zcf ${bakdirname}.tar.gz -c $ basedir ${bakdirtmp##*/}if [ "$?" -ne 0 ]; then echo "$ (date +%y-%m-%d_%h:%m:% S) - Backup directory: $SOURCE to $TARGET/${bakdirname}.tar.gz is Failed " >>/var/log/${BAKDIRTMP##*/}.log Else echo&nBSP; " $ (date +%y-%m-%d_%h:%m:%s) - Backup directory: $SOURCE to $TARGET/${ Bakdirname}.tar.gz is successful " >>/var/log/${BAKDIRTMP##*/}.log Fi exit 0
The method above is more extensible, and then we can modify the variable directly
#!/bin/bash source=/data/weaver target=/data/weaver_backupcpexec=$ ( WHICH CP) mkdirexec=$ (which mkdir) curdate=$ (date +%Y-% m-%d) bakdirtmp=${source%*/} bakdirname=${bakdirtmp##*/} $CURDATE BASEDIR=${BAKDIRTMP%/*}if [ ! -d "$SOURCE" ]; then echo "$ (date +%y-%m-%d_%h:%m:%s) - the source $SOURCE is not existed you specified " >>/var/log/${bakdirtmp##*/}. log exit 2 fiif [ ! -d "$TARGET" ]; then echo "$ (date +%y-%m-%d_%h:%m:%s) - The target $TARGET is not existed you spec ified " >>/var/log/${BAKDIRTMP##*/}.log exit 22 fi# $MKDIREXEC -p $TARGET/$BAKDIR # $CPEXEC -rf $SOURCE/* $TARGET/$BAKDIR/cd $TARGET  && TAR ZCF ${BAKDIRNAME}.tar.gz -C $BASEDIR ${BAKDIRTMP##*/}if [ "$?" -ne 0 ]; then echo "$ (date +%y-%m-%d_%h:%m:% S) - Backup directory: $SOURCE to $TARGET/${bakdirname}.tar.gz is Failed " >>/var/log/${BAKDIRTMP##*/}.log else echo "$ (date +%y-%m-%d_%h:%m:%s) - Backup directory: $SOURCE to $TARGET/${bakdirname}.tar.gz is successful " >>/var/ Log/${bakdirtmp##*/}.log fI exit 0
In this case, we can execute it directly, without adding any parameters.
./databackuper.sh can be executed;
Then we define a scheduled task to be backed up, we're going to make a backup every night 23:30
CRONTAL–E30 * * */root/data/databackuper-new.sh
Once a/root/data/databackuper-new.sh script is executed every night 23:30
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/84/6B/wKioL1eQY32jOrQYAAArOM7Tc6k028.png "height=" 161 "/>
This article is from the "Gao Wenrong" blog, make sure to keep this source http://gaowenlong.blog.51cto.com/451336/1828368
Centos scheduled backup data