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

Responsible for database backup, realize incremental backup from Monday to Saturday and full backup on Sunday

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

1. Full backup and incremental backup of mysql. Enable the logbin log function of mysql. Add the following code to the / etc/my.cnf file:

[mysqld]

Log-bin = "/ home/mysql/logbin.log"

Binlog-format = ROW

Log-bin-index = "/ home/mysql/logindex"

Binlog_cache_size=32m

Max_binlog_cache_size=512m

Max_binlog_size=512m

Just restart mysql. The users and groups in the path / home/mysql should be changed to mysql.

2. Incremental backup

Create the following directory under the / home/mysql/ directory:

Mkdir-p/home/mysql/backup/daily

Incremental backup script

Cd / home/mysql

Vi binlogbak.sh

#! / bin/bash

Export.UTF-8

BakDir=/home/mysql/backup/daily

BinDir=/home/mysql

LogFile=/home/mysql/backup/binlog.log

BinFile=/home/mysql/logindex.index

Mysqladmin-uroot-proot123flush-logs

# this is used to generate new mysql-bin.00000* files

Counter= `wc-l $BinFile | awk' {print $1}'`

NextNum=0

The for loop is used to compare the two values of $Counter,$NextNum to determine whether the file exists or is up to date.

For file in `cat $BinFile`

Do

Base= `basename $file`

# basename is used to intercept the mysql-bin.00000* file name and remove the. / before the. / mysql-bin.000005

NextNum= `expr $NextNum + 1`

If [$NextNum-eq $Counter]

Then

Echo $base skip! > > $LogFile

Else

Dest=$BakDir/$base

If (test-e $dest)

# test-e is used to detect whether the target file exists. Write exist if it exists! Go to $LogFile.

Then

Echo $base exist! > > $LogFile

Else

Cp $BinDir/$base $BakDir

Echo $base copying > > $LogFile

Fi

Fi

Done

Echo `date + "Y% m% d% H:%M:%S" `Bakup succ! > > $LogFile

Give binlogbak.sh execution permission

Chmod a+x/home/mysql/binlogbak.sh

3. Full backup

Vi databak.sh

#! / bin/bash

Export.UTF-8

BakDir=/home/mysql/backup

LogFile=/home/mysql/backup/bak.log

Date= `date +% Y% m% d`

Begin= `date + "Y% m / d% H:%M:%S" `

Cd $BakDir

DumpFile=$Date.sql

GZDumpFile=$Date.sql.tgz

Mysqldump-uroot-proot123--all-databases-flush-logs-delete-master-logs-single-transaction > $DumpFile

Tar-czvf $GZDumpFile $DumpFile

Rm $DumpFile

Count=$ (ls-l * .tgz | wc-l)

If [$count-ge 5]

Then

File=$ (ls-l * .tgz | awk'{print$9}'| awk 'NR==1')

Rm-f $file

Fi

# keep only the contents of the database in the past four weeks

Last= `date + "Y% m / d% H:%M:%S" `

Echo start: $Begin end: $Last$GZDumpFile succ > > $LogFile

Cd $BakDir/daily

Rm-f *

Give databak.sh execution permission

Chmod a+x/home/mysql/databak.sh

4. Start the timing task

Crontab-e

# execute a full backup script at 3:00 every Sunday morning

0 3 * * 0/home/mysql/databak.sh > / dev/null 2 > & 1

# do incremental backups from Monday to Saturday at 3:00

0 3 * * 1-6/home/mysql/binlogbak.sh > / dev/null 2 > & 1

Make the above scheduled tasks effective

Crontab / etc/crontab

View scheduled tasks

Crontab-l

Done.

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