如何把网站和重要数据定时自动备份到Nas群晖上?

Admin 2023-10-2 468 10/2

1.在网站服务器上安装同步服务

CentOS系统:

sudo yum install rsync

 

Debian或者Ubuntu 系统:

sudo apt install rsync

 

如果无法执行上面的安装命令,那么建议更新下你服务器的系统环境,在终端下执行以下命令:

yum install epel-release #CentOS
apt install epel-release #Debian

 

2.在群晖后台安装非常强大的免费套件 Active Back for Business

如何把网站和重要数据定时自动备份到Nas群晖上?

3.如果你是宝塔面板安装的网站环境,那么可以在其后台进程备份

4.如果你是自定义安装的服务器环境,那么可以通过以下脚本进行打包备份网站:(把下面的代码另存为 backup.sh )

#!/usr/bin/env bash



Backup_Home="/home/backup/"
MySQL_Dump="/usr/local/mysql/bin/mysqldump"

Backup_Dir=("/home/wwwroot/网站1" "/home/wwwroot/网站2")


Backup_Database=("数据库1" "数据库2")


MYSQL_UserName='数据库用户名'
MYSQL_PassWord='数据库密码'



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

最后添加定时执行上面的脚本

输入命令:

crontab -e

然后点击 i 切换到输入模式

最后输入 每隔1天晚上3点执行任务:

0 3 */1 * * /root/backup.sh

- THE END -
最后修改:2023年10月2日

非特殊说明,本博所有文章均为博主原创。