设计一个shell脚本来备份数据库,首先在本地服务器上保存一份数据,然后再远程拷贝一份,本地保存一周的数据,远程保存一个月。
假定,我们知道mysql root账号的密码,要备份的库为discuz,本地备份目录为/bak/mysql, 远程服务器ip为192.168.123.30,远程提供了一个rsync服务,备份的地址是 192.168.123.30::backup . 写完脚本后,需要加入到cron中,每天凌晨3点执行。
参考答案:
#! /bin/bash
### backup mysql
### Writen by Aming.
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/texlive/2012/bin/i386-linux:/usr/local/mysql/bin:/usr/local/mysql/bin:/root/bin
d1=`data +%w`
d2=`date +%d`
pass="your_mysql_password"
bakdir=/bak/mysql
r_bakdir=192.168.123.30::backup
exec 1>/var/log/mysqlbak.log 2>/var/log/mysqlbak.log
echo "mysql backup begin at `date +"%F %T"`."
mysqldump -uroot -p$pass --default-character-set=gbk discuz >$bakdir/$d1.sql
rsync -a $bakdir/$d1.sql $r_bakdir/$d2.sql
echo "mysql backup end at `date +"%F %T"`."
然后加入cron
0 3 * * * /bin/bash /usr/local/sbin/mysqlbak.sh
rsync -av 192.168.123.30::backup/ /bak/mysql/
find /bak/mysql -ctime +30 |xargs rm -f
rsync -av --delete /bak/mysql 192.168.123.30::backup/
find /bak/mysql -ctime +7 |xargs rm -f
- #!/bin/bash
- ##written by lin
- mysqldump -uroot -plin discuz > /bak/mysql/mysql.backup
- find / -name mysql.backup -mtime +7 -exec rm -rf {} \;
- rsync -av /bak/msyql/mysql.backup root@192.168.123.30::backup
- mysql -uroot -plin -h 192.168.123.30
- find / -name mysql.backup -mtime +30 -exec rm -rf {} \;
db=`date +%y%m%d`
dblocal=`date -d "-5day"`
dbremote=`date -d "-1month"`
/usr/local/mysql/bin/msyqldump -uroot -pabc.123 discuz >/tmp/$db.sql
rm -f /tmp/$dblocal.sql
rsync -avzp /tmp/$db.sql 192.168.123.30::backup/
使用rsync删除一个月以前的数据不懂
#设计一个shell脚本来备份数据库,首先在本地服务器上保存一份数据,然后再远程拷贝一份,本地保存一周的数
据,远程保存一个月。
#假定,我们知道mysql root账号的密码,要备份的库为discuz,本地备份目录为/bak/mysql, 远程服务器ip为192
.168.1.212,远程提供了一个rsync服务,备份的地址是 192.168.1.212::javazgq02 . 写完脚本后,需要加入到cron中,每天凌晨3点执行。
d=`date +%F`
week=`date -d "-1day" +%F`
month=`date -d "-2day" +%F`
mysqldump -uroot discuz > /bak/mysql/"$d"discuz.sql
rsync -a /bak/mysql/"$d"discuz.sql 192.168.1.212::javazgq02
rm -rf /bak/mysql/"$week"discuz.sql
mkdir -p /bak/remotemysql/
rsync -a 192.168.1.212::javazgq02 /bak/remotemysql/
rm -rf /bak/mysql/"$month"discuz.sql
rsync -a /bak/remotemysql/ 192.168.1.212::javazgq02
rm -rf /bak/remotemysql
加入cron服务: 0 3 * * * /bin/sh /root/mysqldump_rsync.sh
不会写 看看
补充:
有两个关键点不会,一个是date +%d这个命令忘了,另一个是不知道rsync还可以当mv用
#!/bin/bash
[ -d /bak/mysql ] || /bin/mkdir -p /bak/mysql
d1=
d2=
pass=login
exec >/var/log/mysqlbak.log 2>&1
bakdir=/bak/mysql
r_bakdir=192.168.123.30::backup
echo "mysql backup begin at "
/usr/local/mysql/bin/mysqldump -uroot -p$pass discuz >$bakdir/discuz_$d1.sql
/usr/bin/rsync -av $bakdir/discuz_$d1.sql root@$r_bakdir/discuz_$d2.sql
echo "mysql backup end at "
crontab -e增加一条计划任务
0 3 * * * /bin/sh /usr/local/sbin backup.sh
#!/bin/bash[ -f /bak/mysql ] || [ mkdir /bak/nysql ]mysqldump -uroot -p discuz > /bak/mysql/discuz_`date +%F`/usr/bin/rsync -t /bak/mysql root@192.168.123.30::backupcrontab -e0 3 * * * /usr/bin/bash bak.sh
#Description: This script is to backup speicified mysql and sync to remote host
#Author: Jiazhi Yang
#Date: 16/11/2016
#Script Name: backupmysql.sh
##定义当前的时间
curtime=`date +%H:%m:%S`
##定义当前的日期
curdate=`date +%F`
##定义存放数据库备份文件的目录
backuppath=/bak/mysql
##定义5天前的日期
datea=`date -d "-5 day" +%F`
##定义30天前的日期
dateb=`date -d "-30 day" +%F`
##定义远程主机的IP及存放备份文件的目录
remotehost=192.168.9.213::backup
##定义标准输出及错误输出的日志存放位置
exec 1>/var/log/mysqlbak.log 2>/var/log/mysqlbak.log
##定义密码
passwordfile='passw0rd.password'
##判断存放数据库备份文件的目录是否存在
if [ -d $backuppath/$curdate ]; then
echo "$backuppath/$curdate exists."
else
mkdir -p $bakuppath/$curdate
echo "$bakuppath/$curdate directory created."
fi
##开始备份数据库到指定的目录
echo "Now start to back up database... "
mysqldump -ujustcall -pjcdbup0932 justcall_db >/bak/mysql/$curdate-$curtime.sql
##判断备份结果是否已执行成功
if [ $? eq 0 ]; then
echo "Now backing up datebase ends at $curdate-$curtime..."
fi
##开始将本地的数据库备份文件同步到远程主机
echo "Now start to rsync backup files to remote hosts."
rsync -av --password-file=$passwordfile $backuppath/$curdate/$curtime.sql $remotehost/$curdate-$curtime.sql
##判断本次同步是否成功
if [ $? -eq 0 ]; then
echo "Now rsync finished."
else
echo "Error,rsync is not successfull."
fi
cd $backuppath
##删除5天前的数据库备份文件
rm ./$curdate/$datea.sql -rf
if [ $? -eq 0 ]; then
echo "MySQL backup file deleted that saved in 5 days ago..."
else
echo "Error,deleting is not successfull."
fi
#!/bin/bash
fd=`date -d "-5days ago" +%y%m%d`
d=`date +%F-%T`
user=root
pw=123456
dblist=`mysql -u$user -p$pw -hlocalhost -e "show databases"`
denylist=(1,2,3,4,5,6,7)
exec 1>/var/log/mysqlbak.log 2>&1
for list in $dblist
do
flag=1
for dlist in denylist
do
if test $list = $dlist
then
flag=0
fi
done
if [ $list -ne 0 ]
then
mysqldump -u$user -p$pw -hlocalhost $list > $list_$d.sql
rm -rf $list_$fd.sql
rsync -av $list_$d.sql 192.168.123.30::backup/$list_$d.sql
fi
done
编辑回复