diff --git a/backup/pvc/Dockerfile b/backup/pvc/Dockerfile index a25b81c2..7109d932 100644 --- a/backup/pvc/Dockerfile +++ b/backup/pvc/Dockerfile @@ -17,4 +17,4 @@ COPY bin . RUN chmod +x *.sh USER user -CMD exec /bin/sh -c "while true; do sleep 1000; done" +CMD ./run.sh diff --git a/backup/pvc/bin/backup.sh b/backup/pvc/bin/backup.sh index fe9f9c82..80387373 100644 --- a/backup/pvc/bin/backup.sh +++ b/backup/pvc/bin/backup.sh @@ -6,11 +6,6 @@ set -eo pipefail [[ -z "${BACKUP_DIR}" ]] && echo "Required 'BACKUP_DIR' env not set" && exit 1; [[ -z "${JENKINS_HOME}" ]] && echo "Required 'JENKINS_HOME' env not set" && exit 1; -if [[ ! -z "${BACKUP_COUNT}" ]]; then - echo "Trimming to only $((BACKUP_COUNT-1)) recent backups in preparation for new backup" - ls -1t ${BACKUP_DIR} | tail -n +${BACKUP_COUNT} | xargs -I '{}' rm -f ${BACKUP_DIR}/'{}' -fi - backup_number=$1 echo "Running backup" diff --git a/backup/pvc/bin/run.sh b/backup/pvc/bin/run.sh new file mode 100644 index 00000000..c6cf2c9c --- /dev/null +++ b/backup/pvc/bin/run.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +set -eo pipefail + +[[ -z "${BACKUP_DIR}" ]] && echo "Required 'BACKUP_DIR' env not set" && exit 1; +[[ -z "${JENKINS_HOME}" ]] && echo "Required 'JENKINS_HOME' env not set" && exit 1; + +while true; +do + sleep 10 + if [[ ! -z "${BACKUP_COUNT}" ]]; then + echo "Trimming to only ${BACKUP_COUNT} recent backups in preparation for new backup" + ls ${BACKUP_DIR} | sort -gr | tail -n +$((BACKUP_COUNT +1)) | xargs -I '{}' rm ${BACKUP_DIR}/'{}' + fi +done \ No newline at end of file diff --git a/backup/pvc/e2e/backup_and_restore/test.sh b/backup/pvc/e2e/backup_and_restore/test.sh index c58e8dee..e4ad3ab8 100755 --- a/backup/pvc/e2e/backup_and_restore/test.sh +++ b/backup/pvc/e2e/backup_and_restore/test.sh @@ -1,7 +1,7 @@ #!/bin/bash set -eo pipefail -[ "${DEBUG}" ] && set -x +[[ "${DEBUG}" ]] && set -x # set current working directory to the directory of the script cd "$(dirname "$0")" diff --git a/backup/pvc/e2e/limit_backup_count/test.sh b/backup/pvc/e2e/limit_backup_count/test.sh new file mode 100755 index 00000000..b364e8c6 --- /dev/null +++ b/backup/pvc/e2e/limit_backup_count/test.sh @@ -0,0 +1,52 @@ +#!/bin/bash +set -eo pipefail + +[[ "${DEBUG}" ]] && set -x + +# set current working directory to the directory of the script +cd "$(dirname "$0")" + +docker_image=$1 + +if ! docker inspect ${docker_image} &> /dev/null; then + echo "Image '${docker_image}' does not exists" + false +fi + +JENKINS_HOME="$(pwd)/jenkins_home" +BACKUP_DIR="$(pwd)/backup" +mkdir -p ${BACKUP_DIR} + +touch ${BACKUP_DIR}/1.tar.gz +touch ${BACKUP_DIR}/2.tar.gz +touch ${BACKUP_DIR}/3.tar.gz +touch ${BACKUP_DIR}/4.tar.gz +touch ${BACKUP_DIR}/5.tar.gz +touch ${BACKUP_DIR}/6.tar.gz +touch ${BACKUP_DIR}/7.tar.gz +touch ${BACKUP_DIR}/8.tar.gz +touch ${BACKUP_DIR}/9.tar.gz +touch ${BACKUP_DIR}/10.tar.gz +touch ${BACKUP_DIR}/11.tar.gz + +# Create an instance of the container under testing +cid="$(docker run -e BACKUP_COUNT=2 -e JENKINS_HOME=${JENKINS_HOME} -v ${JENKINS_HOME}:${JENKINS_HOME}:ro -e BACKUP_DIR=${BACKUP_DIR} -v ${BACKUP_DIR}:${BACKUP_DIR}:rw -d ${docker_image})" +echo "Docker container ID '${cid}'" + +# Remove test directory and container afterwards +trap "docker rm -vf $cid > /dev/null;rm -rf ${BACKUP_DIR}" EXIT + +sleep 11 +touch ${BACKUP_DIR}/12.tar.gz +sleep 11 + +if [[ "${DEBUG}" ]]; then + docker logs ${cid} + ls -la ${BACKUP_DIR} +fi + +# only two latest backup should exists +[[ $(ls -1 ${BACKUP_DIR} | wc -l) -eq 2 ]] || exit 1 +[[ -f ${BACKUP_DIR}/11.tar.gz ]] || exit 2 +[[ -f ${BACKUP_DIR}/12.tar.gz ]] || exit 3 +echo PASS