这篇文章将为大家详细讲解有关如何实现备份数据库的shell脚本,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
成都创新互联公司是一家专业提供鄂州企业网站建设,专注与成都网站制作、做网站、html5、小程序制作等业务。10年已为鄂州众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。备份数据库的shell脚本一
代码如下:
#!/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
数据库备份脚本shell二
代码如下:
[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
关于“如何实现备份数据库的shell脚本”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。