设计一个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
myaqldump -uroot -p123456 discuz >/bak/mysql
myaqldump -uroot -p123456 -h192.168.123.30 discuz > 192.168.123.30::backup
0 3 * * * /bak/mysql
0 3 * * * 192.168.123.30::backup
#!bin/sh
name=`date +%y%m%d`
name2=`date -d "5 days ago" +%y%m%d`
name3=`date -d "30 days ago" +$y$m$d`
#本地备份
mysqldump -uroot -p'password' discuz >/bak/mysql/$name
rm -rf /bak/mysql/$name2
#远程备份
rsync /bak/mysql/$name 192.168.123.30::backup
rsync --exclude=$name3 --filter="R $name3" delete-excluded --existing --ignore-existing . 192.168.123.30::backup
#网上找的删除远程指定文件,没测试。。
crontab -e
0 3 * * * /bin/sh /bak/backup.sh
远程同步脚本:
vi mysqlbackup.sh
#!/bin/bash
date=`date =%F`
date7=`date -d "-7 day" +%F`
date30=`date -d "-30 day" +%F`
mysqldump -uroot -p'123456' discuz >/bak/mysql/"$date"mysql.sql
rsync -avL /bak/mysql/ root@192.168.123.30:/backup/
rm -rf /bak/mysql/"$date7"mysql.sql>/dev/null
ssh root@192.168.123.30 "rm -rf /backup/"$date30"mysql.sql"
加入任务计划:
crontab -e
0 3 * * * /bin/bash /root/mysqlbackup.sh
[root@OceanI sh]# cat mysqldump.sh
#!/bin/bash
#
dir=/bak/mysql
cd $dir
filename=`date +%F`
remote=`date +%F -d "30 days ago"`
# Remove old backups
find $dir -mtime +7 -exec rm -fr {} \; > /dev/null 2>&1
#Dump the database of discuz
mysqldump -uroot -ppasswd discuz > $filename
#Transfer the backup to the remote host
rsync /$dir/$name 192.168.123.30:/backup
[root@OceanI sh]# crontab -l
00 03 * * * /bin/bash /root/sh/mysqldump.sh
USER=root
PASSWORD=password
DBNAME=discuz
time=`date +%F`
mysqldump -u$USER -p$PASSWORD $DNAME | gzip -9 > /bak/mysql/$time.sql.gz
rsync -avpz /bak/mysql/ 192.168.123.30::backup
find /bak/mysql -name “*.sql.gz” -mtime +7 | xargs rm -rf >/dev/null
crontab -e
0 3 * * * /bin/bash /pwd/mysql_backup.sh
备份机上
find /backup -name *.sql.gz -mtime +30 | xargs rm -rf > /dev/null
加到crontab里面去即可
- #!/bin/bash
- #Backup the mysql for discuz
- backup_path=/bak/mysql
- date=`date +%Y%m%d`
- /usr/local/mysql/bin/mysqldump -uroot -pwpmysql --default-character-set=utf8 --single-transaction --lock-tables=false $1 > $backup_path/mysql_$1_$date.sql
- find $backup_path/* -mtime +6 |xargs rm -rf
- /usr/bin/rsync -av --delete --password-file=/etc/rsync_passwd /bak/mysql/ rsync@192.168.123.30::backup
- 00 03 * * * /bin/bash /root/script/mysql.sh discuz
- 00 04 * * * /bin/bash /root/script/rsync.sh
- #!/bin/bash
- # backup mysql for mysql.myDB.
- # Writen by Wangxiaoqiang 2014.11.25.
- # crontab -e { * 3 * * * sh /myscipts/backup.sh }
- mysqlDB=mydb
- sqluser=root
- sqlpasswd=123456
- bakpath=/data/sql
- mysqldump -u${sqluser} -p${sqlpasswd} -R ${mysqlDB} > ${bakpath}/`date +%F`_${mysqlDB}.sql
- expect -c "
- spawn rsync -a /data/sql/2014-11-25_mydb.sql root@192.168.1.81:/data/sql_bak/
- expect {
- \"(yes/no)?\" { send \"yes\r\"; exp_continue }
- \"password:\" { send \"123456\r\"; expect eof }
- }
- spawn ssh root@192.168.1.81
- expect {
- \"(yes/no)?\" { send \"yes\r\"; exp_continue }
- \"password:\" { send \"123456\r\"; exp_continue }
- \"#\" { send \"find /data/sql_bak/ -type f -name *.sql -mtime +30 | xargs -i rm -f {}\rexit\r\" ; expect eof }
- }
- "
- find ${bakpath} -type f -name *.sql -mtime +7 -exec rm -f {} \;
这种很蹩脚的方法可以实现了 ,还可以优化到更好。
d1=`date +%F`
d2=`date -d '-7days' +%F`
d3=`date -d '-30days' +%F`
d=root
p=passwd
mysqldump -u$d -p$p discuz >/bak/mysql/$d1.sql
rm -rf /bak/mysql/$d2
rsync -av /bak/mysql/$d1.sql 192.168.123.30::backup/$d1.sql
远程主机上执行rm -rf /bak/mysql/$d3
#
mkdir -p bak/mysql
mysqldump -uroot -p'123456' discuz >/bak/mysql/discuz.bak.`date +%Y%m%d`
rsync -av discuz.bak root@192.168.1.108::backup
本地cron
0 3 * * * /bin/sh /root/rsync_discuz.sh
* * * * * 5 /bin/rm -rf /bak/mysql/discuz.bak.`date -d "5 days ago" +%y%m%d`
远程cron:* * 1 1 * * /bin/rm -rf discuz.bak.`date -d "30 days ago" +$y$m$d`
#!/bin/bash
#backup mysql
#Up to a remote server
time=`date +%F`
time2=`date -d "10 days ago" +%F`
mysqldump -u root -p `123456` discuz > /bak/mysql/$time
rm -rf /bak/mysql/$time2
rsync /bak/mysql/$time 192.168.123.30::backup
crontab -e
0 3 * * * /bin/sh /bak/backup.sh
- #!/bin/bash
- #backup mysql
- #Up to a remote server
- time=`date +%F`
- time2=`date -d "10 days ago" +%F`
- mysqldump -u root -p `123456` discuz > /bak/mysql/$time
- rm -rf /bak/mysql/$time2
- rsync /bak/mysql/$time 192.168.123.30::backup
- crontab -e
- 0 3 * * * /bin/sh /bak/backup.sh
* 3 * * * rsync -avL /bak/mysql/ user@ 192.168.123.30::backup
bash: /tmp/discuz.$d.sql: ambiguous redirect
/usr/local/mysql/bin/mysqldump -uroot -p123456 --single-transaction --databases discuz > /bak/mysql/discuz.$d.sql|gzip -f /bak/mysql/discuz.$d.sql
rsync -a /bak/mysql/* 192.168.123.30::backup
find /bak/mysql/ -name '*.gz' -mtime +7 -type f |xargs rm -f
- #!/bin/bash
- ## gxp is mysql bak rsync
- echo "It\'s OK"
- ## 2016-03-08
- mysqldump -uroot --databases mysql > /tmp/bak/mysql/`date +%F`.sql
- a=`echo $?`
- if [$a -eq 0]
- then
- echo "mysql bacakup error"
- else
- echo "It's OK"
- fi
- rsync -avzP --port 8730 --password-file=/etc/rsync_pass.txt /tmp/bak/mysql/`date
- +%F`.sql aming@192.168.193.130::backup
- b=`echo $?`
- if [$b -eq 0]
- then
- echo "rsync remote error"
- else
- echo "It's OK"
- fi
[ -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::backup
crontab -e
3 * * * * /usr/bin/bash bak.sh
#!/bin/bash
dt=`date +%F`
/bin/mkdir /data/backup 2> /dev/null
/usr/bin/mysqldump -uroot -p123456 discuz> /data/backup/discuz_$dt.sql
/usr/bin/rsync -av --password-file=/etc/rsync.passwd /data/backup/discuz_$dt.sql repl@192.168.0.161::backup/
/bin/find /data/backup/ -name "discuz*.sql" -mtime +7 -exec rm -f {} \;
/usr/bin/ssh root@192.168.0.161 "find /data/backup/ -name "discuz*.sql" -mtime +30 -exec rm -f {} \;"
--------------------------------------分割线--------------------------------------
# crontab -l
* 3 * * * /bin/sh /usr/local/sbin/rsync-mysql-discuz.sh
先粗略的写了下
编辑回复