MySQL scheduled backup data script

Source: Internet
Author: User

The cluster of Project 3.0 is built to complete, leaving only the details of the configuration, and the post-security and optimization of processing, taking into account the database data needs to be backed up processing,

But daily manual backup processing is too passive, and daytime user access, there will be data changes and in the backup will affect the performance of the server's normal operation,

So consider writing a script and developing a timed task to perform a script backup of the data;

#!/bin/bash
# # #数据库定时备份, and package the backed up library for compression
User=root
passwd=123456
Back_path=/vdb1/mysql/backup
Data_name=zmq123
Date= ' Date +%y%m%d '

mkdir $back _path/$date
Mysqldump-u $user-p$passwd $data _name |gzip > $back _path/$date/$data _name.sql.gz

#定时任务, schedule a data backup every night 23:55
55 23 * * *. /vdb1/mysql/mysqldump.sh

After running for a while, and found that the backup of the data some of the disk space, and I want to save only the last January of data can be, so the script to upgrade,

In the case where no one is aware, the disk will not be full of backup data;

# # #升级版每天晚上11:55 minutes for scheduled backups,
#!/bin/bash
year= ' date +%y '
month= ' date +%m '
day= ' date +%d '
backpath=/ vdb1/mysql/backup/$year $month/
user=root
passwd=123456
dataname=zmq123

[-D $backpath] | | mkdir-p $ Backpath
rq= ' date +%y%m%d '
mysqldump-u $user-p$passwd $dataname |gzip > $backpath/$RQ. sql.gz

#定时任务
Timeshare    Sun Moon Week
23   *  *  *./vdb1/mysql/mysqldump.sh

####### #每月2号晚上11:30 for scheduled deletion last month back up data
#!/bin/bash
Year= ' Date +%y '
Month= ' Date +%m '
Month= ' Expr $month-1 '
If [$month-eq 0];then
Month=12
Day=31
Year= ' expr $year-1 '
Fi

If [$month-lt];then
Pre=0
Fi

backdir=/vdb1/mysql/backup/$year $pre$month
RM-RF $backdir #删除上一个月的数据
# #定时任务
30 23 2 * *. /vdb1/mysql/delmysqlbak.sh

Ok! Write to finish, first run for a while to see it! There is a situation for further improvement;

MySQL scheduled backup data script

Related Article

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.