Get the App
SLTechnology News&Howtos  ›  Database  › 

How to realize automatic backup of Mysql

Shulou Source: shulou.com Published: 2022-05-31 15:56:14 10月17日 Update

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.

Tags: Backup data database article situation method more script path login good practical code location content password file article log server Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Xiaomi Redmi Huawei vpn