From 000ddb992569bf5595afb885a698d03d6488b05d Mon Sep 17 00:00:00 2001 From: Viacheslav Sarzhan Date: Sat, 5 Oct 2024 19:43:04 +0300 Subject: [PATCH 1/6] use aws-cli --- percona-xtradb-cluster-8.0-backup/Dockerfile | 26 +++++++++---------- .../lib/pxc/backup.sh | 15 +++++++---- .../recovery-cloud.sh | 12 ++++----- .../run_backup.sh | 6 ++--- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/percona-xtradb-cluster-8.0-backup/Dockerfile b/percona-xtradb-cluster-8.0-backup/Dockerfile index ba4f38b6..7997c9fb 100644 --- a/percona-xtradb-cluster-8.0-backup/Dockerfile +++ b/percona-xtradb-cluster-8.0-backup/Dockerfile @@ -32,13 +32,17 @@ LABEL org.opencontainers.image.version=${PXC_VERSION} # check repository package signature in secure way RUN set -ex; \ export GNUPGHOME="$(mktemp -d)"; \ - gpg --batch --keyserver keyserver.ubuntu.com --recv-keys 4D1BB29D63D98E422B2113B19334A25F8507EFA5 3E6D826D3FBAB389C2F38E34BC4D06A08D8B756F; \ + gpg --batch --keyserver keyserver.ubuntu.com --recv-keys 4D1BB29D63D98E422B2113B19334A25F8507EFA5 3E6D826D3FBAB389C2F38E34BC4D06A08D8B756F FB5DB77FD5C118B80511ADA8A6310ACC4672475C; \ gpg --batch --export --armor 4D1BB29D63D98E422B2113B19334A25F8507EFA5 > ${GNUPGHOME}/PERCONA-PACKAGING-KEY; \ gpg --batch --export --armor 3E6D826D3FBAB389C2F38E34BC4D06A08D8B756F > ${GNUPGHOME}/RPM-GPG-KEY-oracle; \ - rpmkeys --import ${GNUPGHOME}/PERCONA-PACKAGING-KEY ${GNUPGHOME}/RPM-GPG-KEY-oracle; \ + gpg --batch --export --armor FB5DB77FD5C118B80511ADA8A6310ACC4672475C > ${GNUPGHOME}/RPM-GPG-KEY-aws; \ + rpmkeys --import ${GNUPGHOME}/PERCONA-PACKAGING-KEY ${GNUPGHOME}/RPM-GPG-KEY-oracle ${GNUPGHOME}/RPM-GPG-KEY-aws; \ microdnf install -y findutils; \ curl -Lf -o /tmp/percona-release.rpm https://repo.percona.com/yum/percona-release-latest.noarch.rpm; \ rpmkeys --checksig /tmp/percona-release.rpm; \ + curl -o /tmp/awscliv2.zip https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip; \ + curl -o /tmp/awscliv2.sig https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip.sig; \ + gpg --verify /tmp/awscliv2.sig /tmp/awscliv2.zip; \ rpm -i /tmp/percona-release.rpm; \ rm -rf "$GNUPGHOME" /tmp/percona-release.rpm; \ rpm --import /etc/pki/rpm-gpg/PERCONA-PACKAGING-KEY @@ -66,9 +70,13 @@ RUN set -ex; \ iputils \ procps-ng \ util-linux \ + unzip \ findutils; \ microdnf clean all; \ - rm -rf /var/cache/dnf /var/cache/yum + unzip -q /tmp/awscliv2.zip -d /tmp/; \ + /tmp/aws/install; \ + aws --version; \ + rm -rf /var/cache/dnf /var/cache/yum /tmp/awscliv2.zip /tmp/awscliv2.sig /tmp/aws # create mysql user/group before mysql installation RUN groupadd -g 1001 mysql; \ @@ -90,7 +98,8 @@ RUN set -ex; \ COPY LICENSE /licenses/LICENSE.Dockerfile RUN cp /usr/share/doc/percona-xtrabackup-80/LICENSE /licenses/LICENSE.xtrabackup; \ - cp /usr/share/doc/percona-xtradb-cluster-garbd-3/COPYING /licenses/LICENSE.garbd + cp /usr/share/doc/percona-xtradb-cluster-garbd-3/COPYING /licenses/LICENSE.garbd; \ + curl -o /licenses/LICENSE.aws-cli https://raw.githubusercontent.com/aws/aws-cli/refs/heads/master/LICENSE.txt RUN set -ex; \ curl -o /usr/bin/kubectl -LO \ @@ -103,15 +112,6 @@ RUN set -ex; \ RUN install -d -o 1001 -g 0 -m 0775 /backup; \ mkdir /usr/lib/pxc -ENV MC_VERSION=RELEASE.2024-07-08T20-59-24Z -ENV MC_SHA256SUM=e111d2b4bea05aadbffaa3fc8d2436a3fefedf030cd1318568bccb72810024f0 -RUN set -ex; \ - curl -o /usr/bin/mc -O https://dl.minio.io/client/mc/release/linux-amd64/archive/mc.${MC_VERSION} \ - && chmod +x /usr/bin/mc \ - && echo "${MC_SHA256SUM} /usr/bin/mc" | sha256sum -c - \ - && curl -o /licenses/LICENSE.mc \ - https://raw.githubusercontent.com/minio/mc/${MC_VERSION}/LICENSE - COPY lib/pxc /usr/lib/pxc COPY recovery-*.sh run_backup.sh backup.sh get-pxc-state /usr/bin/ diff --git a/percona-xtradb-cluster-8.0-backup/lib/pxc/backup.sh b/percona-xtradb-cluster-8.0-backup/lib/pxc/backup.sh index a68c904d..4ae2cc1a 100755 --- a/percona-xtradb-cluster-8.0-backup/lib/pxc/backup.sh +++ b/percona-xtradb-cluster-8.0-backup/lib/pxc/backup.sh @@ -4,11 +4,14 @@ set -o errexit SST_INFO_NAME=sst_info XBCLOUD_ARGS="--curl-retriable-errors=7 $XBCLOUD_EXTRA_ARGS" +export AWS_SHARED_CREDENTIALS_FILE='/tmp/aws-credfile' +export AWS_ENDPOINT_URL="${ENDPOINT:-https://s3.amazonaws.com}" INSECURE_ARG="" + if [ -n "$VERIFY_TLS" ] && [[ $VERIFY_TLS == "false" ]]; then - INSECURE_ARG="--insecure" - XBCLOUD_ARGS="${INSECURE_ARG} ${XBCLOUD_ARGS}" + AWS_S3_NO_VERIFY_SSL='--no-verify-ssl' + XBCLOUD_ARGS="--insecure ${XBCLOUD_ARGS}" fi S3_BUCKET_PATH=${S3_BUCKET_PATH:-$PXC_SERVICE-$(date +%F-%H-%M)-xtrabackup.stream} @@ -28,15 +31,17 @@ is_object_exist() { local bucket="$1" local object="$2" - if [[ -n "$(mc -C /tmp/mc ${INSECURE_ARG} --json ls "dest/$bucket/$object" | jq '.status')" ]]; then + aws $AWS_S3_NO_VERIFY_SSL s3api head-object --bucket $bucket --key "$object" || NOT_EXIST=true + if [[ -z "$NOT_EXIST" ]]; then return 1 fi } mc_add_bucket_dest() { - echo "+ mc -C /tmp/mc ${INSECURE_ARG} config host add dest ${ENDPOINT:-https://s3.amazonaws.com} ACCESS_KEY_ID SECRET_ACCESS_KEY " +# echo "+ mc -C /tmp/mc ${INSECURE_ARG} config host add dest ${ENDPOINT:-https://s3.amazonaws.com} ACCESS_KEY_ID SECRET_ACCESS_KEY " { set +x; } 2>/dev/null - mc -C /tmp/mc ${INSECURE_ARG} config host add dest "${ENDPOINT:-https://s3.amazonaws.com}" "$ACCESS_KEY_ID" "$SECRET_ACCESS_KEY" + aws configure set aws_access_key_id "$ACCESS_KEY_ID" + aws configure set aws_secret_access_key "$SECRET_ACCESS_KEY" set -x } diff --git a/percona-xtradb-cluster-8.0-backup/recovery-cloud.sh b/percona-xtradb-cluster-8.0-backup/recovery-cloud.sh index c63caab7..77748810 100755 --- a/percona-xtradb-cluster-8.0-backup/recovery-cloud.sh +++ b/percona-xtradb-cluster-8.0-backup/recovery-cloud.sh @@ -9,20 +9,20 @@ LIB_PATH='/usr/lib/pxc' # temporary fix for PXB-2784 XBCLOUD_ARGS="--curl-retriable-errors=7 $XBCLOUD_EXTRA_ARGS" - -MC_ARGS='-C /tmp/mc' +export AWS_SHARED_CREDENTIALS_FILE='/tmp/aws-credfile' +export AWS_ENDPOINT_URL="${ENDPOINT:-https://s3.amazonaws.com}" if [ -n "$VERIFY_TLS" ] && [[ $VERIFY_TLS == "false" ]]; then XBCLOUD_ARGS="--insecure ${XBCLOUD_ARGS}" - MC_ARGS="${MC_ARGS} --insecure" + AWS_S3_NO_VERIFY_SSL='--no-verify-ssl' fi if [ -n "$S3_BUCKET_URL" ]; then { set +x; } 2>/dev/null - echo "+ mc ${MC_ARGS} config host add dest ${ENDPOINT:-https://s3.amazonaws.com} ACCESS_KEY_ID SECRET_ACCESS_KEY" - mc ${MC_ARGS} config host add dest "${ENDPOINT:-https://s3.amazonaws.com}" "$ACCESS_KEY_ID" "$SECRET_ACCESS_KEY" + aws configure set aws_access_key_id "$ACCESS_KEY_ID" + aws configure set aws_secret_access_key "$SECRET_ACCESS_KEY" set -x - mc ${MC_ARGS} ls "dest/${S3_BUCKET_URL}" + aws $AWS_S3_NO_VERIFY_SSL s3 ls "${S3_BUCKET_URL}" elif [ -n "${BACKUP_PATH}" ]; then XBCLOUD_ARGS="${XBCLOUD_ARGS} --storage=azure" fi diff --git a/percona-xtradb-cluster-8.0-backup/run_backup.sh b/percona-xtradb-cluster-8.0-backup/run_backup.sh index 95fea234..7b92dc88 100755 --- a/percona-xtradb-cluster-8.0-backup/run_backup.sh +++ b/percona-xtradb-cluster-8.0-backup/run_backup.sh @@ -125,9 +125,9 @@ backup_s3() { FIRST_RECEIVED=1 fi - mc -C /tmp/mc stat ${INSECURE_ARG} "dest/$S3_BUCKET/$S3_BUCKET_PATH.md5" - md5_size=$(mc -C /tmp/mc stat ${INSECURE_ARG} --json "dest/$S3_BUCKET/$S3_BUCKET_PATH.md5" | sed -e 's/.*"size":\([0-9]*\).*/\1/') - if [[ $md5_size =~ "Object does not exist" ]] || ((md5_size < 23000)); then + aws $AWS_S3_NO_VERIFY_SSL s3 ls s3://$S3_BUCKET/$S3_BUCKET_PATH.md5 + md5_size=$(aws $AWS_S3_NO_VERIFY_SSL --output json s3api list-objects --bucket "$S3_BUCKET" --prefix "$S3_BUCKET_PATH.md5" --query 'Contents[0].Size' | sed -e 's/.*"size":\([0-9]*\).*/\1/') + if [[ $md5_size =~ "Object does not exist" ]] || ((md5_size < 23000)) ; then log 'ERROR' 'Backup is empty' log 'ERROR' 'Backup was finished unsuccessfull' exit 1 From 31d3835992d95f4fcd795a59cc8880d13a68978a Mon Sep 17 00:00:00 2001 From: Viacheslav Sarzhan Date: Tue, 8 Oct 2024 12:43:17 +0300 Subject: [PATCH 2/6] aws lib --- .../lib/pxc/aws.sh | 29 +++++++++++++++++++ .../lib/pxc/backup.sh | 25 ++-------------- .../recovery-cloud.sh | 7 ++--- .../run_backup.sh | 1 + 4 files changed, 35 insertions(+), 27 deletions(-) create mode 100755 percona-xtradb-cluster-8.0-backup/lib/pxc/aws.sh diff --git a/percona-xtradb-cluster-8.0-backup/lib/pxc/aws.sh b/percona-xtradb-cluster-8.0-backup/lib/pxc/aws.sh new file mode 100755 index 00000000..493b62d5 --- /dev/null +++ b/percona-xtradb-cluster-8.0-backup/lib/pxc/aws.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +set -o errexit + +export AWS_SHARED_CREDENTIALS_FILE='/tmp/aws-credfile' +export AWS_ENDPOINT_URL="${ENDPOINT:-https://s3.amazonaws.com}" +export AWS_REGION="${DEFAULT_REGION:-us-west-2}" + +if [ -n "$VERIFY_TLS" ] && [[ $VERIFY_TLS == "false" ]]; then + AWS_S3_NO_VERIFY_SSL='--no-verify-ssl' +fi + +is_object_exist() { + local bucket="$1" + local object="$2" + + aws $AWS_S3_NO_VERIFY_SSL s3api head-object --bucket $bucket --key "$object" || NOT_EXIST=true + if [[ -z "$NOT_EXIST" ]]; then + return 1 + fi +} + +mc_add_bucket_dest() { + { set +x; } 2>/dev/null + aws configure set aws_access_key_id "$ACCESS_KEY_ID" + aws configure set aws_secret_access_key "$SECRET_ACCESS_KEY" + set -x +} + diff --git a/percona-xtradb-cluster-8.0-backup/lib/pxc/backup.sh b/percona-xtradb-cluster-8.0-backup/lib/pxc/backup.sh index 4ae2cc1a..0476b218 100755 --- a/percona-xtradb-cluster-8.0-backup/lib/pxc/backup.sh +++ b/percona-xtradb-cluster-8.0-backup/lib/pxc/backup.sh @@ -2,15 +2,14 @@ set -o errexit +LIB_PATH='/usr/lib/pxc' +. ${LIB_PATH}/aws.sh + SST_INFO_NAME=sst_info XBCLOUD_ARGS="--curl-retriable-errors=7 $XBCLOUD_EXTRA_ARGS" -export AWS_SHARED_CREDENTIALS_FILE='/tmp/aws-credfile' -export AWS_ENDPOINT_URL="${ENDPOINT:-https://s3.amazonaws.com}" - INSECURE_ARG="" if [ -n "$VERIFY_TLS" ] && [[ $VERIFY_TLS == "false" ]]; then - AWS_S3_NO_VERIFY_SSL='--no-verify-ssl' XBCLOUD_ARGS="--insecure ${XBCLOUD_ARGS}" fi @@ -27,24 +26,6 @@ log() { set -x } -is_object_exist() { - local bucket="$1" - local object="$2" - - aws $AWS_S3_NO_VERIFY_SSL s3api head-object --bucket $bucket --key "$object" || NOT_EXIST=true - if [[ -z "$NOT_EXIST" ]]; then - return 1 - fi -} - -mc_add_bucket_dest() { -# echo "+ mc -C /tmp/mc ${INSECURE_ARG} config host add dest ${ENDPOINT:-https://s3.amazonaws.com} ACCESS_KEY_ID SECRET_ACCESS_KEY " - { set +x; } 2>/dev/null - aws configure set aws_access_key_id "$ACCESS_KEY_ID" - aws configure set aws_secret_access_key "$SECRET_ACCESS_KEY" - set -x -} - clean_backup_s3() { mc_add_bucket_dest diff --git a/percona-xtradb-cluster-8.0-backup/recovery-cloud.sh b/percona-xtradb-cluster-8.0-backup/recovery-cloud.sh index 77748810..3993b0f4 100755 --- a/percona-xtradb-cluster-8.0-backup/recovery-cloud.sh +++ b/percona-xtradb-cluster-8.0-backup/recovery-cloud.sh @@ -6,21 +6,18 @@ set -o xtrace LIB_PATH='/usr/lib/pxc' . ${LIB_PATH}/check-version.sh . ${LIB_PATH}/vault.sh +. ${LIB_PATH}/aws.sh # temporary fix for PXB-2784 XBCLOUD_ARGS="--curl-retriable-errors=7 $XBCLOUD_EXTRA_ARGS" -export AWS_SHARED_CREDENTIALS_FILE='/tmp/aws-credfile' -export AWS_ENDPOINT_URL="${ENDPOINT:-https://s3.amazonaws.com}" if [ -n "$VERIFY_TLS" ] && [[ $VERIFY_TLS == "false" ]]; then XBCLOUD_ARGS="--insecure ${XBCLOUD_ARGS}" - AWS_S3_NO_VERIFY_SSL='--no-verify-ssl' fi if [ -n "$S3_BUCKET_URL" ]; then { set +x; } 2>/dev/null - aws configure set aws_access_key_id "$ACCESS_KEY_ID" - aws configure set aws_secret_access_key "$SECRET_ACCESS_KEY" + mc_add_bucket_dest set -x aws $AWS_S3_NO_VERIFY_SSL s3 ls "${S3_BUCKET_URL}" elif [ -n "${BACKUP_PATH}" ]; then diff --git a/percona-xtradb-cluster-8.0-backup/run_backup.sh b/percona-xtradb-cluster-8.0-backup/run_backup.sh index 7b92dc88..5417db01 100755 --- a/percona-xtradb-cluster-8.0-backup/run_backup.sh +++ b/percona-xtradb-cluster-8.0-backup/run_backup.sh @@ -7,6 +7,7 @@ set -m LIB_PATH='/usr/lib/pxc' . ${LIB_PATH}/vault.sh . ${LIB_PATH}/backup.sh +. ${LIB_PATH}/aws.sh SOCAT_OPTS="TCP-LISTEN:4444,reuseaddr,retry=30" From 903ee38991d09faddce733234f7f15d84de6c1a1 Mon Sep 17 00:00:00 2001 From: Viacheslav Sarzhan Date: Tue, 8 Oct 2024 13:15:32 +0300 Subject: [PATCH 3/6] add post_backup script --- .../post_backup.sh | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 percona-xtradb-cluster-8.0-backup/post_backup.sh diff --git a/percona-xtradb-cluster-8.0-backup/post_backup.sh b/percona-xtradb-cluster-8.0-backup/post_backup.sh new file mode 100755 index 00000000..51fee552 --- /dev/null +++ b/percona-xtradb-cluster-8.0-backup/post_backup.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +set -o errexit +set -o xtrace +set -m + +LIB_PATH='/usr/lib/pxc' +. ${LIB_PATH}/vault.sh +. ${LIB_PATH}/backup.sh +. ${LIB_PATH/aws.sh + +handle_sigterm() { + log 'INFO' 'Post recv script was finished' + exit 0 +} + +backup_volume() { + log 'INFO' 'Checking backup in PVC' + cd "$BACKUP_DIR" + + stat xtrabackup.stream + if (($(stat -c%s xtrabackup.stream) < 5000000)); then + log 'ERROR' 'Backup is empty' + log 'ERROR' 'Backup was finished unsuccessfully' + exit 1 + fi + md5sum xtrabackup.stream | tee md5sum.txt +} + +backup_s3() { + log 'INFO' 'Checking backup in S3' + aws $AWS_S3_NO_VERIFY_SSL s3 ls s3://$S3_BUCKET/$S3_BUCKET_PATH.md5 + md5_size=$(aws $AWS_S3_NO_VERIFY_SSL --output json s3api list-objects --bucket "$S3_BUCKET" --prefix "$S3_BUCKET_PATH.md5" --query 'Contents[0].Size' | sed -e 's/.*"size":\([0-9]*\).*/\1/') + if [[ $md5_size =~ "Object does not exist" ]] || ((md5_size < 23000)) ; then + log 'ERROR' 'Backup is empty' + log 'ERROR' 'Backup was finished unsuccessfull' + exit 1 + fi +} + +backup_azure() { + log 'INFO' 'Checking backup in Azure' +} + +trap 'handle_sigterm' 15 + +if [ -n "$S3_BUCKET" ]; then + backup_s3 +elif [ -n "$AZURE_CONTAINER_NAME" ]; then + backup_azure +else + backup_volume +fi + +exit 0 From a76dc330d8b08fe58a42c9a8aaae72969281bf08 Mon Sep 17 00:00:00 2001 From: Viacheslav Sarzhan Date: Tue, 8 Oct 2024 13:16:27 +0300 Subject: [PATCH 4/6] fix build --- percona-xtradb-cluster-8.0-backup/Dockerfile | 2 +- percona-xtradb-cluster-8.0-backup/backup.sh | 163 +++++++++--------- .../run_backup.sh | 14 -- 3 files changed, 83 insertions(+), 96 deletions(-) diff --git a/percona-xtradb-cluster-8.0-backup/Dockerfile b/percona-xtradb-cluster-8.0-backup/Dockerfile index 7997c9fb..4b95c463 100644 --- a/percona-xtradb-cluster-8.0-backup/Dockerfile +++ b/percona-xtradb-cluster-8.0-backup/Dockerfile @@ -113,7 +113,7 @@ RUN install -d -o 1001 -g 0 -m 0775 /backup; \ mkdir /usr/lib/pxc COPY lib/pxc /usr/lib/pxc -COPY recovery-*.sh run_backup.sh backup.sh get-pxc-state /usr/bin/ +COPY recovery-*.sh run_backup.sh backup.sh post_backup.sh get-pxc-state /usr/bin/ VOLUME ["/backup"] USER 1001 diff --git a/percona-xtradb-cluster-8.0-backup/backup.sh b/percona-xtradb-cluster-8.0-backup/backup.sh index 60d434d2..3e59c99d 100755 --- a/percona-xtradb-cluster-8.0-backup/backup.sh +++ b/percona-xtradb-cluster-8.0-backup/backup.sh @@ -9,97 +9,98 @@ LIB_PATH='/usr/lib/pxc' GARBD_OPTS="" function get_backup_source() { - CLUSTER_SIZE=$(/opt/percona/peer-list -on-start=/usr/bin/get-pxc-state -service=$PXC_SERVICE 2>&1 \ - | grep wsrep_cluster_size \ - | sort \ - | tail -1 \ - | cut -d : -f 12) - - if [ -z "${CLUSTER_SIZE}" ]; then - exit 1 - fi - - FIRST_NODE=$(/opt/percona/peer-list -on-start=/usr/bin/get-pxc-state -service=$PXC_SERVICE 2>&1 \ - | grep wsrep_ready:ON:wsrep_connected:ON:wsrep_local_state_comment:Synced:wsrep_cluster_status:Primary \ - | sort -r \ - | tail -1 \ - | cut -d : -f 2 \ - | cut -d . -f 1) - - SKIP_FIRST_POD='|' - if ((${CLUSTER_SIZE:-0} > 1)); then - SKIP_FIRST_POD="$FIRST_NODE" - fi - /opt/percona/peer-list -on-start=/usr/bin/get-pxc-state -service=$PXC_SERVICE 2>&1 \ - | grep wsrep_ready:ON:wsrep_connected:ON:wsrep_local_state_comment:Synced:wsrep_cluster_status:Primary \ - | grep -v $SKIP_FIRST_POD \ - | sort \ - | tail -1 \ - | cut -d : -f 2 \ - | cut -d . -f 1 + CLUSTER_SIZE=$(/opt/percona/peer-list -on-start=/usr/bin/get-pxc-state -service=$PXC_SERVICE 2>&1 \ + | grep wsrep_cluster_size \ + | sort \ + | tail -1 \ + | cut -d : -f 12) + + if [ -z "${CLUSTER_SIZE}" ]; then + exit 1 + fi + + FIRST_NODE=$(/opt/percona/peer-list -on-start=/usr/bin/get-pxc-state -service=$PXC_SERVICE 2>&1 \ + | grep wsrep_ready:ON:wsrep_connected:ON:wsrep_local_state_comment:Synced:wsrep_cluster_status:Primary \ + | sort -r \ + | tail -1 \ + | cut -d : -f 2 \ + | cut -d . -f 1) + + SKIP_FIRST_POD='|' + if (( ${CLUSTER_SIZE:-0} > 1 )); then + SKIP_FIRST_POD="$FIRST_NODE" + fi + /opt/percona/peer-list -on-start=/usr/bin/get-pxc-state -service=$PXC_SERVICE 2>&1 \ + | grep wsrep_ready:ON:wsrep_connected:ON:wsrep_local_state_comment:Synced:wsrep_cluster_status:Primary \ + | grep -v $SKIP_FIRST_POD \ + | sort \ + | tail -1 \ + | cut -d : -f 2 \ + | cut -d . -f 1 } function check_ssl() { - CA=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt - if [ -f /var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt ]; then - CA=/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt - fi - SSL_DIR=${SSL_DIR:-/etc/mysql/ssl} - if [ -f ${SSL_DIR}/ca.crt ]; then - CA=${SSL_DIR}/ca.crt - fi - SSL_INTERNAL_DIR=${SSL_INTERNAL_DIR:-/etc/mysql/ssl-internal} - if [ -f ${SSL_INTERNAL_DIR}/ca.crt ]; then - CA=${SSL_INTERNAL_DIR}/ca.crt - fi - - KEY=${SSL_DIR}/tls.key - CERT=${SSL_DIR}/tls.crt - if [ -f ${SSL_INTERNAL_DIR}/tls.key -a -f ${SSL_INTERNAL_DIR}/tls.crt ]; then - KEY=${SSL_INTERNAL_DIR}/tls.key - CERT=${SSL_INTERNAL_DIR}/tls.crt - fi - - if [ -f "$CA" -a -f "$KEY" -a -f "$CERT" ]; then - GARBD_OPTS="socket.ssl_ca=${CA};socket.ssl_cert=${CERT};socket.ssl_key=${KEY};socket.ssl_cipher=;pc.weight=0;${GARBD_OPTS}" - fi + CA=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt + if [ -f /var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt ]; then + CA=/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt + fi + SSL_DIR=${SSL_DIR:-/etc/mysql/ssl} + if [ -f ${SSL_DIR}/ca.crt ]; then + CA=${SSL_DIR}/ca.crt + fi + SSL_INTERNAL_DIR=${SSL_INTERNAL_DIR:-/etc/mysql/ssl-internal} + if [ -f ${SSL_INTERNAL_DIR}/ca.crt ]; then + CA=${SSL_INTERNAL_DIR}/ca.crt + fi + + KEY=${SSL_DIR}/tls.key + CERT=${SSL_DIR}/tls.crt + if [ -f ${SSL_INTERNAL_DIR}/tls.key -a -f ${SSL_INTERNAL_DIR}/tls.crt ]; then + KEY=${SSL_INTERNAL_DIR}/tls.key + CERT=${SSL_INTERNAL_DIR}/tls.crt + fi + + if [ -f "$CA" -a -f "$KEY" -a -f "$CERT" ]; then + GARBD_OPTS="socket.ssl_ca=${CA};socket.ssl_cert=${CERT};socket.ssl_key=${KEY};socket.ssl_cipher=;pc.weight=0;${GARBD_OPTS}" + fi } function request_streaming() { - local LOCAL_IP=$(hostname -i | sed -E 's/.*\b([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\b.*/\1/') - local NODE_NAME=$(get_backup_source) - - if [ -z "$NODE_NAME" ]; then - /opt/percona/peer-list -on-start=/usr/bin/get-pxc-state -service=$PXC_SERVICE - log 'ERROR' 'Cannot find node for backup' - log 'ERROR' 'Backup was finished unsuccessfull' - exit 1 - fi - - set +o errexit - log 'INFO' 'Garbd was started' - garbd \ - --address "gcomm://$NODE_NAME.$PXC_SERVICE?gmcast.listen_addr=tcp://0.0.0.0:4567" \ - --donor "$NODE_NAME" \ - --group "$PXC_SERVICE" \ - --options "$GARBD_OPTS" \ - --sst "xtrabackup-v2:$LOCAL_IP:4444/xtrabackup_sst//1" \ - --recv-script="/usr/bin/run_backup.sh" - EXID_CODE=$? - - if [ -f '/tmp/backup-is-completed' ]; then - log 'INFO' 'Backup was finished successfully' - exit 0 - fi - - log 'ERROR' 'Backup was finished unsuccessfull' - - exit $EXID_CODE + local LOCAL_IP=$(hostname -i | sed -E 's/.*\b([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\b.*/\1/') + local NODE_NAME=$(get_backup_source) + + if [ -z "$NODE_NAME" ]; then + /opt/percona/peer-list -on-start=/usr/bin/get-pxc-state -service=$PXC_SERVICE + log 'ERROR' 'Cannot find node for backup' + log 'ERROR' 'Backup was finished unsuccessfull' + exit 1 + fi + + set +o errexit + log 'INFO' 'Garbd was started' + garbd \ + --address "gcomm://$NODE_NAME.$PXC_SERVICE?gmcast.listen_addr=tcp://0.0.0.0:4567" \ + --donor "$NODE_NAME" \ + --group "$PXC_SERVICE" \ + --options "$GARBD_OPTS" \ + --sst "xtrabackup-v2:$LOCAL_IP:4444/xtrabackup_sst//1" \ + --recv-script="/usr/bin/run_backup.sh" \ + --post-recv-script="/usr/bin/post_backup.sh" + EXID_CODE=$? + + if [ -f '/tmp/backup-is-completed' ]; then + log 'INFO' 'Backup was finished successfully' + exit 0 + fi + + log 'ERROR' 'Backup was finished unsuccessfull' + + exit $EXID_CODE } check_ssl if [ -n "${S3_BUCKET}" ]; then - clean_backup_s3 + clean_backup_s3 fi request_streaming diff --git a/percona-xtradb-cluster-8.0-backup/run_backup.sh b/percona-xtradb-cluster-8.0-backup/run_backup.sh index 5417db01..89b94b54 100755 --- a/percona-xtradb-cluster-8.0-backup/run_backup.sh +++ b/percona-xtradb-cluster-8.0-backup/run_backup.sh @@ -90,13 +90,6 @@ backup_volume() { log 'INFO' "Socat(2) returned $?" fi - stat xtrabackup.stream - if (($(stat -c%s xtrabackup.stream) < 5000000)); then - log 'ERROR' 'Backup is empty' - log 'ERROR' 'Backup was finished unsuccessfully' - exit 1 - fi - md5sum xtrabackup.stream | tee md5sum.txt } backup_s3() { @@ -126,13 +119,6 @@ backup_s3() { FIRST_RECEIVED=1 fi - aws $AWS_S3_NO_VERIFY_SSL s3 ls s3://$S3_BUCKET/$S3_BUCKET_PATH.md5 - md5_size=$(aws $AWS_S3_NO_VERIFY_SSL --output json s3api list-objects --bucket "$S3_BUCKET" --prefix "$S3_BUCKET_PATH.md5" --query 'Contents[0].Size' | sed -e 's/.*"size":\([0-9]*\).*/\1/') - if [[ $md5_size =~ "Object does not exist" ]] || ((md5_size < 23000)) ; then - log 'ERROR' 'Backup is empty' - log 'ERROR' 'Backup was finished unsuccessfull' - exit 1 - fi } backup_azure() { From 7f220ff26db537501e125d6a96fb909236cc1326 Mon Sep 17 00:00:00 2001 From: Viacheslav Sarzhan Date: Thu, 10 Oct 2024 20:34:50 +0300 Subject: [PATCH 5/6] K8SPXC-1475 use aws cli * rename function --- percona-xtradb-cluster-8.0-backup/lib/pxc/aws.sh | 2 +- percona-xtradb-cluster-8.0-backup/lib/pxc/backup.sh | 2 +- percona-xtradb-cluster-8.0-backup/recovery-cloud.sh | 2 +- percona-xtradb-cluster-8.0-backup/run_backup.sh | 3 +-- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/percona-xtradb-cluster-8.0-backup/lib/pxc/aws.sh b/percona-xtradb-cluster-8.0-backup/lib/pxc/aws.sh index 493b62d5..13cb2f09 100755 --- a/percona-xtradb-cluster-8.0-backup/lib/pxc/aws.sh +++ b/percona-xtradb-cluster-8.0-backup/lib/pxc/aws.sh @@ -20,7 +20,7 @@ is_object_exist() { fi } -mc_add_bucket_dest() { +s3_add_bucket_dest() { { set +x; } 2>/dev/null aws configure set aws_access_key_id "$ACCESS_KEY_ID" aws configure set aws_secret_access_key "$SECRET_ACCESS_KEY" diff --git a/percona-xtradb-cluster-8.0-backup/lib/pxc/backup.sh b/percona-xtradb-cluster-8.0-backup/lib/pxc/backup.sh index 0476b218..4900235f 100755 --- a/percona-xtradb-cluster-8.0-backup/lib/pxc/backup.sh +++ b/percona-xtradb-cluster-8.0-backup/lib/pxc/backup.sh @@ -27,7 +27,7 @@ log() { } clean_backup_s3() { - mc_add_bucket_dest + s3_add_bucket_dest is_object_exist "$S3_BUCKET" "$S3_BUCKET_PATH.$SST_INFO_NAME" || xbcloud delete ${XBCLOUD_ARGS} --storage=s3 --s3-bucket="$S3_BUCKET" "$S3_BUCKET_PATH.$SST_INFO_NAME" is_object_exist "$S3_BUCKET" "$S3_BUCKET_PATH/" || xbcloud delete ${XBCLOUD_ARGS} --storage=s3 --s3-bucket="$S3_BUCKET" "$S3_BUCKET_PATH" diff --git a/percona-xtradb-cluster-8.0-backup/recovery-cloud.sh b/percona-xtradb-cluster-8.0-backup/recovery-cloud.sh index 3993b0f4..36787b26 100755 --- a/percona-xtradb-cluster-8.0-backup/recovery-cloud.sh +++ b/percona-xtradb-cluster-8.0-backup/recovery-cloud.sh @@ -17,7 +17,7 @@ fi if [ -n "$S3_BUCKET_URL" ]; then { set +x; } 2>/dev/null - mc_add_bucket_dest + s3_add_bucket_dest set -x aws $AWS_S3_NO_VERIFY_SSL s3 ls "${S3_BUCKET_URL}" elif [ -n "${BACKUP_PATH}" ]; then diff --git a/percona-xtradb-cluster-8.0-backup/run_backup.sh b/percona-xtradb-cluster-8.0-backup/run_backup.sh index ebbf13ef..9506379d 100755 --- a/percona-xtradb-cluster-8.0-backup/run_backup.sh +++ b/percona-xtradb-cluster-8.0-backup/run_backup.sh @@ -92,7 +92,7 @@ backup_volume() { } backup_s3() { - mc_add_bucket_dest + s3_add_bucket_dest socat -u "$SOCAT_OPTS" stdio | xbstream -x -C /tmp $XBSTREAM_EXTRA_ARGS & wait $! @@ -117,7 +117,6 @@ backup_s3() { | (grep -v "error: http request failed: Couldn't resolve host name" || exit 1) FIRST_RECEIVED=1 fi - } backup_azure() { From bdc7dd90294cde758def1afc393110dc73744644 Mon Sep 17 00:00:00 2001 From: Viacheslav Sarzhan Date: Mon, 14 Oct 2024 14:21:51 +0300 Subject: [PATCH 6/6] add aws cli for pxc 5.7 backups --- percona-xtradb-cluster-5.7-backup/Dockerfile | 23 +++++++++---------- percona-xtradb-cluster-5.7-backup/backup.sh | 17 ++++---------- .../recovery-cloud.sh | 8 +++---- 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/percona-xtradb-cluster-5.7-backup/Dockerfile b/percona-xtradb-cluster-5.7-backup/Dockerfile index ac282d63..f116e671 100644 --- a/percona-xtradb-cluster-5.7-backup/Dockerfile +++ b/percona-xtradb-cluster-5.7-backup/Dockerfile @@ -27,13 +27,17 @@ LABEL org.opencontainers.image.version=${PXC_VERSION} # check repository package signature in secure way RUN set -ex; \ export GNUPGHOME="$(mktemp -d)"; \ - gpg --batch --keyserver keyserver.ubuntu.com --recv-keys 4D1BB29D63D98E422B2113B19334A25F8507EFA5 3E6D826D3FBAB389C2F38E34BC4D06A08D8B756F; \ + gpg --batch --keyserver keyserver.ubuntu.com --recv-keys 4D1BB29D63D98E422B2113B19334A25F8507EFA5 3E6D826D3FBAB389C2F38E34BC4D06A08D8B756F FB5DB77FD5C118B80511ADA8A6310ACC4672475C; \ gpg --batch --export --armor 4D1BB29D63D98E422B2113B19334A25F8507EFA5 > ${GNUPGHOME}/PERCONA-PACKAGING-KEY; \ gpg --batch --export --armor 3E6D826D3FBAB389C2F38E34BC4D06A08D8B756F > ${GNUPGHOME}/RPM-GPG-KEY-oracle; \ - rpmkeys --import ${GNUPGHOME}/PERCONA-PACKAGING-KEY ${GNUPGHOME}/RPM-GPG-KEY-oracle; \ + gpg --batch --export --armor FB5DB77FD5C118B80511ADA8A6310ACC4672475C > ${GNUPGHOME}/RPM-GPG-KEY-aws; \ + rpmkeys --import ${GNUPGHOME}/PERCONA-PACKAGING-KEY ${GNUPGHOME}/RPM-GPG-KEY-oracle ${GNUPGHOME}/RPM-GPG-KEY-aws; \ microdnf install -y findutils; \ curl -Lf -o /tmp/percona-release.rpm https://repo.percona.com/yum/percona-release-latest.noarch.rpm; \ rpmkeys --checksig /tmp/percona-release.rpm; \ + curl -o /tmp/awscliv2.zip https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip; \ + curl -o /tmp/awscliv2.sig https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip.sig; \ + gpg --verify /tmp/awscliv2.sig /tmp/awscliv2.zip; \ rpm -i /tmp/percona-release.rpm; \ rm -rf "$GNUPGHOME" /tmp/percona-release.rpm; \ rpm --import /etc/pki/rpm-gpg/PERCONA-PACKAGING-KEY; \ @@ -61,6 +65,7 @@ RUN set -ex; \ iputils \ procps-ng \ util-linux \ + unzip \ procps-ng \ qpress \ tar \ @@ -68,7 +73,10 @@ RUN set -ex; \ libatomic \ libaio; \ microdnf clean all; \ - rm -rf /var/cache/dnf /var/cache/yum + unzip -q /tmp/awscliv2.zip -d /tmp/; \ + /tmp/aws/install; \ + aws --version; \ + rm -rf /var/cache/dnf /var/cache/yum /tmp/awscliv2.zip /tmp/awscliv2.sig /tmp/aws # create mysql user/group before mysql installation RUN groupadd -g 1001 mysql; \ @@ -102,15 +110,6 @@ RUN set -ex; \ RUN install -d -o 1001 -g 0 -m 0775 /backup; \ mkdir /usr/lib/pxc -ENV MC_VERSION=RELEASE.2024-07-08T20-59-24Z -ENV MC_SHA256SUM=e111d2b4bea05aadbffaa3fc8d2436a3fefedf030cd1318568bccb72810024f0 -RUN set -ex; \ - curl -o /usr/bin/mc -O https://dl.minio.io/client/mc/release/linux-amd64/archive/mc.${MC_VERSION} \ - && chmod +x /usr/bin/mc \ - && echo "${MC_SHA256SUM} /usr/bin/mc" | sha256sum -c - \ - && curl -o /licenses/LICENSE.mc \ - https://raw.githubusercontent.com/minio/mc/${MC_VERSION}/LICENSE - COPY lib/pxc /usr/lib/pxc COPY recovery-*.sh backup.sh get-pxc-state /usr/bin/ diff --git a/percona-xtradb-cluster-5.7-backup/backup.sh b/percona-xtradb-cluster-5.7-backup/backup.sh index 59168669..430ab7e3 100755 --- a/percona-xtradb-cluster-5.7-backup/backup.sh +++ b/percona-xtradb-cluster-5.7-backup/backup.sh @@ -5,6 +5,7 @@ set -o xtrace LIB_PATH='/usr/lib/pxc' . ${LIB_PATH}/vault.sh +. ${LIB_PATH}/aws.sh GARBD_OPTS="" SOCAT_OPTS="TCP-LISTEN:4444,reuseaddr,retry=30" @@ -141,22 +142,12 @@ backup_volume() { echo '[INFO] Backup was finished successfully' } -is_object_exist() { - local bucket="$1" - local object="$2" - - if [[ -n "$(mc -C /tmp/mc ${INSECURE_ARG} --json ls "dest/$bucket/$object" | jq '.status')" ]]; then - return 1 - fi -} - backup_s3() { S3_BUCKET_PATH=${S3_BUCKET_PATH:-$PXC_SERVICE-$(date +%F-%H-%M)-xtrabackup.stream} echo "[INFO] Backup to s3://$S3_BUCKET/$S3_BUCKET_PATH started" { set +x; } 2>/dev/null - echo "+ mc -C /tmp/mc ${INSECURE_ARG} config host add dest "${ENDPOINT:-https://s3.amazonaws.com}" ACCESS_KEY_ID SECRET_ACCESS_KEY" - mc -C /tmp/mc ${INSECURE_ARG} config host add dest "${ENDPOINT:-https://s3.amazonaws.com}" "$ACCESS_KEY_ID" "$SECRET_ACCESS_KEY" + s3_add_bucket_dest set -x is_object_exist "$S3_BUCKET" "$S3_BUCKET_PATH.$SST_INFO_NAME" || xbcloud delete ${INSECURE_ARG} $XBCLOUD_EXTRA_ARGS --storage=s3 --s3-bucket="$S3_BUCKET" "$S3_BUCKET_PATH.$SST_INFO_NAME" is_object_exist "$S3_BUCKET" "$S3_BUCKET_PATH" || xbcloud delete ${INSECURE_ARG} $XBCLOUD_EXTRA_ARGS --storage=s3 --s3-bucket="$S3_BUCKET" "$S3_BUCKET_PATH" @@ -176,8 +167,8 @@ backup_s3() { | xbcloud put --storage=s3 --parallel="$(grep -c processor /proc/cpuinfo)" --md5 ${INSECURE_ARG} $XBCLOUD_EXTRA_ARGS --s3-bucket="$S3_BUCKET" "$S3_BUCKET_PATH" 2>&1 \ | (grep -v "error: http request failed: Couldn't resolve host name" || exit 1) - mc -C /tmp/mc ${INSECURE_ARG} stat "dest/$S3_BUCKET/$S3_BUCKET_PATH.md5" - md5_size=$(mc -C /tmp/mc ${INSECURE_ARG} stat --json "dest/$S3_BUCKET/$S3_BUCKET_PATH.md5" | sed -e 's/.*"size":\([0-9]*\).*/\1/') + aws $AWS_S3_NO_VERIFY_SSL s3 ls s3://$S3_BUCKET/$S3_BUCKET_PATH.md5 + md5_size=$(aws $AWS_S3_NO_VERIFY_SSL --output json s3api list-objects --bucket "$S3_BUCKET" --prefix "$S3_BUCKET_PATH.md5" --query 'Contents[0].Size' | sed -e 's/.*"size":\([0-9]*\).*/\1/') if [[ $md5_size =~ "Object does not exist" ]] || ((md5_size < 23000)); then echo '[ERROR] Backup is empty' echo '[ERROR] Backup was finished unsuccessfully' diff --git a/percona-xtradb-cluster-5.7-backup/recovery-cloud.sh b/percona-xtradb-cluster-5.7-backup/recovery-cloud.sh index 52de5fda..88c9dc0b 100755 --- a/percona-xtradb-cluster-5.7-backup/recovery-cloud.sh +++ b/percona-xtradb-cluster-5.7-backup/recovery-cloud.sh @@ -6,21 +6,19 @@ set -o xtrace LIB_PATH='/usr/lib/pxc' . ${LIB_PATH}/check-version.sh . ${LIB_PATH}/vault.sh +. ${LIB_PATH}/aws.sh -MC_ARGS='-C /tmp/mc' XBCLOUD_ARGS="$XBCLOUD_EXTRA_ARGS" if [ -n "$VERIFY_TLS" ] && [[ $VERIFY_TLS == "false" ]]; then XBCLOUD_ARGS="--insecure ${XBCLOUD_ARGS}" - MC_ARGS="${MC_ARGS} --insecure" fi if [ -n "$S3_BUCKET_URL" ]; then { set +x; } 2>/dev/null - echo "+ mc ${MC_ARGS} config host add dest ${ENDPOINT:-https://s3.amazonaws.com} ACCESS_KEY_ID SECRET_ACCESS_KEY" - mc ${MC_ARGS} config host add dest "${ENDPOINT:-https://s3.amazonaws.com}" "$ACCESS_KEY_ID" "$SECRET_ACCESS_KEY" + s3_add_bucket_dest set -x - mc ${MC_ARGS} ls "dest/${S3_BUCKET_URL}" + aws $AWS_S3_NO_VERIFY_SSL s3 ls "${S3_BUCKET_URL}" elif [ -n "${BACKUP_PATH}" ]; then XBCLOUD_ARGS="${XBCLOUD_ARGS} --storage=azure" fi