FROM ubuntu:22.04
ARG DEBIAN_FRONTEND=noninteractive

# ---------- 基础工具 ----------
RUN apt-get update && apt-get install -y --no-install-recommends \
        curl wget ca-certificates gnupg2 lsb-release \
        postgresql-client \
        sqlite3 \
        pigz pv \
        unzip \
        netcat-openbsd \
    && rm -rf /var/lib/apt/lists/*

# ---------- AWS CLI v2 ----------
RUN set -e \
    && curl -fsSL "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o /tmp/awscliv2.zip \
    && unzip -q /tmp/awscliv2.zip -d /tmp/ \
    && /tmp/aws/install \
    && rm -rf /tmp/awscliv2.zip /tmp/aws

# ---------- age 加密工具 v1.2.0 ----------
RUN set -e \
    && AGE_VERSION=v1.2.0 \
    && wget -q -O /tmp/age.tar.gz \
       "https://github.com/FiloSottile/age/releases/download/${AGE_VERSION}/age-${AGE_VERSION}-linux-amd64.tar.gz" \
    && tar -xf /tmp/age.tar.gz -C /tmp/ \
    && install -m 755 /tmp/age/age /usr/local/bin/age \
    && install -m 755 /tmp/age/age-keygen /usr/local/bin/age-keygen \
    && rm -rf /tmp/age*

# ---------- Percona XtraBackup 8.0 ----------
RUN set -e \
    && DISTRO=$(lsb_release -cs) \
    && wget -q -O /tmp/percona-release.deb \
       "https://repo.percona.com/apt/percona-release_latest.${DISTRO}_all.deb" \
    && dpkg -i /tmp/percona-release.deb \
    && percona-release setup pxb80 \
    && apt-get update && apt-get install -y --no-install-recommends percona-xtrabackup-80 \
    && rm /tmp/percona-release.deb \
    && rm -rf /var/lib/apt/lists/*

# ---------- 备份脚本 ----------
COPY scripts/ /scripts/
COPY entrypoint.sh /entrypoint.sh
COPY crontab.txt /etc/cron.d/pangolin-backup

RUN chmod +x /scripts/*.sh /entrypoint.sh \
    && chmod 0644 /etc/cron.d/pangolin-backup \
    && mkdir -p /backup /var/log/backup

# cron 日志直接输出到 stdout/stderr
RUN touch /var/log/backup/cron.log

ENTRYPOINT ["/entrypoint.sh"]
