这个是脚本是我自己一直在用的,非常好用,定时把网站程序和网站数据库备份并上传到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 或下载本文件
链接: https://pan.baidu.com/s/1vaeJWyhzm2BCf_V0zcXuMQ 密码: 95jj
#!/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
以上(脚本摘自lnmp.org)仅是Linux系统才可以使用,如果是windows系统可以用护卫神备份工具以及睿备份都可以
定时执行脚本请看这篇文章Linux定时重启系统或服务
原创文章,转载请注明: 转载自荣耀博客
本文链接地址: 定时备份网站程序和MySQL数据库到FTP
本文链接地址: 定时备份网站程序和MySQL数据库到FTP