lnmp官方自带的有backup.sh脚本,但是该脚本只能上传ftp,不能发送邮件,稍微修改了下,然后可以自己发送到指定邮箱,省事了,定时任务需要cron配合。

安装mutt

yum install -y mutt

创建.muttrc

set envelope_from=yes
set from="backup@hmilyld.com"
set realname="hmilyld"
set use_from=yes

backup.sh

#!/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=("网站路径1" "网站路径2")

######~Set MySQL Database you want to backup~######
Backup_Database=("数据库1" "数据库2")

######~Set MySQL UserName and password~######
MYSQL_UserName='root'
MYSQL_PassWord='mysql密码'

######~Enable Ftp Backup~######
Enable_FTP=1
# 0: enable; 1: disable
######~Set FTP Information~######
FTP_Host='1.2.3.4'
FTP_Username='vpser.net'
FTP_Password='yourftppassword'
FTP_Dir="backup"

######~Enable EMAIL Backup~######
Enable_EMAIL=0
# 0: enable; 1: disable
######~Set Email Information~######
TOEMAIL="邮件地址"
SUBJECT=BlogBak_$(date +"%Y%m%d")

#Values Setting END!

TodayWWWBackup=www-*-$(date +"%Y%m%d").tar.gz
TodayDBBackup=db-*-$(date +"%Y%m%d").sql
OldWWWBackup=www-*-$(date -d -1day +"%Y%m%d").tar.gz
OldDBBackup=db-*-$(date -d -1day +"%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}www-${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

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

if [ ${Enable_EMAIL} = 0 ]; then
    type mutt >/dev/null 2>&1 || { echo >&2 "mutt command not found. Install: centos:yum install mutt,debian/ubuntu:apt-get install mutt."; }
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

if [ ${Enable_EMAIL} = 0 ]; then
    echo "Uploading backup files to Email..."
    cd ${Backup_Home}
    rm -f backup.tar.gz
    tar zcf backup.tar.gz *
    BACKFILE=${Backup_Home}backup.tar.gz
    echo "backup" | mutt -s ${SUBJECT} ${TOEMAIL} -a ${BACKFILE}
    echo "complete."
fi 

定时任务

00 01 * * * backup.sh

标签: 脚本, linux, 服务器

添加新评论