使用Docker定时备份数据

文章目录


一、Docker镜像制作

bash 复制代码
# https://download.samba.org/pub/rsync/rsyncd.conf.5

port 873
uid = nobody
gid = nobody
use chroot = yes
max connections = 5
syslog facility = local5
pid file = /var/run/rsyncd.pid
read only = true

[backup]
path = /home/backup
  • root
bash 复制代码
# min   hour    day     month   weekday command
0       *       *       *       *       run-parts /home/1hour
0       0       *       *       *       run-parts /home/0Hday
0       1       *       *       *       run-parts /home/1Hday
0       2       *       *       *       run-parts /home/2Hday
0       3       *       *       *       run-parts /home/3Hday
0       5       *       *       *       run-parts /home/5Hday
0       4       *       *       6       run-parts /home/1week
  • Dockerfile
bash 复制代码
FROM alpine:3.19.1
WORKDIR /home

COPY root /var/spool/cron/crontabs/root
COPY rsyncd.conf /home
COPY mc /home
RUN chmod +x mc

RUN mkdir 1hour && mkdir 0Hday && mkdir 1Hday && mkdir 2Hday && mkdir 3Hday && mkdir 5Hday && mkdir 1week
RUN echo "http://mirrors.aliyun.com/alpine/v3.19/community" > /etc/apk/repositories
RUN echo "http://mirrors.aliyun.com/alpine/v3.19/main" >> /etc/apk/repositories
RUN apk update && apk upgrade
RUN apk --no-cache add bash mysql-client rsync tzdata
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo "Asia/Shanghai" > /etc/timezone
  • 镜像制作
bash 复制代码
cd /home/dockerimg/crontab
docker build --tag crontab:1.0 .

二、MySQL数据备份

bash 复制代码
#!/bin/sh

log_save_days=30						# 备份日志保留几天
log_path=/home/logs						# 备份日志路径
log_file=backup-data-mysql.log			# 备份日志名称

data_path=/home/backup/mysql			# 备份路径
data_dir=$(date +%Y%m)					# 备份数据目录
mysql_path=/home/mysql/data				# mysql-bin.*日志存放目录
mysql_bin=mysql-bin.index				# mysql-bin.index位置
mysql_addr=192.168.1.6					# mysql地址
mysql_port=3306							# mysql端口
mysql_user=root							# mysql用户名
mysql_password=12345678					# mysql密码

data_dir_before=$(date -d@"$(( `date +%s`-86400))" +"%Y%m")
mkdir -p /home/crontab/logs
mkdir -p $data_path/$data_dir
mkdir -p $data_path/$data_dir_before

echo `` >> $log_path/$(date +%Y%m)-$log_file
echo "备份开始时间: "$(date +%F%n%T) >> $log_path/$(date +%Y%m)-$log_file
mysqladmin -h$mysql_addr -P$mysql_port -u$mysql_user -p$mysql_password flush-logs
count=`wc -l $mysql_path/$mysql_bin |awk '{print $1}'`
num=0
for file in `cat $mysql_path/$mysql_bin`
do
    base=`basename $file`
    num=`expr $num + 1`
    if [ $num  -ne $count ] && (! test -e $data_path/$data_dir/$base.log.gz) && (! test -e $data_path/$data_dir_before/$base.log.gz)
    then
		gzip -c $mysql_path/$base > $data_path/$data_dir/$base.log.gz
		echo "备份: "$data_path/$data_dir/$base.log.gz >> $log_path/$(date +%Y%m)-$log_file
	fi
done
echo "备份结束时间: "$(date +%F%n%T) >> $log_path/$(date +%Y%m)-$log_file

find $log_path -type f -name "*$log_file" -mtime +$log_save_days -exec rm -rf {} \;

三、Minio数据备份

bash 复制代码
#!/bin/sh

log_save_days=30						# 备份日志保留几天
log_path=/home/logs						# 备份日志路径
log_file=backup-data-minio.log			# 备份日志名称

data_path=/home/backup/minio			# 备份路径
data_dir=$(date +%Y%m)					# 备份数据目录
minio_dir=$(date +%Y%m%d)				# minio存储 月 $(date +%Y%m) 天 $(date +%Y%m%d)
minio_addr=http://192.168.1.6:9000		# minio地址
minio_user=admin						# minio用户名
minio_password=123456					# minio密码
minio_bucket=bucket						# minio桶名称

mkdir -p /home/crontab/logs
mkdir -p $data_path/$data_dir
mkdir $data_path/$data_dir/tmp

echo `` >> $log_path/$(date +%Y%m)-$log_file
echo "备份开始时间: "$(date +%F%n%T) >> $log_path/$(date +%Y%m)-$log_file
/home/mc alias set minio_backup $minio_addr $minio_user $minio_password

minio_bucket=test
/home/mc cp --recursive minio_backup/$minio_bucket/$minio_dir $data_path/$data_dir/tmp
cd $data_path/$data_dir/tmp
tar -zcf $data_path/$data_dir/$minio_bucket-$minio_dir.tar.gz *
rm -rf $data_path/$data_dir/tmp
echo "备份: "$data_path/$data_dir/$minio_bucket-$minio_dir.tar.gz  >> $log_path/$(date +%Y%m)-$log_file

echo "备份结束时间: "$(date +%F%n%T) >> $log_path/$(date +%Y%m)-$log_file

find $log_path -type f -name "*$log_file" -mtime +$log_save_days -exec rm -rf {} \;

四、数据跨服务器传输

bash 复制代码
#!/bin/sh

crond -f -l 8
bash 复制代码
#!/bin/sh

rsync --daemon --config=/home/rsyncd.conf
crond -f -l 8
  • rsyncd.secrets
bash 复制代码
root:password
bash 复制代码
#!/bin/sh
# 只传输变化部分、支持断点续传、同步服务器都需要安装rsync

log_save_days=30						# 日志保留几天
log_path=/home/logs						# 日志路径
log_file=backup-data-copy.log			# 日志名称

echo `` >> $log_path/$(date +%Y%m)-$log_file
echo "数据传输开始时间: "$(date +%F%n%T) >> $log_path/$(date +%Y%m)-$log_file
rsync -avzP --password-file=/home/rsyncd.secrets root@192.168.6.12::backup /home/backup
echo "数据传输结束时间: "$(date +%F%n%T) >> $log_path/$(date +%Y%m)-$log_file

find $log_path -type f -name "*$log_file" -mtime +$log_save_days -exec rm -rf {} \;

五、Nginx日志分割

bash 复制代码
#!/bin/sh

log_save_days=30					# 备份日志保留几天
log_path=/home/logs					# 备份日志路径
log_file=backup-log-nginx.log		# 备份日志名称

data_save_days=60					# 备份保留几天
data_path=/home/nginx/logs			# 备份路径

mkdir -p /home/crontab/logs
mkdir -p /home/nginx/logs/access
mkdir -p /home/nginx/logs/error

echo `` >> $log_path/$(date +%Y%m)-$log_file
echo "备份开始时间: "$(date +%F%n%T) >> $log_path/$(date +%Y%m)-$log_file
for item in "access" "error"
do
  gzip -c $data_path/$item.log > $data_path/$item/$(date -d@"$(( `date +%s`-3600))" +"%Y-%m-%d")_$item.log.gz
  echo "备份: "$data_path/$item/$(date -d@"$(( `date +%s`-3600))" +"%Y-%m-%d")_$item.log.gz >> $log_path/$(date +%Y%m)-$log_file
  rm -rf $data_path/$item.log && touch $data_path/$item.log
done
find $data_path/$item -type f -name "*.gz" -mtime +$data_save_days -exec rm -rf {} \;

echo "备份结束时间: "$(date +%F%n%T) >> $log_path/$(date +%Y%m)-$log_file

find $log_path -type f -name "*$log_file" -mtime +$log_save_days -exec rm -rf {} \;

六、Docker启动

  • /home/docker/crontab

  • 数据库服务器

    docker-crontab-data.yaml

bash 复制代码
version: "3"

services:
  crontab:
    image: "crontab:1.0"
    restart: always
    container_name: ser-crontab
    hostname: crontab
    ports:
      - 873:873
    networks:
      - apphost
    environment:
      TZ: "Asia/Shanghai"
    volumes:
      - /home/docker/crontab/backup-log-nginx.sh:/home/0Hday/backup-log-nginx.sh
      - /home/docker/crontab/backup-data-mysql.sh:/home/1Hday/backup-data-mysql.sh
      - /home/docker/crontab/backup-data-minio.sh:/home/2Hday/backup-data-minio.sh
      - /home/docker/crontab/cronddata.sh:/home/command.sh
      - /home/backup:/home/backup
      - /home/docker/logs:/home/logs
      - /home/nginx/logs:/home/nginx/logs
      - /home/mysql/data:/home/mysql/data
    command: >
       sh -c "/home/command.sh"

networks:
  apphost:
  • 数据备份服务器
    docker-crontab-base.yaml
bash 复制代码
version: "3"

services:
  crontab:
    image: "crontab:1.0"
    restart: always
    container_name: ser-crontab
    hostname: crontab
    networks:
      - apphost
    environment:
      TZ: "Asia/Shanghai"
    volumes:
      - /home/docker/crontab/backup-log-nginx.sh:/home/0Hday/backup-log-nginx.sh
      - /home/docker/crontab/backup-data-copy.sh:/home/3Hday/backup-data-copy.sh
      - /home/docker/crontab/rsyncd.secrets:/home/rsyncd.secrets
      - /home/docker/crontab/crondbase.sh:/home/command.sh
      - /home/backup:/home/backup
      - /home/docker/logs:/home/logs
      - /home/nginx/logs:/home/nginx/logs
    command: >
       sh -c "/home/command.sh"

networks:
  apphost:
  • 数据库服务器启动
bash 复制代码
mkdir -p /home/docker/crontab && mkdir -p /home/docker/logs
cd /home/docker/crontab && chmod 777 *.* && chmod 600 rsyncd.secrets
docker-compose -f /home/docker/crontab/docker-crontab-data.yaml -p crontab up -d
  • 数据备份服务器启动
bash 复制代码
mkdir -p /home/docker/crontab && mkdir -p /home/docker/logs
cd /home/docker/crontab && chmod 777 *.* && chmod 600 rsyncd.secrets
docker-compose -f /home/docker/crontab/docker-crontab-base.yaml -p crontab up -d

七、Docker备份日志

  • /home/docker/logs/202404-backup-data-copy.log
bash 复制代码
数据传输开始时间: 2024-04-12 03:00:00
数据传输结束时间: 2024-04-12 03:00:00
  • /home/docker/logs/202404-backup-log-nginx.log
bash 复制代码
备份开始时间: 2024-04-12 00:00:00
备份: /home/nginx/logs/access/2024-04-11_access.log.gz
备份: /home/nginx/logs/error/2024-04-11_error.log.gz
备份结束时间: 2024-04-12 00:00:00
  • /home/docker/logs/202404-backup-data-minio.log
bash 复制代码
备份开始时间: 2024-04-12 02:00:00
备份: /home/backup/minio/202404/test-20240412.tar.gz
备份结束时间: 2024-04-12 02:00:00
  • /home/docker/logs/202404-backup-data-mysql.log
bash 复制代码
备份开始时间: 2024-04-12 01:00:00
备份: /home/backup/mysql/202404/mysql-bin.000041.log.gz
备份结束时间: 2024-04-12 01:00:03
相关推荐
wenyue112144 分钟前
Revolutionize Your Kubernetes Experience with Easegress: Kubernetes Gateway API
容器·kubernetes·gateway
yunfanleo1 小时前
docker run m3e 配置网络,自动重启,GPU等 配置渠道要点
linux·运维·docker
梅见十柒3 小时前
wsl2中kali linux下的docker使用教程(教程总结)
linux·经验分享·docker·云原生
O&REO5 小时前
单机部署kubernetes环境下Overleaf-基于MicroK8s的Overleaf应用部署指南
云原生·容器·kubernetes
运维小文6 小时前
K8S资源限制之LimitRange
云原生·容器·kubernetes·k8s资源限制
登云时刻6 小时前
Kubernetes集群外连接redis集群和使用redis-shake工具迁移数据(二)
redis·容器·kubernetes
wuxingge14 小时前
k8s1.30.0高可用集群部署
云原生·容器·kubernetes
志凌海纳SmartX15 小时前
趋势洞察|AI 能否带动裸金属 K8s 强势崛起?
云原生·容器·kubernetes
锅总15 小时前
nacos与k8s service健康检查详解
云原生·容器·kubernetes
BUG弄潮儿16 小时前
k8s 集群安装
云原生·容器·kubernetes