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 realize automatic backup of Mysql

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

Share

Shulou(Shulou.com)05/31 Report--

This article will give you a detailed explanation on how to achieve automatic backup of Mysql. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

You can put this script into crontab, execute it once every morning, and back it up automatically.

This script can only be executed once a day at most, and only the backup for the last five days is kept on the server.

Code:

#! / bin/bash

# This is a ShellScript For Auto DB Backup

# Powered by aspbiz

# 2004-09

# Setting

# set database name, database login name, password, backup path, log path, data file location, and backup method

# by default, the backup method is tar or mysqldump,mysqldotcopy

# by default, log in to the mysql database with root (empty) and back up to / root/dbxxxxx.tgz

DBName=mysql

DBUser=root

DBPasswd=

BackupPath=/root/

LogFile=/root/db.log

DBPath=/var/lib/mysql/

# BackupMethod=mysqldump

# BackupMethod=mysqlhotcopy

# BackupMethod=tar

# Setting End

NewFile= "$BackupPath" db$ (date +% y%m%d) .tgz

DumpFile= "$BackupPath" db$ (date +% y%m%d)

OldFile= "$BackupPath" db$ (date +% y%m%d-- date='5 days ago') .tgz

Echo "- -" > > $LogFile

Echo $(date + "% y-%m-%d% H:%M:%S") > > $LogFile

Echo "--> > $LogFile

# Delete Old File

If [- f $OldFile]

Then

Rm-f $OldFile > > $LogFile 2 > & 1

Echo "[$OldFile] Delete OldFile Success!" > > $LogFile

Else

Echo "[$OldFile] No Old Backup File!" > > $LogFile

Fi

If [- f $NewFile]

Then

Echo "[$NewFile] The Backup File is exists,Can't Backup!" > > $LogFile

Else

Case $BackupMethod in

Mysqldump)

If [- z $DBPasswd]

Then

Mysqldump-u $DBUser-- opt $DBName > $DumpFile

Else

Mysqldump-u $DBUser-p$DBPasswd-- opt $DBName > $DumpFile

Fi

Tar czvf $NewFile $DumpFile > > $LogFile 2 > & 1

Echo "[$NewFile] Backup Success!" > > $LogFile

Rm-rf $DumpFile

Mysqlhotcopy)

Rm-rf $DumpFile

Mkdir $DumpFile

If [- z $DBPasswd]

Then

Mysqlhotcopy-u $DBUser $DBName $DumpFile > > $LogFile 2 > & 1

Else

Mysqlhotcopy-u $DBUser-p $DBPasswd $DBName $DumpFile > > $LogFile 2 > & 1

Fi

Tar czvf $NewFile $DumpFile > > $LogFile 2 > & 1

Echo "[$NewFile] Backup Success!" > > $LogFile

Rm-rf $DumpFile

*)

/ etc/init.d/mysqld stop > / dev/null 2 > & 1

Tar czvf $NewFile $DBPath$DBName > > $LogFile 2 > & 1

/ etc/init.d/mysqld start > / dev/null 2 > & 1

Echo "[$NewFile] Backup Success!" > > $LogFile

Esac

Fi

Echo "-" > > $LogFile

_________________

Do

{

Goodgoodstudy ()

Daydayup ()

}

While (! died)

This is the end of the article on "how to achieve automatic backup of Mysql". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

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

Wechat

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

12
Report