admin

基于国人lnmp一键安装包的一键备份
简单修改了一下,可以一键备份mysql和wwwroot所有数据  以及nginx和php的配置 并且删除7天之前的...
扫描右侧二维码阅读全文
03
2018/08

基于国人lnmp一键安装包的一键备份

简单修改了一下,可以一键备份mysql和wwwroot所有数据  以及nginx和php的配置 并且删除7天之前的备份。当然也和原版一样支持 ftp,支持计划任务

原版可以参考lnmp目录里面的 tools/backup.sh

先安装依赖包,计划任务和lftp
编辑计划任务

yum install vixie-cron crontabs lftp
chkconfig crond on
service crond start  
crontab -e

添加一行
0 3 * /root/backup.sh
注意北京时间和vps时差

下面内容存到 /root/backup.sh
然后

chmod 777 /root/backup.sh
#!/bin/bash

#Funciont: Backup website and mysql database
#Author: licess
#Website: https://lnmp.org
#Edit by:leiyanhui.com

#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" "/usr/local/nginx/conf" "/usr/local/php/etc")

######~Set MySQL Database you want to backup~######
Backup_Database=("--all-databases")

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

######~Enable Ftp Backup~######
Enable_FTP=0
# 0: enable; 1: disable
######~Set FTP Information~######
FTP_Host='ftp.leiyanhui.com'
FTP_Username='ftpusername'
FTP_Password='yourftppassword'
FTP_Dir="/myfolder/back-for-vps"

#Values Setting END!

TodayWWWBackup=www-*-$(date +"%Y%m%d").tar.gz
TodayDBBackup=db-*-$(date +"%Y%m%d").sql

OldWWWBackup=www-*-$(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}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

echo "Backup website and conf 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
Last modification:October 16th, 2018 at 03:00 pm
If you think my article is useful to you, please feel free to appreciate

One comment

  1. 藏阴套图网

    认真拜读,好为学徒!

Leave a Comment

captcha
请输入验证码