Yefei.Blog

个人日记 WIKI

用户工具


服务器数据备份小脚本

平时维护服务器的时候需要定期自动备份数据,就写了一个小脚本

#!/bin/sh
 
BACKUPROOT=/backup/automatic
 
FTPUSER=backup
FTPPASS=password
FTPHOST=192.168.1.2
 
DATE=`date +%Y-%m-%d_%H-%M-%S`
 
# MySQL 完整备份
BACKUPPATH=$BACKUPROOT/db-$DATE
BACKUPNAME="db-$DATE.zip"
mkdir -p $BACKUPPATH
mysqlhotcopy -u root -p123456 dbname $BACKUPPATH
zip -r9 $BACKUPROOT/$BACKUPNAME $BACKUPPATH
rm -fr $BACKUPPATH
 
# 上传到 FTP
ftp -n<<!
open $FTPHOST
user $FTPUSER $FTPPASS
binary
cd /db
lcd $BACKUPROOT
prompt
put $BACKUPNAME $BACKUPNAME
close
bye
!
 
# 零散文件增量备份
# 本地文件系统备份
duplicity --no-encryption -v 0 /var/www file:///backup/vhosts
# 远程 FTP 备份
duplicity --no-encryption /var/www ftp://$FTPUSER:$FTPPASS@$FTPHOST/www/ --time-separator _ >> /root/duplicity.log

crontab 中创建一个定时任务

0 5 * * * /backup/automatic.sh > /backup/automatic.log


皖ICP备15000265号

皖公网安备 34110302000106号