LNMP网站文件、数据库自动备份脚本
文章标签:
linux
先新建一个文件 /home/backup/my.cnf (该文件路径可随意,路径可在下面脚本代码中修改)
文件内容,填写root密码
[client] user = root password = 123456
以下是修改后的脚本,增加了sql备份文件压缩功能
#!/usr/bin/env bash #Funciont: Backup website and mysql database #Author: licess #Website: https://lnmp.org #IMPORTANT!!!Please Setting the following Values! Backup_Home="/home/backup/" MySQL_Dump="/usr/local/mysql/bin/mysqldump" ######~Set Directory you want to backup~###### #Backup_Dir=("/home/wwwroot/abc" "/home/wwwroot/123") #多个文件夹可以这样 Backup_Dir=("/home/wwwroot/abc") ######~Set MySQL Database you want to backup~###### #多个数据库方法同上 Backup_Database=("abc") ######~Enable Ftp Backup~###### Enable_FTP=0 # 0: enable; 1: disable ######~Set FTP Information~###### FTP_Host='2.2.2.2' FTP_Username='zhangsan' FTP_Password='123456' FTP_Dir="backup" #Values Setting END! TodayWWWBackup=www-*-$(date +"%Y%m%d").tar.gz TodayDBBackup=db-*-$(date +"%Y%m%d").sql.tar.gz OldWWWBackup=www-*-$(date -d -3day +"%Y%m%d").tar.gz OldDBBackup=db-*-$(date -d -3day +"%Y%m%d").sql.tar.gz Backup_Dir() { Backup_Path=$1 Dir_Name=`echo ${Backup_Path##*/}` Pre_Dir=`echo ${Backup_Path}|sed 's/'${Dir_Name}'//g'` tar zcf ${Backup_Home}www-${Dir_Name}-$(date +"%Y%m%d").tar.gz -C ${Pre_Dir} ${Dir_Name} } Backup_Sql() { NewDBFile=${Backup_Home}db-$1-$(date +"%Y%m%d").sql ${MySQL_Dump} --defaults-file=/home/backup/my.cnf $1 > ${NewDBFile} tar zcf ${Backup_Home}db-$1-$(date +"%Y%m%d").sql.tar.gz -C ${Backup_Home} db-$1-$(date +"%Y%m%d").sql rm -f ${NewDBFile} } 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 if [ ${Enable_FTP} = 0 ]; then type lftp >/dev/null 2>&1 || { echo >&2 "lftp command not found. Install: centos:yum install lftp,debian/ubuntu:apt-get install lftp."; } fi 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
登录后可发表评论
点击登录