环境

CentOS Linux release 7.9.2009 (Core)
Confluence 7.12.0
Jira 8.16.1

备份

由 cp -a 代替 rsync,在数据量很大时,rsync -a 经常出现上班时间仍未复制完成,仍在进行复制。
远端同步时挂载,同步后卸载 nfs。
未成功挂载 nfs 时,则不进行 复制 本地本地备份 到 远端操作(实际为本地),避免重复数据。
远端备份采用 rsync 同步,每次与最新版本地备份 local_latest 镜像。

backup_jira_wiki.sh

#!/bin/bash
#Backup for Jira and Confluence.
REMOTE_IP=IP地址
REMOTE_DIR=/data/jira_wiki_backup
 
MYSQL_ACCOUNT=root
MYSQL_PASSWORD=root
MYSQL_DB_NAME_JIRA=jira
MYSQL_DB_NAME_CONFLUENCE=confluence
 
SRC_INSTALL_DIR=/opt/atlassian
SRC_HOME_DIR=/var/atlassian/application-data
SRC_MYSQL_CONF=/etc/my.cnf
 
BACKUP_DIR=/backup
BACKUP_DIR_LOG=${BACKUP_DIR}/log
BACKUP_DIR_LOCAL=${BACKUP_DIR}/local
BACKUP_DIR_REMOTE=${BACKUP_DIR}/remote
 
BACKUP_DATE=`date +'%Y%m%d'`
BACKUP_DIR_LOCAL_BACKUP_DATE=${BACKUP_DIR_LOCAL}/${BACKUP_DATE}
BACKUP_DIR_LOG_BACKUP_DATE=${BACKUP_DIR_LOG}/backup.log
 
RSYNC_LOG=${BACKUP_DIR_LOG}/rsync.log
 
if [ ! -d ${BACKUP_DIR_LOG} ];then
    mkdir -p ${BACKUP_DIR_LOG}
fi
if [ ! -d ${BACKUP_DIR_LOCAL_BACKUP_DATE} ];then
    mkdir -p ${BACKUP_DIR_LOCAL_BACKUP_DATE}
fi
if [ ! -d ${BACKUP_DIR_REMOTE} ];then
    mkdir -p ${BACKUP_DIR_REMOTE}
fi
 
echo -e '\n\n' >> ${BACKUP_DIR_LOG_BACKUP_DATE}
echo "`date +'%Y-%m-%d %H:%M'` Backup start." >> ${BACKUP_DIR_LOG_BACKUP_DATE}
 
# 本地备份
# 备份 安装程序 程序家目录 到本地
echo "`date +'%Y-%m-%d %H:%M'` Backup SRC_INSTALL_DIR and SCR_HOME_DIR" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
 
cp -a ${SRC_INSTALL_DIR} ${BACKUP_DIR_LOCAL_BACKUP_DATE}/ &> /dev/null
if [ $? -eq 0 ];then
    echo "`date +'%Y-%m-%d %H:%M'` Backup SRC_INSTALL_DIR Successful!" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
else
    echo "`date +'%Y-%m-%d %H:%M'` Backup SRC_INSTALL_DIR Failed!" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
fi
 
cp -a ${SRC_HOME_DIR} ${BACKUP_DIR_LOCAL_BACKUP_DATE}/ &> /dev/null
if [ $? -eq 0 ];then
    echo "`date +'%Y-%m-%d %H:%M'` Backup SRC_HOME_DIR Successful!" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
else
    echo "`date +'%Y-%m-%d %H:%M'` Backup SRC_HOME_DIR Failed!" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
fi
 
# 备份 数据库实例 数据库配置文件 到本地
echo "`date +'%Y-%m-%d %H:%M'` Backup Database" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
 
mysqldump -u${MYSQL_ACCOUNT} -p${MYSQL_PASSWORD} ${MYSQL_DB_NAME_JIRA} > ${BACKUP_DIR_LOCAL_BACKUP_DATE}/${MYSQL_DB_NAME_JIRA}.sql &> /dev/null
if [ $? -eq 0 ];then
    echo "`date +'%Y-%m-%d %H:%M'` Backup Jira Database Successful!" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
else
    echo "`date +'%Y-%m-%d %H:%M'` Backup Jira Database Failed!" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
fi
 
mysqldump -u${MYSQL_ACCOUNT}  -p${MYSQL_PASSWORD} ${MYSQL_DB_NAME_CONFLUENCE} -R > ${BACKUP_DIR_LOCAL_BACKUP_DATE}/${MYSQL_DB_NAME_CONFLUENCE}.sql &> /dev/null
if [ $? -eq 0 ];then
    echo "`date +'%Y-%m-%d %H:%M'` Backup Confluence Database Successful!" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
else
    echo "`date +'%Y-%m-%d %H:%M'` Backup Confluence Database Failed!" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
fi
 
echo "`date +'%Y-%m-%d %H:%M'` Backup my.cnf" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
cp -a  ${SRC_MYSQL_CONF} ${BACKUP_DIR_LOCAL_BACKUP_DATE}/ &> /dev/null
if [ $? -eq 0 ];then
    echo "`date +'%Y-%m-%d %H:%M'` Backup my.cnf Successful!" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
else
    echo "`date +'%Y-%m-%d %H:%M'` Backup my.cnf Failed!" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
fi
echo "`date +'%Y-%m-%d %H:%M'` Backup end." >> ${BACKUP_DIR_LOG_BACKUP_DATE}
 
# 更新 软链 local_latest
cd ${BACKUP_DIR_LOCAL}
ln -snf ${BACKUP_DIR_LOCAL}/"$(ls -1t | head -n 1)" ${BACKUP_DIR}/local_latest &> /dev/null
 
# NFS 挂载 RSYNC 同步 最新本地备份 到 远程
echo "`date +'%Y-%m-%d %H:%M'` Mount BACKUP_DIR_REMOTE" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
mount -t nfs ${REMOTE_IP}:${REMOTE_DIR} ${BACKUP_DIR_REMOTE} &>> ${RSYNC_LOG}
if [ $? -eq 0 ];then
 
    echo "`date +'%Y-%m-%d %H:%M'` Mount BACKUP_DIR_REMOTE Successful!" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
 
    echo "`date +'%Y-%m-%d %H:%M'` Backup BACKUP_DIR_LOCAL to BACKUP_DIR_REMOTE" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
    rsync -av --ignore-existing --delete ${BACKUP_DIR}/local_latest/  ${BACKUP_DIR_REMOTE}/ &>> ${RSYNC_LOG}
    if [ $? -eq 0 ];then
        echo "`date +'%Y-%m-%d %H:%M'` Backup Local to Remote Successful!" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
    else
        echo "`date +'%Y-%m-%d %H:%M'` Backup Local to Remote Failed!" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
    fi
 
    echo "`date +'%Y-%m-%d %H:%H'` UMount BACKUP_DIR_REMOTE..." >> ${BACKUP_DIR_LOG_BACKUP_DATE}
    umount -l ${BACKUP_DIR_REMOTE} &>> ${RSYNC_LOG}
    if [ $? -eq 0 ];then
        echo "`date +'%Y-%m-%d %H:%M'` UMount BACKUP_DIR_REMOTE Successful!" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
    else
        echo "`date +'%Y-%m-%d %H:%M'` UMount BACKUP_DIR_REMOTE Failed!" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
    fi
 
else
    echo "`date +'%Y-%m-%d %H:%M'` Mount BACKUP_DIR_REMOTE Failed!" >> ${BACKUP_DIR_LOG_BACKUP_DATE}
fi

本地备份删除

以保留最新数量的备份文件方式,删除多余备份文件;不以过期时间定义是否删除备份文件;避免未备份成功的情况下,备份文件仍被删除。
本地保留 4 份。

keep_num_latest_files.sh

#!/bin/bash
#Keep the latest number of files.
 
KEEP_NUM=4
DEL_DATE=`date +'%Y%m%d'`
WORK_PATH=/backup/local
LOG_PATH=/backup/log
LOG_FILE_NAME=delete.log
LOG=${LOG_PATH}/${LOG_FILE_NAME}
 
if [ ! -d ${WORK_PATH} ];then
    mkdir -p ${WORK_PATH}
fi
if [ ! -d ${LOG_PATH} ];then
    mkdir -p ${LOG_PATH}
fi
 
echo -e '\n\n' >> ${LOG}
echo ${DEL_DATE} >> ${LOG}
 
cd ${WORK_PATH}
echo "The following files in the working path will be deleted:\n" >> ${LOG}
ls -1tr | head -n -${KEEP_NUM} | grep -v "${LOG_FILE_NAME}" >> ${LOG}
ls -1tr | head -n -${KEEP_NUM} | grep -v "${LOG_FILE_NAME}" | xargs -d '\n' rm -rf &> /dev/null

远程 本地备份

最新备份:/data/jira_wiki_backup/
上次备份:/data/jira_wiki_backup_old/

remote_local_backup.sh

#!/bin/bash
#远程备份后 远程机器本地备份 两份备份。
 
SRC_PATH=/data/jira_wiki_backup
DST_PATH=/data/jira_wiki_backup_old
RSYNC_LOG=/data/rsync.log
rsync -av --ignore-existing --delete ${SRC_PATH}/  ${DST_PATH}/ &>> ${RSYNC_LOG}
最后修改:2022 年 01 月 17 日 07 : 05 PM
如果觉得文章帮助了您,您可以随意赞赏。