#!/usr/bin/env bash
# 文件名: minio_backup.sh

### 变量定义 开始
readonly RSYNC_AUTH_USER='gitee'
readonly RSYNC_PORT='873'
readonly RSYNC_AUTH_PASS='790a002393c3e022d50a917a3d26fafedf842'
readonly RSYNC_AUTH_FILE='/etc/rsyncd.auth'
readonly BACKUP_DIR='/data/backup/minio/mirror'
readonly ARCHIVE_DIR='/data/backup/minio/archive'
readonly DATE_FORMAT=$(date +"%Y-%m-%d")
readonly KEEP_DAY=$(date -d "-3 days" +%Y-%m-%d)
declare -A dic
dic=(
    [minio_0]="10.223.132.200"
    [minio_1]="10.223.132.202"
    [minio_2]="10.223.132.201"
    [minio_3]="10.223.132.199"
)

### 变量定义 结束

# 创建所需目录
[[ -d ${BACKUP_DIR} ]] || install -d ${BACKUP_DIR}
[[ -d ${ARCHIVE_DIR} ]] || install -d ${ARCHIVE_DIR}

export RSYNC_PASSWORD=${RSYNC_AUTH_PASSWORD}

for module in $(echo ${!dic[*]})
do
    ip=${dic[$module]} && rsync \
                       -avrtopg \
                       --port=${PORT:-873} \
                       --progress \
                       --delete \
                       ${AUTH_USER}@${ip}::${module} ${BACKUP_DIR}/${module} &
done
wait

# 归档压缩
cd ${BACKUP_DIR} && tar -cf - . | pigz  > ${ARCHIVE_DIR}/${DATE_FORMAT}.tar.gz || tar acvf ${ARCHIVE_DIR}/${DATE_FORMAT}.tar.gz .

# 删除旧备份数据
find ${ARCHIVE_DIR} -type f -name "${KEEP_DAY}.tar.gz" -exec rm -f {} \;

