Shell scripts for two backup Databases

Source: Internet
Author: User

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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.