The implementation of crond for regular backup of CentOS 7.2 database (taking Kingbase 7 as an example)

Keywords: Database crontab Unix CentOS

When a project goes online, it often needs to back up the database regularly to deal with uncertain environment and unknown problems. It is arranged as follows:

What is crond?

crond and crontab are inseparable. Crontab is a command, common in Unix and Unix like operating systems, which is used to set periodically executed instructions. The command reads the instructions from the standard input device and stores them in a crontab file for later reading and execution. It comes from the Greek word chronos(χ ρόο ο), which originally means time.

And crond is its daemons. cron service is a scheduled service. You can add or edit tasks to be scheduled through crontab command.

crond has been installed in the default CentOS 7.2. If not, you can install it through: yum install crontabs

To see if the crontab service is set to boot:

systemctl list-unit-files | grep enable | grep crond

View crontab status

service crond status

On / off

//Start service

service crond start

//Shut down service

service crond stop

//Restart service

service crond restart

//Reload configuration

service crond reload

Writing scheduled task command format

min hour day month dayofweek command

Time share day month week order

crontab command

Function: set the timer.

Syntax: crontab [- U < user name >] [profile] or crontab [- U < user name >] [- ELR]
Explanation: cron is a resident service, which provides the function of timer, allowing users to execute preset instructions or programs at a specific time. As long as the user can edit the timer configuration file, the timer function can be used. Its configuration file format is as follows: Minute Hour Day Month DayOFWeek Command

Parameters:
-e edit the timer settings for the user.
-l lists the timer settings for the user.
-r delete the timer settings for this user.
-U < user name > specifies the user name to set the timer.

The first column represents minute 1-59, and each minute is represented by * or * / 1
The second column represents hours 1-23 (0 represents zero point)
The third column represents the date 1-31
The fourth column represents the months from January to December
The fifth column identification number is from 0 to 6 (0 means Sunday)
Column 6 commands to run

The specific time operations are as follows:

1. Check the status of crond

2. Write the database scheduled backup script as follows:

#!/bin/bash
####################################################################################################################
###
###Descipt: this script is used for kingbase database backup,before you run it,you should set the variables such as
###         kdb_home,kdbback_dest,kdb_user,kdb_pass,kdb_host,kdb_port,kdb_list,keep_time and so on.
###
####################################################################################################################
####################### variable define ##########################
# Database installation directory
kdb_home="/home/XXXXX/Kingbase"
# Target path for database backup
kdbback_dest="/home/XXXXX/dbbackup"
# Database user name
kdb_user="username"
# Database user password
kdb_pass="password"
# Database port number
kdb_port="54321"
# Database access ip
kdb_host="127.0.0.1"
# The mode of database. Multiple modes can be separated by commas, such as "TEST,SAMPLE"
kdb_list="DBNAME"

keep_time="7"

date=$(date '+%Y%m%d%H')
kdbback_final="${kdbback_dest}/kdbback_final"
LD_LIBRARY_PATH="${kdb_home}/unixodbc/lib:${kdb_home}/lib:${kdb_home}/bin"

####################### kingbase backup dest test ##################

[ -d ${kdbback_dest} ] || mkdir -p ${kdbback_dest}
[ -d ${kdbback_final} ] || mkdir -p ${kdbback_final}


####################### kingbase backup start  #######################

cd ${kdbback_dest}
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
for db in `echo $kdb_list | sed 's/,/ /g'`; do
	[ -d ${db} ] || mkdir -p ${db}
        cd ${db}
	##### kingbase server check as follows
        ${kdb_home}/bin/isql -h ${kdb_host} -p ${kdb_port} -U ${kdb_user} -W ${kdb_pass}  -c "select now();" TEMPLATE2 > /dev/null 2>&1
        if [ $? -ne 0 ] ;then
	    echo "${date} sorry, please run the script in a kingbase server" >> backup_${db}_${date}.log 
	    mv backup_${db}_${date}.log ${kdbback_final}
	    exit 1
        else
	    echo "${date} kingbase server is ok,kingbase backup ${db} is beginning ..." >> backup_${db}_${date}.log
        fi
	##### end
        ##### kingbase backup files process as follows
	${kdb_home}/bin/sys_dump -p ${kdb_port} -U ${kdb_user} -W ${kdb_pass} -h ${kdb_host} -f ${db}_${date}.KDMP ${db} >> backup_${db}_${date}.log 2>&1
        if [ $? -eq 0 ] ;then
            tar zcvf ${db}_${date}.tar.gz ${db}_${date}.KDMP*
            if [ $? -eq 0 ] ;then
               rm -f ${db}_${date}.KDMP*
            else
               mv ${db}_${date}.KDMP* ${kdbback_final} 
            fi
            find . -mtime +${keep_time} -name ${db}'_*' | xargs -I {} rm {}
        else
	    rm -f ${db}_${date}.KDMP*
        fi
        ###### end
        ###### kingbase backup log files process as follows
	tar zcvf backup_log_${db}_${date}.tar.gz backup_${db}_${date}.log
        if [ $? -eq 0 ] ;then
	       rm -f backup_${db}_${date}.log
	else
	       mv backup_${db}_${date}.log ${kdbback_final}
        fi
        find . -mtime +${keep_time} -name backup_log_${db}'_*' | xargs -I {} rm {}
        ###### end
        cd ${kdbback_dest} 
done
exit 0

3. crontab setting

Execute "contab -e"

Write the following statement and execute it every five minutes

*/5 * * * / usr/local/backup.sh (path of script)

4. Start crond

service crond start

You can check the execution status through cd /var/spool/mail / user file

In case of insufficient permission, you can empower as follows:

chmod 777 backup.sh

Reload:

service crond reload

 

So far

Posted by jswash on Thu, 07 Nov 2019 19:56:55 -0800