mysql 物理备份
#!/usr/bin/bash
DBUSER=root
DBPASSWORD=123
BACKUPDIR=/dback
HISTLOG=/tmp/history.log
RULE=(0 1 2 1 2 2 2) # 0 full 1 diff 2 incr
DATE=$(date "+%Y-%m-%d_%H-%M-%S")
logdev=local2
full="innobackupex --user=$DBUSER --password=$DBPASSWORD $BACKUPDIR"
diff="innobackupex --user=$DBUSER --password=$DBPASSWORD --incremental $BACKUPDIR --incremental-basedir=${BACKUPDIR}/"
envCheck(){
# 检查 MySQL 连接
mysql -u$DBUSER -p$DBPASSWORD -e "select user()" &>/dev/null
if [ $? -ne 0 ];then
logger -p ${logdev}.error "MySQL 连接异常" && exit 2
fi
# 检查备份目录
if [ ! -e $BACKUPDIR ];then
logger -p ${logdev}.error "备份目录未正常初始化" && \
mkdir $BACKUPDIR
fi
}
work(){
datetime=$[`date "+%u"`-1]
case ${RULE[$datetime]} in
0)
# 移动历史备份文件日志
mv $HISTLOG $BACKUPDIR/ && \
# 打包历史备份文件
tar cvzf /tmp/${DATE}.tar.gz $BACKUPDIR && \
logger -p ${logdev}.info "历史备份文件打包成功 /tmp/${DATE}.tar.gz" && \
# 删除原备份目录和日志文件,重新创建备份目录
rm -rf $BACKUPDIR $HISTLOG && mkdir $BACKUPDIR || \
logger -p ${logdev}.warn "历史备份文件打包异常 ${DATE}"
# 执行完整备份
eval $full && \
# 记录完整备份日志
echo "full `ls $BACKUPDIR |tail -n1`" >> $HISTLOG && \
logger -p ${logdev}.info "完整备份完成 `ls $BACKUPDIR |tail -n1`" || \
logger -p ${logdev}.error "完整备份异常 ${DATE}"
;;
1)
# 获取上一次完整备份的目录
BEFOREDIR=$(awk '$1 ~ /full/{print $2}' $HISTLOG)
# 执行差异备份
eval $diff${BEFOREDIR} && \
# 记录差异备份日志
echo "diff `ls $BACKUPDIR |tail -n1`" >> $HISTLOG && \
logger -p ${logdev}.info "差异备份完成 `ls $BACKUPDIR |tail -n1`" || \
logger -p ${logdev}.error "差异备份异常 ${DATE}"
;;
2)
# 获取上一次备份的类型
BEFOREDIR=$(tail -n1 $HISTLOG|awk '{print $2}')
# 执行增量备份
eval $diff${BEFOREDIR} && \
# 记录增量备份日志
echo "incr `ls $BACKUPDIR |tail -n1`" >> $HISTLOG && \
logger -p ${logdev}.info "增量备份完成 `ls $BACKUPDIR |tail -n1`" || \
logger -p ${logdev}.error "增量备份异常 ${DATE}"
;;
*)
echo "异常操作"
;;
esac
}
# 调用函数
envCheck
work
mysql 物理备份2
#!/usr/bin/bash
<<!
mysql 物理备份脚本
工具:xtrabackup 2.3.6
备份策略:周日完整备份 周一、二、四、五增量备份 周三、六差异备份
1、完全后台执行
2、备份完整
3、日志记录
4、历史数据的处理
!
dbUser="root"
dbPasswd="QianFeng@123"
backCMD="/usr/bin/innobackupex"
backDir="/backup"
logPath="/var/log/backup.log"
# 定义规则数组 0 完整备份 1 增量备份 2 差异备份
# 数组的值是备份的方式,数组的下标是星期几
rule=(0 1 1 2 1 1 2)
# 0 1 2 3 4 5 6
datetime=$(date +%w)
full(){
# 移动之前的日志
mv $logPath $backDir
# 压缩之前的备份
tar cvzf /tmp/mysql_back_`date +%F`.tar.gz "$backDir" && \
rm -rf $backDir
# 创建备份目录结构
mkdir -p $backDir/{full,incr,diff}
# 执行完整备份
$backCMD --user=$dbUser --password=$dbPasswd $backDir/full
# 记录备份日志
echo "full `ls $backDir/full |tail -n 1`" >> $logPath
}
other(){
# $1 incr|diff 备份的类型
# $2 需要基于的完整路径
$backCMD --user=$dbUser --password=$dbPasswd --incremental $backDir/$1 --incremental-basedir=$2
# 记录备份日志
echo "$3 `ls $backDir/$3 |tail -n 1`" >> $logPath
}
case ${rule[$datetime]} in
0)
full
;;
1)
# 获取上一次备份的类型
type=$(tail -n 1 $logPath|awk '{print $1}')
# 获取上一次备份的目录
time=$(tail -n 1 $logPath|awk '{print $2}')
# 执行增量备份
other incr $backDir/$type/$time incr
;;
2)
# 获取上一次完整备份的目录
time=$(grep full $logPath|awk '{print $2}')
# 执行差异备份
other diff $backDir/full/$time diff
;;
esac
redis 数据备份
#!/bin/bash
# Redis 数据备份脚本
# Redis 服务器地址和端口
REDIS_HOST="localhost"
REDIS_PORT="6379"
# 备份保存路径
BACKUP_DIR="/path/to/backup"
# 备份文件名前缀
BACKUP_PREFIX="redis_backup"
# 获取当前日期
DATE=$(date +"%Y%m%d_%H%M%S")
# 创建备份目录
mkdir -p "$BACKUP_DIR"
# 备份 Redis 数据
redis-cli -h $REDIS_HOST -p $REDIS_PORT SAVE
# 将备份文件保存到指定目录
BACKUP_FILE="$BACKUP_DIR/$BACKUP_PREFIX-$DATE.rdb"
mv dump.rdb "$BACKUP_FILE"
# 清理过期备份,只保留最近 N 天的备份
DAYS_TO_KEEP=7
find $BACKUP_DIR -type f -name "$BACKUP_PREFIX*" -mtime +$DAYS_TO_KEEP -exec rm {} \;
echo "Redis 数据备份完成,备份文件保存在: $BACKUP_FILE"
elasticsearch 数据备份
#!/bin/bash
# Elasticsearch 快照备份脚本
# Elasticsearch 服务器信息
ES_HOST="localhost"
ES_PORT="9200"
ES_USER="es"
ES_PASSWORD="123"
# 快照存储库设置
REPO_NAME="hello"
SNAPSHOT_NAME="snapshot_$(date +%Y%m%d%H%M%S)"
# 备份目录
BACKUP_DIR="/opt/es/backup"
# 日志文件
LOG_FILE="/var/log/es_snapshot_backup.log"
# 如果目录不存在,则创建备份目录
mkdir -p "$BACKUP_DIR"
# Elasticsearch 快照 API URL
SNAPSHOT_API_URL="http://$ES_HOST:$ES_PORT/_snapshot/$REPO_NAME/$SNAPSHOT_NAME"
# 记录日志消息的函数
log() {
echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
}
# 检查 Elasticsearch 是否可访问
curl -s -k --user "$ES_USER:$ES_PASSWORD" "$ES_HOST:$ES_PORT" >/dev/null
if [ $? -ne 0 ]; then
log "错误:无法连接到 Elasticsearch,请检查 Elasticsearch 设置。"
exit 1
fi
# 创建快照
log "创建 Elasticsearch 快照:$SNAPSHOT_NAME"
curl -s -k --user "$ES_USER:$ES_PASSWORD" -X PUT "$SNAPSHOT_API_URL" >> "$LOG_FILE" 2>&1
# 检查快照是否成功创建
if [ $? -eq 0 ]; then
log "快照创建成功。"
# 将快照移动到备份目录
log "将快照移动到备份目录:$BACKUP_DIR"
mv "$SNAPSHOT_NAME" "$BACKUP_DIR/"
log "备份完成。快照存储在:$BACKUP_DIR/$SNAPSHOT_NAME"
else
log "错误:快照创建失败,请检查 Elasticsearch 日志以获取详细信息。"
fi
etcd 数据备份
#!/bin/bash
# etcd 数据库备份脚本
# etcd 集群连接信息
ETCD_ENDPOINTS="http://127.0.0.1:2379"
# 备份目录
BACKUP_DIR="/opt/etcd/backup"
# 备份文件名称
BACKUP_FILE="etcd_backup_$(date +'%Y%m%d%H%M%S').db"
# 日志文件
LOG_FILE="/var/log/etcd_backup.log"
# 创建备份目录
mkdir -p "$BACKUP_DIR"
# 记录日志消息的函数
log() {
echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
}
# 检查 etcdctl 是否可用
if ! command -v etcdctl >/dev/null; then
log "错误:etcdctl 未找到,请确保已安装 etcdctl。"
exit 1
fi
# 检查 etcd 连接
etcdctl --endpoints="$ETCD_ENDPOINTS" endpoint health &>/dev/null
if [ $? -ne 0 ]; then
log "错误:无法连接到 etcd,请检查 etcd 设置。"
exit 1
fi
# 备份 etcd 数据库
log "开始备份 etcd 数据库到目录:$BACKUP_DIR"
etcdctl --endpoints="$ETCD_ENDPOINTS" snapshot save "$BACKUP_DIR/$BACKUP_FILE" &>> "$LOG_FILE"
# 检查备份是否成功
if [ $? -eq 0 ]; then
log "etcd 数据库备份成功。备份文件存储在:$BACKUP_DIR/$BACKUP_FILE"
else
log "错误:etcd 数据库备份失败,请检查 etcdctl 输出以获取详细信息。"
fi
日志备份
#!/bin/bash
# 备份 /var/log/ 目录下所有10天之前的日志到 /tmp/logbak 目录中,并打包压缩
# 备份源目录
SOURCE_DIR="/var/log/"
# 目标备份目录
BACKUP_DIR="/tmp/logbak/"
# 备份文件名
BACKUP_FILE="log_backup_$(date +'%Y%m%d%H%M%S').tar.gz"
# 保留备份天数
RETENTION_DAYS=7
# 创建备份目录
mkdir -p "$BACKUP_DIR"
# 记录日志
LOG_FILE="/var/log/log_backup.log"
# 清理过期备份函数
cleanup_old_backups() {
find "$BACKUP_DIR" -name "log_backup_*" -type f -mtime +$RETENTION_DAYS -exec rm {} \;
}
# 备份操作
backup_logs() {
log_files=$(find "$SOURCE_DIR" -type f -mtime +$RETENTION_DAYS)
if [ -z "$log_files" ]; then
echo "没有需要备份的日志文件。"
exit 0
fi
tar czf "$BACKUP_DIR/$BACKUP_FILE" $log_files
}
# 执行备份
backup_logs
# 清理过期备份
cleanup_old_backups
# 记录日志
echo "$(date +'%Y-%m-%d %H:%M:%S') - 日志备份完成。备份文件:$BACKUP_DIR/$BACKUP_FILE" >> "$LOG_FILE"
gitlab 数据备份
#!/bin/bash
# 备份 GitLab 仓库数据脚本
# GitLab 安装路径
GITLAB_PATH="/var/opt/gitlab"
# 备份目标路径
BACKUP_DIR="/tmp/gitlab_backup"
# GitLab 备份工具路径
GITLAB_BACKUP_TOOL="$GITLAB_PATH/bin/gitlab-rake gitlab:backup:create"
# 备份文件名
BACKUP_FILE="gitlab_backup_$(date +'%Y%m%d%H%M%S').tar.gz"
# 保留备份天数
RETENTION_DAYS=7
# 创建备份目录
mkdir -p "$BACKUP_DIR"
# 清理过期备份函数
cleanup_old_backups() {
find "$BACKUP_DIR" -name "gitlab_backup_*" -type f -mtime +$RETENTION_DAYS -exec rm {} \;
}
# 备份操作
backup_gitlab() {
$GITLAB_BACKUP_TOOL
}
# 执行备份
backup_gitlab
# 清理过期备份
cleanup_old_backups
# 压缩备份文件
tar czf "$BACKUP_DIR/$BACKUP_FILE" "$GITLAB_PATH/backups"
# 记录日志
LOG_FILE="/var/log/gitlab_backup.log"
echo "$(date +'%Y-%m-%d %H:%M:%S') - GitLab 备份完成。备份文件:$BACKUP_DIR/$BACKUP_FILE" >> "$LOG_FILE"