生产环境中的备份shell/ target=_blank class=infotextkey>shell脚本,共四个,供大家学习参考。
1,批量备份多个数据库
#!/bin/bash
BACKUP_HOST="localhost"
BACKUP_USER="root"
BACKUP_PASS="123456"
BACKUP_DIR="/backup/mysql"
BACKUP_DBNAME="one two three"
COPIES=10
mysqldump="mysqldump"
TIMEPOINT=$(date +%Y-%m-%d)
MYSQLDUMP_OPTS="-h $BACKUP_HOST -u$BACKUP_USER -p$BACKUP_PASS"
umask 0077
test ! -d "$BACKUP_DIR" && mkdir -p "$BACKUP_DIR"
test ! -w $BACKUP_DIR && echo "Error: $BACKUP_DIR is un-writeable." && exit 0
for dbname in $BACKUP_DBNAME
do
test ! -d "$BACKUP_DIR/$dbname" && mkdir -p "$BACKUP_DIR/$dbname"
$MYSQLDUMP $MYSQLDUMP_OPTS $dbname | gzip > $BACKUP_DIR/$dbname/$dbname.$TIMEPOINT.sql.gz
done
find $BACKUP_DIR -type f -mtime +$COPIES -delete
2,网站目录备份
#!/bin/bash
#edit www.jb200.com
BACKUP_DIR=/backup/website
WEBSITE_DIR=/www
TIMEPOINT=$(date +%Y-%m-%d)
COPIES=10
test ! -d "$BACKUP_DIR" && mkdir -p "$BACKUP_DIR"
test ! -w $BACKUP_DIR && echo "Error: $BACKUP_DIR is un-writeable." && exit 0
umask 0077
tar -zcf $BACKUP_DIR/www.$TIMEPOINT.tar.gz $WEBSITE_DIR
find $BACKUP_DIR -type f -mtime +$COPIES -delete
3,目录rsync同步(需先安装配置rsync)
#!/bin/sh
srcdir1=/www/example1.com
srcdir2=/www/example2.com
dstdir3=jszb@111.111.111.111::example1
dstdir4=jszb@222.222.222.222::example2
rsync -avz --delete --progress --password-file=/backup/bash/rsync.pas $srcdir1 $dstdir3 &
rsync -avz --delete --progress --password-file=/backup/bash/rsync.pas $srcdir2 $dstdir4 &
4,nginx日志切割