Back up database shell script 1
Copy codeThe Code is as follows :#! /Bin/bash
# Specify the running script shell
# Run the script to grant the user the execution permission
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
# The remote copy directory must have the write permission
Scp-r/backup/$ dirname root@10.1.1.178:/backup
Database backup script shell 2Copy codeThe 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 '/'
# 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 Old File 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 <$ NewFile
Echo "Data has been restored! ">>> $ 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
Echo "-------------------------------------------"
Echo "author Chen jianhong"