Merge branch 'main' of github.com:jkaninda/pg-bkup into docs
This commit is contained in:
78
README.md
78
README.md
@@ -85,59 +85,45 @@ networks:
|
||||
```
|
||||
## Deploy on Kubernetes
|
||||
|
||||
For Kubernetes, you don't need to run it in scheduled mode. You can deploy it as CronJob.
|
||||
For Kubernetes, you don't need to run it in scheduled mode. You can deploy it as Job or CronJob.
|
||||
|
||||
### Simple Kubernetes CronJob usage:
|
||||
### Simple Kubernetes backup Job :
|
||||
|
||||
```yaml
|
||||
apiVersion: batch/v1
|
||||
kind: CronJob
|
||||
kind: Job
|
||||
metadata:
|
||||
name: backup-job
|
||||
name: backup
|
||||
spec:
|
||||
schedule: "0 1 * * *"
|
||||
jobTemplate:
|
||||
template:
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: pg-bkup
|
||||
# In production, it is advised to lock your image tag to a proper
|
||||
# release version instead of using `latest`.
|
||||
# Check https://github.com/jkaninda/pg-bkup/releases
|
||||
# for a list of available releases.
|
||||
image: jkaninda/pg-bkup
|
||||
command:
|
||||
- bkup
|
||||
- backup
|
||||
- --storage
|
||||
- s3
|
||||
- --disable-compression
|
||||
env:
|
||||
- name: DB_PORT
|
||||
value: "5432"
|
||||
- name: DB_HOST
|
||||
value: ""
|
||||
- name: DB_NAME
|
||||
value: ""
|
||||
- name: DB_USERNAME
|
||||
value: ""
|
||||
# Please use secret!
|
||||
- name: DB_PASSWORD
|
||||
value: ""
|
||||
- name: AWS_S3_ENDPOINT
|
||||
value: "https://s3.amazonaws.com"
|
||||
- name: AWS_S3_BUCKET_NAME
|
||||
value: "xxx"
|
||||
- name: AWS_REGION
|
||||
value: "us-west-2"
|
||||
- name: AWS_ACCESS_KEY
|
||||
value: "xxxx"
|
||||
- name: AWS_SECRET_KEY
|
||||
value: "xxxx"
|
||||
- name: AWS_DISABLE_SSL
|
||||
value: "false"
|
||||
restartPolicy: Never
|
||||
containers:
|
||||
- name: pg-bkup
|
||||
# In production, it is advised to lock your image tag to a proper
|
||||
# release version instead of using `latest`.
|
||||
# Check https://github.com/jkaninda/pg-bkup/releases
|
||||
# for a list of available releases.
|
||||
image: jkaninda/pg-bkup
|
||||
command:
|
||||
- bkup
|
||||
- backup
|
||||
resources:
|
||||
limits:
|
||||
memory: "128Mi"
|
||||
cpu: "500m"
|
||||
env:
|
||||
- name: DB_PORT
|
||||
value: "5432"
|
||||
- name: DB_HOST
|
||||
value: ""
|
||||
- name: DB_NAME
|
||||
value: "dbname"
|
||||
- name: DB_USERNAME
|
||||
value: "postgres"
|
||||
# Please use secret!
|
||||
- name: DB_PASSWORD
|
||||
value: ""
|
||||
restartPolicy: Never
|
||||
```
|
||||
## Available image registries
|
||||
|
||||
|
||||
@@ -31,7 +31,9 @@ ENV SSH_HOST_NAME=""
|
||||
ENV SSH_IDENTIFY_FILE=""
|
||||
ENV SSH_PORT="22"
|
||||
ARG DEBIAN_FRONTEND=noninteractive
|
||||
ENV VERSION="v1.2.1"
|
||||
ENV VERSION="v1.2.2"
|
||||
ENV BACKUP_CRON_EXPRESSION=""
|
||||
ENV GNUPGHOME="/tmp/gnupg"
|
||||
ARG WORKDIR="/app"
|
||||
ARG BACKUPDIR="/backup"
|
||||
ARG BACKUP_TMP_DIR="/tmp/backup"
|
||||
@@ -41,21 +43,23 @@ LABEL author="Jonas Kaninda"
|
||||
|
||||
RUN apt-get update -qq
|
||||
|
||||
RUN apt install postgresql-client postgresql-client-common supervisor cron gnupg -y
|
||||
RUN apt install postgresql-client supervisor cron gnupg -y
|
||||
|
||||
# Clear cache
|
||||
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
RUN mkdir $WORKDIR
|
||||
RUN mkdir $BACKUPDIR
|
||||
RUN mkdir -p $BACKUP_TMP_DIR
|
||||
RUN mkdir -p $BACKUP_TMP_DIR && \
|
||||
mkdir -p $GNUPGHOME
|
||||
RUN chmod 777 $WORKDIR
|
||||
RUN chmod 777 $BACKUPDIR
|
||||
RUN chmod 777 $BACKUP_TMP_DIR
|
||||
RUN touch $BACKUP_CRON && \
|
||||
touch $BACKUP_CRON_SCRIPT && \
|
||||
chmod 777 $BACKUP_CRON && \
|
||||
chmod 777 $BACKUP_CRON_SCRIPT
|
||||
chmod 777 $BACKUP_CRON_SCRIPT && \
|
||||
chmod 777 $GNUPGHOME
|
||||
|
||||
COPY --from=build /app/pg-bkup /usr/local/bin/pg-bkup
|
||||
RUN chmod +x /usr/local/bin/pg-bkup
|
||||
@@ -65,4 +69,18 @@ RUN ln -s /usr/local/bin/pg-bkup /usr/local/bin/bkup
|
||||
ADD docker/supervisord.conf /etc/supervisor/supervisord.conf
|
||||
|
||||
WORKDIR $WORKDIR
|
||||
# Create backup shell script
|
||||
COPY <<EOF /usr/local/bin/backup
|
||||
#!/bin/sh
|
||||
# shellcheck disable=SC2068
|
||||
/usr/local/bin/pg-bkup backup $@
|
||||
EOF
|
||||
# Create restore shell script
|
||||
COPY <<EOF /usr/local/bin/restore
|
||||
#!/bin/sh
|
||||
# shellcheck disable=SC2068
|
||||
/usr/local/bin/pg-bkup restore $@
|
||||
EOF
|
||||
RUN chmod +x /usr/local/bin/backup && \
|
||||
chmod +x /usr/local/bin/restore
|
||||
ENTRYPOINT ["/usr/local/bin/pg-bkup"]
|
||||
|
||||
@@ -85,7 +85,7 @@ networks:
|
||||
|
||||
For Kubernetes, you don't need to run it in scheduled mode. You can deploy it as CronJob.
|
||||
|
||||
### Simple Kubernetes CronJob usage:
|
||||
### Simple Kubernetes backup CronJob:
|
||||
|
||||
```yaml
|
||||
apiVersion: batch/v1
|
||||
|
||||
@@ -10,7 +10,60 @@ nav_order: 8
|
||||
To deploy PostgreSQL Backup on Kubernetes, you can use Job to backup or Restore your database.
|
||||
For recurring backup you can use CronJob, you don't need to run it in scheduled mode. as described bellow.
|
||||
|
||||
## Backup Job
|
||||
## Backup Job to S3 Storage
|
||||
|
||||
```yaml
|
||||
apiVersion: batch/v1
|
||||
kind: Job
|
||||
metadata:
|
||||
name: backup
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: pg-bkup
|
||||
# In production, it is advised to lock your image tag to a proper
|
||||
# release version instead of using `latest`.
|
||||
# Check https://github.com/jkaninda/pg-bkup/releases
|
||||
# for a list of available releases.
|
||||
image: jkaninda/pg-bkup
|
||||
command:
|
||||
- bkup
|
||||
- backup
|
||||
- --storage
|
||||
- s3
|
||||
resources:
|
||||
limits:
|
||||
memory: "128Mi"
|
||||
cpu: "500m"
|
||||
env:
|
||||
- name: DB_PORT
|
||||
value: "5432"
|
||||
- name: DB_HOST
|
||||
value: ""
|
||||
- name: DB_NAME
|
||||
value: ""
|
||||
- name: DB_USERNAME
|
||||
value: ""
|
||||
# Please use secret!
|
||||
- name: DB_PASSWORD
|
||||
value: ""
|
||||
- name: AWS_S3_ENDPOINT
|
||||
value: "https://s3.amazonaws.com"
|
||||
- name: AWS_S3_BUCKET_NAME
|
||||
value: "xxx"
|
||||
- name: AWS_REGION
|
||||
value: "us-west-2"
|
||||
- name: AWS_ACCESS_KEY
|
||||
value: "xxxx"
|
||||
- name: AWS_SECRET_KEY
|
||||
value: "xxxx"
|
||||
- name: AWS_DISABLE_SSL
|
||||
value: "false"
|
||||
restartPolicy: Never
|
||||
```
|
||||
|
||||
## Backup Job to SSH remote Server
|
||||
|
||||
```yaml
|
||||
apiVersion: batch/v1
|
||||
@@ -181,7 +234,10 @@ spec:
|
||||
```
|
||||
|
||||
## Kubernetes Rootless
|
||||
|
||||
|
||||
This image also supports Kubernetes security context, you can run it in Rootless environment.
|
||||
It has been tested on Openshift, it works well.
|
||||
Deployment on Openshift is supported, you need to remove `securityContext` section on your yaml file.
|
||||
|
||||
```yaml
|
||||
apiVersion: batch/v1
|
||||
|
||||
@@ -22,7 +22,7 @@ func StartBackup(cmd *cobra.Command) {
|
||||
utils.SetEnv("STORAGE_PATH", storagePath)
|
||||
utils.GetEnv(cmd, "dbname", "DB_NAME")
|
||||
utils.GetEnv(cmd, "port", "DB_PORT")
|
||||
utils.GetEnv(cmd, "period", "SCHEDULE_PERIOD")
|
||||
utils.GetEnv(cmd, "period", "BACKUP_CRON_EXPRESSION")
|
||||
|
||||
//Get flag value and set env
|
||||
remotePath := utils.GetEnv(cmd, "path", "SSH_REMOTE_PATH")
|
||||
@@ -77,7 +77,7 @@ func scheduledMode(storage string) {
|
||||
fmt.Println(" Starting PostgreSQL Bkup... ")
|
||||
fmt.Println("***********************************")
|
||||
utils.Info("Running in Scheduled mode")
|
||||
utils.Info("Execution period %s ", os.Getenv("SCHEDULE_PERIOD"))
|
||||
utils.Info("Execution period %s ", os.Getenv("BACKUP_CRON_EXPRESSION"))
|
||||
utils.Info("Storage type %s ", storage)
|
||||
|
||||
//Test database connexion
|
||||
|
||||
@@ -56,7 +56,7 @@ set -e
|
||||
}
|
||||
|
||||
cronContent := fmt.Sprintf(`%s root exec /bin/bash -c ". /run/supervisord.env; /usr/local/bin/backup_cron.sh >> %s"
|
||||
`, os.Getenv("SCHEDULE_PERIOD"), cronLogFile)
|
||||
`, os.Getenv("BACKUP_CRON_EXPRESSION"), cronLogFile)
|
||||
|
||||
if err := utils.WriteToFile(cronJob, cronContent); err != nil {
|
||||
utils.Fatal("Error writing to %s: %v\n", cronJob, err)
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
#!/bin/sh
|
||||
DB_USERNAME='db_username'
|
||||
DB_PASSWORD='password'
|
||||
DB_HOST='db_hostname'
|
||||
DB_NAME='db_name'
|
||||
BACKUP_DIR="$PWD/backup"
|
||||
|
||||
docker run --rm --name pg-bkup -v $BACKUP_DIR:/backup/ -e "DB_HOST=$DB_HOST" -e "DB_USERNAME=$DB_USERNAME" -e "DB_PASSWORD=$DB_PASSWORD" jkaninda/pg-bkup bkup backup -d $DB_NAME
|
||||
Reference in New Issue
Block a user