Linux shell regularly backs up mysql database

install crontab


Check if crontab has been installed

# crontab
-bash: crontab: command not found  It

means that crontab is not installed 

to start the installation:

# yum -y install vixie-cron

Wait for the installation to complete.

Writing a backup database shell

The content of the vim /usr/java/shell/bak_mysql.sh

script is as follows:

#!/bin/bash
# @Author: Bottle
# @Date:    2017-09-02
# @Desc:    backup dbName db data

mysqlserver='127.0.0.1'
mysqldbname='dbName' #Database name
mysqluser='dbName' #Database username
mysqlpwd='dbPassword' #Database password
mysqlbakdir='/usr/java/bak_msyql' #Database backup directory
if [ ! -d '$mysqlbakdir/`date +%Y`/`date +%m`' ];then
   mkdir -p $mysqlbakdir/`date +%Y`/`date +%m`
be
sqlbakfile=$mysqlbakdir/`date +%Y`/`date +%m`/$mysqldbname`date +%F-%H-%M-%S`.sql

mysqldump -u$mysqluser -p$mysqlpwd   -h$mysqlserver --hex-blob $mysqldbname >$sqlbakfile
tar czvPf $ sqlbakfile.tar.gz $ sqlbakfile
rm $sqlbakfile

Give bak_mysql.sh execution authority

# chmod u+x bak_mysql.sh

execute it first to see if the script is successful?

# ./bak_mysql.sh  to

see if the backup file is generated?
# cd /usr/java/bak_msyql
# ll

is not generated, that means There is a problem with the bak_msyql.sql script, please debug it until it is correct.


Add scheduled tasks


# crontab -e

This time the operation is the same as vim.

Input:

0 4 * * * /usr/java/shell/bak_mysql.sh

save. (The above is executed once every day at 4 am)

to start the scheduled task.
# /sbin/service crond start

View crontab log:
# tail -f / var/log/cron

set the crontab to run automatically
at  Add /sbin/service crond start to the end of the /etc/rc.d/rc.local script


other instructions

crontab -u //Set a user's cron service, generally root users need this parameter when executing this command

crontab -l //List the details of a user's cron service

crontab -r //Delete no user cron service

crontab -e //edit a user's cron service

/sbin/service crond start //start service 
/sbin/service crond stop //close service 
/sbin/service crond restart //restart service 
/sbin/service crond reload //Reload configuration 
/sbin/service crond status //View status

or use

# service crond start 
# service crond stop 
# service crond restart 
# service crond reload 
# service crond status

Timing parameter description


The meaning of the crontab file:
 In the crontab file created by the user, each line represents a task, and each field in each line represents a setting. Its format is divided into six fields, and the first five paragraphs are the time setting paragraphs. , the sixth segment is the command segment to be executed, the format is as follows:
minute hour day month week command Sequence: minute hour day month week
where :
minute: indicates the minute, which can be any integer from 0 to 59.
hour: Indicates the hour, which can be any integer from 0 to 23.
day: Indicates the date, which can be any integer from 1 to 31.
month: Indicates the month, which can be any integer from 1 to 12.
week: Indicates the day of the week, which can be any integer from 0 to 7, where 0 or 7 represents Sunday.
command: The command to be executed, which can be a system command or a script file written by yourself.
In the above fields, the following special characters can also be used:


Asterisk (*): represents all possible values, for example, if the month field is an asterisk, it means that the command operation will be executed every month after the constraints of other fields are met. .
Comma (,): A list range can be specified with comma-separated values, for example, "1,2,5,7,8,9"
Bars (-): A bar between integers can be used to indicate a range of integers , for example "2-6" means "2,3,4,5,6"
Forward slash (/): You can specify the interval frequency of time with a forward slash, for example "0-23/2" means to execute every two hours. At the same time, forward slashes can be used together with asterisks, such as */10, if used in the minute field, it means to execute every ten minutes.


Reference blog

http://blog.csdn.net/testcs_dn/article/details/48829785
http://www.cnblogs.com/chen-lhx/p/5996781.html
http://blog.csdn.net/testcs_dn/article/details/48780971






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326014631&siteId=291194637