这个是脚本是我自己一直在用的,非常好用,定时把网站程序和网站数据库备份并上传到FTP,备份数据保存7天;当你经历过数据丢失,就知道数据备份的重要性。
参数解释
Backup_Home:设置本机备份文件的路径
MySQL_Dump:MySql mysqldump导出工具路径
Backup_Dir:网站程序路径
Backup_Database:备份网站的数据库名
MYSQL_UserName:数据库账户
MYSQL_PassWord:数据库密码
FTP_Host:ftp地址
FTP_Username:FTP登陆账户
FTP_Password:FTP登陆密码
FTP_Dir:设置备份到ftp的路径
先运行以下命令,免得报错。
CentOS:yum install lftp 或 Debian/Ubuntu:apt-get install lftp
复制以下内容另存为backup.sh
#!/bin/bash
#server ip : 8.8.8.8
#Funciont: Backup website and mysql database
#IMPORTANT!!!Please Setting the following Values!
Backup_Home="/home/wwwroot/"
MySQL_Dump="/home/server/mysql/bin/mysqldump"
######~Set Directory you want to backup~######
Backup_Dir=("/home/wwwroot/web1" "/home/www/web2")
######~Set MySQL Database you want to backup~######
Backup_Database=("web1data" "web2data")
######~Set MySQL UserName and password~######
MYSQL_UserName='root'
MYSQL_PassWord='root'
######~Enable Ftp Backup~######
Enable_FTP=0
# 0: enable; 1: disable
######~Set FTP Information~######
FTP_Host='8.8.8.8'
FTP_Username='ftpuser'
FTP_Password='ftppwd'
FTP_Dir="/backupdir/"
#Values Setting END!
TodayWWWBackup=web-*-$(date +"%Y%m%d").tar.gz
TodayDBBackup=db-*-$(date +"%Y%m%d").sql
OldWWWBackup=web-*-$(date -d -7day +"%Y%m%d").tar.gz
OldDBBackup=db-*-$(date -d -7day +"%Y%m%d").sql
Backup_Dir()
{
Backup_Path=$1
Dir_Name=`echo ${Backup_Path##*/}`
Pre_Dir=`echo ${Backup_Path}|sed 's/'${Dir_Name}'//g'`
tar zcf ${Backup_Home}web-${Dir_Name}-$(date +"%Y%m%d").tar.gz -C ${Pre_Dir} ${Dir_Name}
}
Backup_Sql()
{
${MySQL_Dump} -u$MYSQL_UserName -p$MYSQL_PassWord $1 > ${Backup_Home}db-$1-$(date +"%Y%m%d").sql
}
if [ ! -f ${MySQL_Dump} ]; then
echo "mysqldump command not found.please check your setting."
exit 1
fi
if [ ! -d ${Backup_Home} ]; then
mkdir -p ${Backup_Home}
fi
type lftp >/dev/null 2>&1 || { echo >&2 "lftp command not found. Install: centos:yum install lftp,debian/ubuntu:apt-get install lftp."; }
echo "Backup website files..."
for dd in ${Backup_Dir[@]};do
Backup_Dir ${dd}
done
echo "Backup Databases..."
for db in ${Backup_Database[@]};do
Backup_Sql ${db}
done
echo "Delete old backup files..."
rm -f ${Backup_Home}${OldWWWBackup}
rm -f ${Backup_Home}${OldDBBackup}
if [ ${Enable_FTP} = 0 ]; then
echo "Uploading backup files to ftp..."
cd ${Backup_Home}
lftp ${FTP_Host} -u ${FTP_Username},${FTP_Password} << EOF
cd ${FTP_Dir}
mrm ${OldWWWBackup}
mrm ${OldDBBackup}
mput ${TodayWWWBackup}
mput ${TodayDBBackup}
bye
EOF
echo "complete."
fi
以上仅是Linux系统才可以使用。
日期:2018年02月01日 16:17:16 星期四 分类:
好文分享 浏览(34181)
本文地址:https://www.blogs.hk/post-3446.html [
百度已收录]
声明:本页信息由网友自行发布或来源于网络,真实性、合法性由发布人负责,请仔细甄别!本站只为传递信息,我们不做任何双方证明,也不承担任何法律责任。文章内容若侵犯你的权益,请联系本站删除!
留言咨询