Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement shell script for backing up database

Shulou Source: shulou.com Published: 2022-06-03 07:16:52 09月16日 Update

This article will explain in detail the shell script on how to back up the database. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Shell script 1 for backing up the database

The code is as follows:

#! / bin/bash

# specify the script shell to run

# run the script to give the user the right to execute

Bakdir=/backup

Month= `date +% m`

Day= `date +% d`

Year= `date +% Y`

Hour= `date +% k`

Min= `date +% M`

Dirname=$year-$month-$day-$hour-$min

Mkdir $bakdir/$dirname

Mkdir $bakdir/$dirname/conf

Mkdir $bakdir/$dirname/web

Mkdir $bakdir/$dirname/db

# Hot backup database

Cp / opt/mysql/my.cnf $bakdir/$dirname/db/my.cnf

Cd / opt/mysql

Mysqldump-- opt-u zhy-p-- password=1986 test > $bakdir/$dirname/db/test.sql

Mysqldump-- opt-u zhy-p-- password=1986 phpwind > $bakdir/$dirname/db/phpwind.sql

# remote copied directories must have writeable permissions

Scp-r / backup/$dirname root@10.1.1.178:/backup

Database backup script shell II

The code is as follows:

[plain]

#! / bin/bash

#--

# Target machine configuration

#--

# DBUser- > Database user name

# DBPasswd- > Database password, not null

# DBName- > Database name

# SERVER- > IP address

#--

DBUser=****

DBPasswd=****

DBName=*

SERVER=****

#--

# The local machine configuration

#--

# LocalDBUser- > Database user name

# LocalDBPasswd- > Database password,not null

# LocalDBName- > Database name

# BackupPath- > Backup path,end with'/'

# LogFile- > Log file

# num- > backup numbers

#--

LocalDBUser=**

LocalDBPasswd=***

LocalDBName=****

BackupPath=****

LogFile=****

# num=2-> Has not been completed

#--

#--

# Other configuration

#--

# IgnoreTables- > ignore tables,like this:

# IgnoreTables='- ignore-table=DBName.TableName1-ignore-table=DBName.TableName2.'

# NeedTables- > need backup tables,if you want to backup all tables,please let it empty

# like this: NeedTables='table1 table2 table3'

#--

IgnoreTables='****'

NeedTables=

# begin

NewFile= "$BackupPath" dbBackup$ (date +% y%m%d) .sql

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

Echo "- -" > > $LogFile

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

Echo $(date +% y%m%d-- date='5 days ago') > > $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

Echo "Begin ro recover the data...."

Mysql-u$LocalDBUser-p$LocalDBPasswd-- default-character-set='utf8' $LocalDBName > $LogFile

Else

Mysqldump-h$SERVER-u$DBUser-p$DBPasswd-- default-character-set='utf8'-- quick-- single-transaction $DBName $NeedTables $IgnoreTables > $NewFile

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

Fi

This is the end of the article on "how to implement the shell script for backing up the database". 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 for more people to see.

Tags: Scripts backups data databases articles more permissions run good practical generation content copy articles users directories knowledge second generation references help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MariaDB NVidia Apple vpn