Two shell scripts for backup databases _linux shell

Source: Internet
Author: User

A shell script that backs up a database

Copy Code code as follows:

#!/bin/bash
#指定运行的脚本shell
#运行脚本要给用户执行权限
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
#热备份数据库
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
#远程拷贝的目录要有可写权限
Scp-r/backup/$dirname Root@10.1.1.178:/backup

Database backup script Shell two
Copy Code code 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 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"

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.