'Lambda Function to create a postgress backup from pods in EKS and store it in S3 bucket

I wonder how to make a lambda function to create a backup of all the pods in EKS cluster, using postgress and making a PGDUMP_ALL, and store it in S3 buckets.

So far this is what I have:

#!/bin/bash
# Configuration
POD_NAME="$1"
CONTAINER="$2"
FILENAME="$POD_NAME-`date +%Y-%m-%d-%H:%M:%S`.sql"
DUMPS_FOLDER="/tmp/data"
#BUCKET_NAME="backup_${POD_NAME}"
BUCKET_NAME="$3"
#POSTGRES_PASSWORD AND HOSTNAME
# Backup from docker container
kubectl -n data exec $POD_NAME -c $CONTAINER -- bash -c "PGPASSWORD="abc" pg_dumpall -U postgres > /tmp/$FILENAME"
kubectl -n data exec $POD_NAME -c $CONTAINER -it -- cat /tmp/$FILENAME > $FILENAME
kubectl -n data exec $POD_NAME -c $CONTAINER -- bash -c "rm /tmp/*.sql"

# Cleanup the files inside only #7
(ls -t | head -n 7 ; ls -t) | uniq -u
bzip2 --best $FILENAME

#Upload the backup to S3 bucket
aws s3 sync  $DUMPS_FOLDER s3://$BUCKET_NAME

And I was creating a Dockerfile with that, but I have some feelings its not going to work...

FROM amazonlinux:latest

ADD backup_pgsql_k8s_ens.sh /usr/local/bin/backup_pgsql_k8s_ens.sh
RUN yum -y install unzip aws-cli postgresql shadow-utils bzip2
RUN curl -o kubectl https://amazon-eks.s3.us-west-2.amazonaws.com/1.21.2/2021-07-05/bin/linux/amd64/kubectl && \
chmod +x ./kubectl &&\
mkdir -p $HOME/bin && \
cp ./kubectl $HOME/bin/kubectl && export PATH=$PATH:$HOME/bin && \
echo 'export PATH=$PATH:$HOME/bin' >> ~/.bashrc
# RUN groupadd postgres && useradd -m postgres -g postgres
# USER postgres
# WORKDIR /home/postgres

ENTRYPOINT ["/usr/local/bin/backup_pgsql_k8s_ens.sh"]



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source