前言

服务器中有一些比较重要项目,如配置文件、博客等。为防止服务器宕机或其他原因造成重要数据丢失无法找回,需要对服务器相关项目进行备份。

本文记录在 CentOS 7 下实现博客定期自动进行加密备份、自动上传备份文件到百度云。

实现基础

文件备份&加密

使用以下命令对文件/文件夹进行打包备份:

tar -zcf backup.tar.gz /dst/folder

backup.tar.gz是打包文件名。
/dst/folder是需要打包的文件/文件夹。

使用以下命令对文件/文件夹进行加密打包:

tar -zcf - /backup/file/name | openssl des3 -salt -k passwd | dd of=backupfilename.des3

/backup/file/name是需要加密打包的文件/文件夹。
passwd是加密密码。
backupfilename是打包文件名。
由于 tar 使用 openssl 进行加密,在执行操作前确保安装了 openssl。

博客数据库备份

使用以下命令备份相应数据库:

mysqldump -uUser -pPasswd databaseName>backup.sql

User为数据库用户名,一般只用 root 即可。
Passwd为用户名对应的密码。
databaseName为需要备份的数据库名

上传到百度云

百度云 Python 客户端 BYPY 可以在 Linux 下通过命令行使用百度云。
首先安装 pip:

yum install -y epel-release
yum install -y python-pip

然后安装 bypy:

pip install bypy

最后进行授权(登录百度云盘 ):
执行命令:

bypy info

用浏览器打开执行命令后输出的链接,复制授权码到命令行即可完成配置。
使用bypy upload命令即可上传文件/文件夹,通过命令行上传的文件在网盘/我的应用数据/bypy/目录下。

脚本

脚本参数解释:
BACKUP_DATE:备份时间
BACKUP_KEY:备份加密密码。
BACKUP_DIR:保存备份文件的路径。
SOURCE_DIR:需要备份的文件/文件夹所在路径。
SOURCE_FOLDER_FILE:需要备份的文件/文件夹。SOURCE_DIR + SOURCE_FOLDER_FILE 即为备份文件/文件夹的绝对路径
BACKUP_NAME:备份文件名
BACKUP_FOLDER_NAME:加密备份名(等于前缀+备份时间)。
DATEBASENAME:数据库名
DATEBASEUSR:数据库用户名
DATABASEPASSWD:数据库密码
NUMDAYS:备份保留的天数

备份时,首先打包博客文件、备份数据库,两者都放在名为BACKUP_FOLDER_NAME的文件夹中,然后进行加密打包BACKUP_FOLDER_NAME文件夹,最后通过 bypy 上传加密备份文件到百度云。

授予脚本可执行权限,通过crontab定时执行脚本,实现定期自动备份。

脚本内容如下:

#!/bin/bash

# Shell Script Alias Name
PRGNAME="ScriptName"

# Backup date format,e.g 20150505_2010
BACKUP_DATE=`date +%Y%m%d_%H%M`
# The backuup key
BACKUP_KEY='passwd'
# Following is the backup save directory
BACKUP_DIR=/usr/backups
# Following directory is the directory to be backuped 
SOURCE_DIR=/var/www
# The following folder/file under SOURCE_DIR will be backuped
SOURCE_FOLDER_FILE=folder
# Following is backup name of dir/file
BACKUP_FOLDER_NAME="aliyun1_chevereto_$BACKUP_DATE"
BACKUP_NAME="BackupName"
# database options, when DATEBASE='NULL' datebase will not be backup
# database name
DATEBASENAME="DataBaseName"
# database user
DATEBASEUSR="user"
# database passwd
DATABASEPASSWD="passwd"

# Number of days to keep
NUMDAYS=60

# Some linux command
FINDCMD="find"
TARCMD="tar -zcf"


function USAGE() {
cat << EOF
usage: $0 options

This script backs up the blog programs(or other files you want).

OPTIONS:
  -h    Show this message
  -a    Backup all files
  -l    Databases to backup (space seperated)
  -n    Number of days to keep backups
EOF
}

while getopts "hal:n:" opt; do
  case $opt in
    a)
      PRGNAME=""
      ;;
    h)
      USAGE
      exit 1
      ;;
    l)
      PRGNAME="$OPTARG"
      ;;
    n)
      NUMDAYS=$OPTARG
      ;;
    \?)
      USAGE
      exit
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

function ERROR() {
  echo && echo "[error] $@"
  exit 1
}

function NOTICE() {
  echo && echo "[notice] $@"
}

function RUNCMD() {
  echo $@
  eval $@
}

# Sanity checks
if [ ! -n "$BACKUP_DIR" ]; then
  ERROR "Invalid backup directory"
fi

if [[ ! $NUMDAYS =~ ^[0-9]+$ ]]; then
  ERROR "Invalid number of days: $NUMDAYS"
elif [ "$NUMDAYS" -eq "0" ]; then
  ERROR "Number of days must be greater than zero"
fi

# Lock down permissions
umask 077

# Create directory if needed
RUNCMD mkdir -p -v $BACKUP_DIR

if [ ! -d $BACKUP_DIR ]; then
  ERROR "Invalid directory: $BACKUP_DIR"
fi

RC=0

# make the backupfile folder
RUNCMD "cd $BACKUP_DIR && mkdir $BACKUP_FOLDER_NAME"
# backup datebase 
if [ $DATEBASENAME != "NULL" ];then
  NOTICE "Backuping database..."
  RUNCMD "cd $BACKUP_DIR/$BACKUP_FOLDER_NAME && mysqldump -u$DATEBASEUSR -p$DATABASEPASSWD $DATEBASENAME>$DATEBASENAME.sql"
fi
# backup files
NOTICE "Backuping files..."
RUNCMD "cd $SOURCE_DIR && $TARCMD $BACKUP_NAME.tar.gz $SOURCE_FOLDER_FILE && mv $BACKUP_NAME.tar.gz $BACKUP_DIR/$BACKUP_FOLDER_NAME"
# encrypt the backups
NOTICE "Encrypting backups..."
RUNCMD "cd $BACKUP_DIR && $TARCMD - $BACKUP_FOLDER_NAME | openssl des3 -salt -k $BACKUP_KEY | dd of=$BACKUP_FOLDER_NAME.des3" 
# delete the backupfile folder
RUNCMD "cd $BACKUP_DIR && rm -rf $BACKUP_FOLDER_NAME"

RC=$?

if [ $RC -gt 0 ]; then
  ERROR "Backup failed!"
  if [ $MAILNOTIC == "true" ]; then
  {
    echo "Backup time: `date "+%Y-%m-%d %H:%M:%S"`"
    echo "Backup file: $BACKUP_FOLDER_NAME.des3"
  } | mail -s "PRGNAME backup failed on `hostname -s`"  example.email.com
  fi
else
  NOTICE "Removing dumps older than $NUMDAYS days..."
  RUNCMD "$FINDCMD $BACKUP_DIR -name \"*.des3\" -type f -mtime +$NUMDAYS -print0 | xargs -0 rm -fv"

  NOTICE "Listing backup directory contents..."
  RUNCMD ls -la $BACKUP_DIR

  NOTICE "Dumping Programs is complete!"
# cloud backup
  NOTICE "Uploading the backups..."
  RUNCMD "cd $BACKUP_DIR && bypy upload  $BACKUP_FOLDER_NAME.des3"
fi

# exit 0

Last modification:June 2nd, 2020 at 10:48 am