Skip to content

Instantly share code, notes, and snippets.

@asksven
Created April 2, 2023 14:49
Show Gist options
  • Save asksven/ef674da4bddf85e0c30461345e08c511 to your computer and use it in GitHub Desktop.
Save asksven/ef674da4bddf85e0c30461345e08c511 to your computer and use it in GitHub Desktop.
Backup / Restore and error handling
apiVersion: v1
kind: ConfigMap
metadata:
name: backupcronjob-config
data:
backup.sh: |
#!/bin/bash
# Handler will be called if something fails
handler()
{
# quit gracefully if the error code was 0
[ $? -eq 0 ] && exit
echo "Backup: failed!"
exit 1
}
set -e
# since we use /bin/sh we trap on exit and use the handler to check the error code
trap 'handler' EXIT
# do the checks
[[ -z "${BACKUP_DB_FILENAME}" ]] && { echo "BACKUP_DB_FILENAME is empty" ; exit 1; }
[[ -z "${BACKUP_FS_FILENAME}" ]] && { echo "BACKUP_FS_FILENAME is empty" ; exit 1; }
[[ -z "${BACKUP_DB_HOST}" ]] && { echo "BACKUP_DB_HOST is empty" ; exit 1; }
[[ -z "${MY_CLUSTER}" ]] && { echo "MY_CLUSTER is empty" ; exit 1; }
[[ -z "${MY_NAMESPACE}" ]] && { echo "MY_NAMESPACE is empty" ; exit 1; }
TODAY=$(date +%Y%m%d-%H%M%S)
BACKUP_LOCATION="/tmp"
DB_BACKUP_FILE="${BACKUP_DB_FILENAME}-${TODAY}.sql.gz"
FS_BACKUP_FILE="${BACKUP_FS_FILENAME}-${TODAY}.tar.gz"
df -h
PGPASSWORD=${POSTGRES_PASSWORD} pg_dumpall --username="${POSTGRES_USER}" \
--host="${BACKUP_DB_HOST}" \
| gzip > ${BACKUP_LOCATION}/${DB_BACKUP_FILE}
tar --exclude='fs1/import' --exclude='fs1/plugins/.bundled-plugins' --exclude='fs1/plugins/.osgi-plugins' -czf ${BACKUP_LOCATION}/${FS_BACKUP_FILE} /fs1
echo "Cleaning-up exports older than 30 days"
echo "Before clean-up"
ls -la /fs1/export
find /fs1/export -name "*.zip" -type f -mtime +30 -exec rm -f {} \;
echo "After clean-up"
ls -la /fs1/export
echo "Cleaning-up backups older than 5 days"
echo "Before clean-up"
ls -la ${BACKUP_LOCATION}
find ${BACKUP_LOCATION} -name "*.gz" -type f -mtime +5 -exec rm -f {} \;
echo "After clean-up"
ls -la ${BACKUP_LOCATION}
#while true; do sleep 1; done
echo "Uploading"
echo "Creating alias"
mc alias set minio http://s3proxy-service.s3proxy.svc.cluster.local:9000 ${S3_KEY} ${S3_SECRET} --api S3v4
echo "Listing buckets"
mc ls minio
echo "Creating bucket"
bucket=${MY_CLUSTER}-${MY_NAMESPACE}
mc mb -p minio/$bucket
echo "Copying file"
mc cp $BACKUP_LOCATION/$DB_BACKUP_FILE minio/$bucket
mc cp $BACKUP_LOCATION/$FS_BACKUP_FILE minio/$bucket
echo "Listing files"
mc ls minio/$bucket
echo "Cleaning-up files older than 30 days"
mc find minio/$bucket --older-than 30d --exec "mc rm {}"
echo "Backup: success!"
restore.sh: |
#!/bin/bash
# Handler will be called if something fails
handler()
{
# quit gracefully if the error code was 0
[ $? -eq 0 ] && exit
echo "Backup: failed!"
exit 1
}
set -e
# since we use /bin/sh we trap on exit and use the handler to check the error code
trap 'handler' EXIT
# do the checks
[[ -z "${BACKUP_DB_FILENAME}" ]] && { echo "BACKUP_DB_FILENAME is empty" ; exit 1; }
[[ -z "${BACKUP_DB_HOST}" ]] && { echo "BACKUP_DB_HOST is empty" ; exit 1; }
[[ -z "${MY_CLUSTER}" ]] && { echo "MY_CLUSTER is empty" ; exit 1; }
[[ -z "${MY_NAMESPACE}" ]] && { echo "MY_NAMESPACE is empty" ; exit 1; }
BACKUP_LOCATION="/tmp"
BACKUP_DB_FILE="${BACKUP_DB_FILENAME}.sql.gz"
bucket=${MY_CLUSTER}-${MY_NAMESPACE}
echo "Downloading"
echo "Creating alias"
mc alias set minio http://s3proxy-service.s3proxy.svc.cluster.local:9000 ${S3_KEY} ${S3_SECRET} --api S3v4
echo "Listing buckets"
mc ls minio/$bucket
echo "Copying files"
mc cp minio/$bucket/$BACKUP_DB_FILE $BACKUP_LOCATION/$BACKUP_DB_FILE
mc cp minio/$bucket/${BACKUP_FS_FILENAME}.tar.gz $BACKUP_LOCATION/${BACKUP_FS_FILENAME}.tar.gz
echo "Listing files"
mc ls $BACKUP_LOCATION
set -e;
echo "Dropping the databases";
PGPASSWORD=${POSTGRES_PASSWORD} dropdb --username="${POSTGRES_USER}" --host="${BACKUP_DB_HOST}" default;
PGPASSWORD=${POSTGRES_PASSWORD} dropdb --username="${POSTGRES_USER}" --host="${BACKUP_DB_HOST}" postgres;
echo "Re-creating the database";
PGPASSWORD=${POSTGRES_PASSWORD} createdb --locale="C" --encoding="UTF8" --owner="${POSTGRES_USER}" --template="template0" --username="${POSTGRES_USER}" --host="${BACKUP_DB_HOST}" default;
PGPASSWORD=${POSTGRES_PASSWORD} createdb --locale="C" --encoding="UTF8" --owner="${POSTGRES_USER}" --template="template0" --username="${POSTGRES_USER}" --host="${BACKUP_DB_HOST}" postgres;
echo "Restoring database";
cat ${BACKUP_LOCATION}/${BACKUP_DB_FILE} | gunzip | PGPASSWORD=${POSTGRES_PASSWORD} psql --username="${POSTGRES_USER}" --host="${BACKUP_DB_HOST}" default;
echo "Restoring file system";
tar vxzf ${BACKUP_LOCATION}/${BACKUP_FS_FILENAME}.tar.gz fs1;
set +e;
echo "Cleaning-up /tmp"
echo "Before clean-up"
ls -la ${BACKUP_LOCATION}
find ${BACKUP_LOCATION} -name "*.gz" -type f -mtime +5 -exec rm -f {} \;
echo "After clean-up"
ls -la ${BACKUP_LOCATION}
#while true; do sleep 1; done
echo "Restore: success!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment