通用Linux服务器备份网站和数据脚本 - backup.sh

[复制链接] |主动推送
查看21 | 回复0 | 2024-9-11 17:55:55 | 显示全部楼层 |阅读模式
无论我们使用哪个主机商,都需要注意的数据安全备份。我们可以每月备份一次,或者每周备份一次,或者每天备份。且我们可以备份的形式有很多种,比如我们可以每天手动备份、服务器环境自带的第三方对象存储备份,或者备份到邮箱中。甚至我们可以用这里通用的脚本备份。
毕竟不是所有的WEB环境都有自动备份的软件支持。这里我们可以用这个备份脚本 backup.sh 实现快速备份。
#!/bin/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/vpser.net" "/home/wwwroot/lnmp.org")######~Set MySQL Database you want to backup~######Backup_Database=("lnmp" "vpser")######~Set MySQL UserName and password~######MYSQL_UserName='root'MYSQL_PassWord='yourrootpassword'######~Enable Ftp Backup~######Enable_FTP=0# 0: enable; 1: disable######~Set FTP Information~######FTP_Host='1.2.3.4'FTP_Username='vpser.net'FTP_Password='yourftppassword'FTP_Dir="backup"#Values Setting END!TodayWWWBackup=www-*-$(date +"%Y%m%d").tar.gzTodayDBBackup=db-*-$(date +"%Y%m%d").sqlOldWWWBackup=www-*-$(date -d -3day +"%Y%m%d").tar.gzOldDBBackup=db-*-$(date -d -3day +"%Y%m%d").sqlBackup_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 1fiif [ ! -d ${Backup_Home} ]; then      mkdir -p ${Backup_Home}fiif [ ${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."; }fiecho "Backup website files..."for dd in ${Backup_Dir[@]};do    Backup_Dir ${dd}doneecho "Backup Databases..."for db in ${Backup_Database[@]};do    Backup_Sql ${db}doneecho "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}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则