mysql back up data automatically achieved under linux

1. Create and edit the file / usr / sbin / backmysql, the command:

vi /usr/sbin/backmysql 

/ Usr / sbin / backmysql The path can customize

It reads as follows:

1 db_user="root"
2 db_passwd="root"
3 db_name="db_test"
4 # the directory for story your backup file.you shall change this dir
5 backup_dir="/usr/software/backup/mysqlbackup"
6 # date format for backup file (dd-mm-yyyy)
7 time="$(date +"%Y%m%d%H%M%S")"     
8 
9 /绝对路径/mysqldump -u$db_user  -p$db_passwd $db_name  > "$backup_dir/$db_name"_"$time.sql"
#压缩文件
/usr/local/mysql/bin/mysqldump -u$db_user  -p$db_passwd $db_name | gzip > "$backup_dir/$db_name"_`date +%Y-%m-%d_%H%M%S`.sql.gz

#文件保存30天
find $backup_dir -name $db_name"_*.sql.gz" -type f -mtime +30 -exec rm -rf {} \; > /dev/null 2>&1

 Note that there is no space between -p and $ db_passwd, otherwise $ db_passwd will be treated as parameters [database name] to resolve

/ Absolute path / mysqldump this must be an absolute path, otherwise the resulting backup file is empty (I here come a cropper a)

db_user = "root" // username

db_passwd = "root" // Password
db_name = "db_test" // database name

 2. Modify the file backmysql property, make it executable;

chmod +x /usr/sbin/backmysql

3.创建定时任务

crontab –e 

#进入编辑界面,内容如下

00 23 * * * ./usr/sbin/backmysql

 

# On the command line. "" Do not be too

23:00 # 0023 is a day to perform bakmysql files, back up the database in the path / usr / software / backup / mysqlbackup

Record keeping for future reference (= =)

service crond start to start

service crond restart restart

service crond stop stop

chkconfig --level 345 crond on service from the start

Guess you like

Origin blog.csdn.net/weixin_42389328/article/details/83820046