Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use script to back up full database and incremental backup of MySQL database

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

How do MySQL databases back up full libraries and incremental backups with scripts? In view of this problem, today, the editor summarizes this article on database backup, which can be used as a reference for interested friends. I hope it will be helpful to you.

1. The full database backup script is as follows:

#! / bin/bash# definition default value # user='root'passwd='123.com'host='localhost'today= `date'+% F'`dump_cmd='/usr/local/mysql/bin/mysqldump'full_backupdir= "/ opt/database/full_backup/" email='916551516@qq.com'# # # load variables and define script usage # USAGE () {echo-e "\ 033 [33m $0 script usage:-u: specify backup user (default is root)-p: specify user password (default password is" 123.com ")-h: specify backup host (default Recognize as local)-d: specify the database to back up (all libraries are backed up by default)\ 033 [0m "} if [[$1 = ='--help' | | $1 = ='- h']] Then USAGE exit 0fi## assigns options # while getopts ": u:p:h:d:" optname;docase "$optname" in "u") user=$OPTARG;; "p") passwd=$OPTARG;; "h") host=$OPTARG;; "d") db=$OPTARG ":") echo "this option has no value!"; "*") echo "error message.";; "?") Echo "does not know this option.";; esacdone# determines the library to be backed up and performs the backup operation # [- d ${full_backupdir}] | | mkdir-p ${full_backupdir} cd ${full_backupdir}: > err.logif [${# db}-eq 0] Then ${dump_cmd}-u$ {user}-p$ {passwd}-h$ {host}-A-F > ${today} .SQL 2 > err.log status=$? Sqlname= "${today} .sql" else ${dump_cmd}-u$ {user}-p$ {passwd}-h$ {host}-- databases $db-F > ${today}. ${db} .SQL 2 > err.log status=$? Sqlname= "${today}. ${db} .sql" fi# determines the backup status # if [${status}-eq 0] Then tar zcf ${sqlname} .tar.gz. / ${sqlname} rm-f ${sqlname} # if it fails, send an alarm email to the specified mailbox # else echo "backup database failed, error log shown above!" > > err.log mail-s "mysql backup" $email

< err.logfi############ 删除二十天前的备份文件 ##############find . -type f -name "*.tar.gz" -mtime +20 -delete2、二进制增量备份脚本 #!/bin/bash ####################### 定义初始值 #############################user='root'passwd='123.com'datadir="/usr/local/mysql/data"backupdir="/opt/database/dailybackup/"dump_cmd='/usr/local/mysql/bin/mysqlbinlog'[ -d ${backupdir} ] || mkdir -p ${backupdir} #判断备份文件存放路径是否存在cd ${datadir} #切换至mysql的数据存放路径sum=`cat bin_log.index | wc -l` #取得所有二进制日志文件总数/usr/local/mysql/bin/mysql -u${user} -p${passwd} -e "flush logs" &>

/ dev/null # refresh binary log files before backup nextnum=0 # define an initial value of 0backupsum=0for file in `log log.index` # traverse all binary log files do binlogname= `basename $file` # get binary log file names # if the number of files currently backed up is less than the total number of files (newly refreshed binaries do not need backup) if [$nextnum-lt $sum] Then [- f ${backupdir} ${binlogname}] | | cp-a ${file} ${backupdir} ${binlogname} # if the binary does not exist in the backup directory, perform the copy action let nextnum++ # to increase the number of successful backups fidoneecho "skip the latest binary log files, backup complete!"

The above is the MySQL database with script backup full database and incremental backup of the specific implementation method, after reading whether there is anything to gain? If you want to know more about it, you are welcome to follow the industry information!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report