两个备份数据库的shell脚本
程序员文章站
2022-06-29 23:06:58
备份数据库的shell脚本一复制代码 代码如下:#!/bin/bash#指定运行的脚本shell#运行脚本要给用户执行权限bakdir=/backupmonth=`date...
备份数据库的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
echo "-------------------------------------------"
echo "作者 陈建虹"
上一篇: 水果减肥法:香蕉怎么吃才能减肥