crontab scheduled backup MySQL database in CentOS

Source: Internet
Author: User
Tags centos


1, write the shell script used by the backup database

The code is as follows Copy Code

vim/home/mysqlback.sh
#!/bin/bash
Mysql_user=root
Mysql_pass=root
Database=test
File_name= $DATABASE. ' Date +%y%m%d '. tar.gz
Tmp_filename= $DATABASE. ' Date +%y%m%d%h%m%s '. sql.gz
#切换到文件存放的目录 to prevent a warning message from being reported in tar: tar:removing leading '/' from member names
cd/alidata1/mysqlback/
#删除7天以前的备份文件
RM-RF $DATABASE. $ (date-d -1day + "%y%m%d"). tar.gz
Mysqldump-u$mysql_user-p$mysql_pass $DATABASE--socket=/tmp/mysql.sock | Gzip-9-> $TMP _filename
Tar-czf $FILE _name $TMP _filename
RM-RF $TMP _filename

2. Use crontab to perform scheduled tasks

The code is as follows Copy Code

Crontab-e
1 * * * root/home/mysqlback.sh

Scheduled to execute every 1:00 script

Simple note

Mysql_user=root Database user Name
Mysql_pass=root Database Password
Database=test Backup Database
File_name= $DATABASE. ' Date +%y%m%d '. tar.gz generate backup files by date

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.