Skip to content
Snippets Groups Projects
Commit b91f0ffb authored by Jerome Coste's avatar Jerome Coste Committed by PICHOU Kyâne
Browse files

add tests scripts

parent 945bec15
No related branches found
No related tags found
1 merge request!5CheckMK stable image
FROM debian:latest
MAINTAINER antoine@barbare.me
RUN apt-get update && \
apt-get install -y --no-install-recommends \
postgresql-client \
mysql-client \
cron && \
apt-get clean
ENV CRON_TIME="0 0 * * *" \
MYSQL_DB="--all-databases"
COPY mysql-run.sh /mysql-run.sh
COPY postgres-run.sh /postgres-backup.sh
VOLUME ["/backup"]
CMD ["/bin/bash"]
version: "2"
services:
backup:
image: backup
container_name: test-backup
volumes:
- /home/user/Documents/Picasoft/dockerfiles/pica-backup/t1:/backup/test-backup1
- /home/user/Documents/Picasoft/dockerfiles/pica-backup/t2:/backup/test-backup2
environment:
- TEST=aaaaa,bbbbbbb,ccccc
../mysql-backup/
\ No newline at end of file
#!/bin/bash
if [ "${MYSQL_ENV_MYSQL_PASS}" == "**Random**" ]; then
unset MYSQL_ENV_MYSQL_PASS
fi
MYSQL_HOST=${MYSQL_PORT_3306_TCP_ADDR:-${MYSQL_HOST}}
MYSQL_HOST=${MYSQL_PORT_1_3306_TCP_ADDR:-${MYSQL_HOST}}
MYSQL_PORT=${MYSQL_PORT_3306_TCP_PORT:-${MYSQL_PORT}}
MYSQL_PORT=${MYSQL_PORT_1_3306_TCP_PORT:-${MYSQL_PORT}}
MYSQL_USER=${MYSQL_USER:-${MYSQL_ENV_MYSQL_USER}}
MYSQL_PASS=${MYSQL_PASS:-${MYSQL_ENV_MYSQL_PASS}}
[ -z "${MYSQL_HOST}" ] && { echo "=> MYSQL_HOST cannot be empty" && exit 1; }
[ -z "${MYSQL_PORT}" ] && { echo "=> MYSQL_PORT cannot be empty" && exit 1; }
[ -z "${MYSQL_USER}" ] && { echo "=> MYSQL_USER cannot be empty" && exit 1; }
[ -z "${MYSQL_PASS}" ] && { echo "=> MYSQL_PASS cannot be empty" && exit 1; }
BACKUP_CMD="mysqldump -h${MYSQL_HOST} -P${MYSQL_PORT} -u${MYSQL_USER} -p${MYSQL_PASS} ${EXTRA_OPTS} ${MYSQL_DB} > /backup/"'${BACKUP_NAME}'
echo "=> Creating backup script"
rm -f /backup.sh
cat <<EOF >> /backup.sh
#!/bin/bash
MAX_BACKUPS=${MAX_BACKUPS}
BACKUP_NAME=\$(date +\%Y.\%m.\%d.\%H\%M\%S).sql
echo "=> Backup started: \${BACKUP_NAME}"
if ${BACKUP_CMD} ;then
echo " Backup succeeded"
else
echo " Backup failed"
rm -rf /backup/\${BACKUP_NAME}
fi
if [ -n "\${MAX_BACKUPS}" ]; then
while [ \$(ls /backup -N1 | wc -l) -gt \${MAX_BACKUPS} ];
do
BACKUP_TO_BE_DELETED=\$(ls /backup -N1 | sort | head -n 1)
echo " Backup \${BACKUP_TO_BE_DELETED} is deleted"
rm -rf /backup/\${BACKUP_TO_BE_DELETED}
done
fi
echo "=> Backup done"
EOF
chmod +x /backup.sh
echo "=> Creating restore script"
rm -f /restore.sh
cat <<EOF >> /restore.sh
#!/bin/bash
echo "=> Restore database from \$1"
if mysql -h${MYSQL_HOST} -P${MYSQL_PORT} -u${MYSQL_USER} -p${MYSQL_PASS} < \$1 ;then
echo " Restore succeeded"
else
echo " Restore failed"
fi
echo "=> Done"
EOF
chmod +x /restore.sh
touch /mysql_backup.log
tail -F /mysql_backup.log &
if [ -n "${INIT_BACKUP}" ]; then
echo "=> Create a backup on the startup"
/backup.sh
elif [ -n "${INIT_RESTORE_LATEST}" ]; then
echo "=> Restore lates backup"
until nc -z $MYSQL_HOST $MYSQL_PORT
do
echo "waiting database container..."
sleep 1
done
ls -d -1 /backup/* | tail -1 | xargs /restore.sh
fi
echo "${CRON_TIME} /backup.sh >> /mysql_backup.log 2>&1" > /crontab.conf
crontab /crontab.conf
echo "=> Running cron job"
exec cron -f
../postgres-backup/
\ No newline at end of file
#!/bin/bash
[ -z "${POSTGRES_HOST}" ] && { echo "=> POSTGRES_HOST cannot be empty" && exit 1; }
[ -z "${POSTGRES_PORT}" ] && { echo "=> POSTGRES_PORT cannot be empty" && exit 1; }
[ -z "${POSTGRES_USER}" ] && { echo "=> POSTGRES_USER cannot be empty" && exit 1; }
[ -z "${POSTGRES_PASS}" ] && { echo "=> POSTGRES_PASS cannot be empty" && exit 1; }
[ -z "${POSTGRES_DB}" ] && { echo "=> POSTGRES_DB cannot be empty" && exit 1; }
BACKUP_CMD="pg_dump -w -c > /backup/"'${BACKUP_NAME}'
echo "=> Creating backup script"
rm -f /backup.sh
cat <<EOF >> /backup.sh
#!/bin/bash
MAX_BACKUPS=${MAX_BACKUPS}
export PGHOST=$POSTGRES_HOST
export PGPORT=$POSTGRES_PORT
export PGDATABASE=$POSTGRES_DB
export PGUSER=$POSTGRES_USER
export PGPASSWORD=$POSTGRES_PASS
BACKUP_NAME=\$(date +\%Y.\%m.\%d.\%H\%M\%S).sql
echo "=> Backup started: \${BACKUP_NAME}"
if ${BACKUP_CMD} ;then
echo " Backup succeeded"
else
echo " Backup failed"
rm -rf /backup/\${BACKUP_NAME}
fi
if [ -n "\${MAX_BACKUPS}" ]; then
while [ \$(ls /backup -N1 | wc -l) -gt \${MAX_BACKUPS} ];
do
BACKUP_TO_BE_DELETED=\$(ls /backup -N1 | sort | head -n 1)
echo " Backup \${BACKUP_TO_BE_DELETED} is deleted"
rm -rf /backup/\${BACKUP_TO_BE_DELETED}
done
fi
echo "=> Backup done"
EOF
chmod +x /backup.sh
echo "=> Creating restore script"
rm -f /restore.sh
cat <<EOF >> /restore.sh
#!/bin/bash
export PGHOST=$POSTGRES_HOST
export PGPORT=$POSTGRES_PORT
export PGDATABASE=$POSTGRES_DB
export PGUSER=$POSTGRES_USER
export PGPASSWORD=$POSTGRES_PASS
echo "=> Restore database from \$1"
if pg_restore -w -d $POSTGRES_DB < \$1 ;then
echo " Restore succeeded"
else
echo " Restore failed"
fi
echo "=> Done"
EOF
chmod +x /restore.sh
touch /postgres_backup.log
tail -F /postgres_backup.log &
if [ -n "${INIT_BACKUP}" ]; then
echo "=> Create a backup on the startup"
/backup.sh
elif [ -n "${INIT_RESTORE_LATEST}" ]; then
echo "=> Restore lates backup"
until nc -z $POSTGRES_HOST $POSTGRES_PORT
do
echo "waiting database container..."
sleep 1
done
ls -d -1 /backup/* | tail -1 | xargs /restore.sh
fi
echo "${CRON_TIME} /backup.sh >> /postgres_backup.log 2>&1" > /crontab.conf
crontab /crontab.conf
echo "=> Running cron job"
exec cron -f
#!/bin/bash
# DB_TYPE : MYSQL|POSTGRES
# MYSQL POSTGRESQL
#
# MYSQL_HOST_LIST POSTGRES_HOST_LIST
# MYSQL_PORT_LIST POSTGRES_PORT_LIST
# MYSQL_USER_LIST POSTGRES_USER_LIST
# MYSQL_PASS_LIST POSTGRES_PASS_LIST
# POSTGRES_DB_LIST
# MYSQL_MAX_BACKUP_LIST POSTGRES_MAX_BACKUP_LIST
# MYSQL_INIT_BACKUP_LIST POSTGRES_INIT_BACKUP_LIST
# MYSQL_CRON_TIME_LIST POSTGRES_CRON_TIME_LIST
# MYSQL_EXTRA_OPTS_LIST POSTGRES_EXTRA_OPTS-LIST
IFS=,
#MYSQL
read -a MYSQL_HOST_ARRAY <<< "$MYSQL_HOST_LIST"
read -a MYSQL_PORT_ARRAY <<< "$MYSQL_PORT_LIST"
read -a MYSQL_USER_ARRAY <<< "$MYSQL_USER_LIST"
read -a MYSQL_PASS_ARRAY <<< "$MYSQL_PASS_LIST"
read -a MYSQL_MAX_BACKUP_ARRAY <<< "$MYSQL_MAX_BACKUP_LIST"
read -a MYSQL_INIT_BACKUP_ARRAY <<< "$MYSQL_INIT_BACKUP_LIST"
read -a MYSQL_CRON_TIME_ARRAY <<< "$MYSQL_CRON_TIME_LIST"
read -a MYSQL_EXTRA_OPTS_ARRAY <<< "$MYSQL_EXTRA_OPTS_LIST"
#POSTGRES
read -a POSTGRES_HOST_ARRAY <<< "$POSTGRES_HOST_LIST"
read -a POSTGRES_PORT_ARRAY <<< "$POSTGRES_PORT_LIST"
read -a POSTGRES_USER_ARRAY <<< "$POSTGRES_PASS_LIST"
read -a POSTGRES_DB_ARRAY <<< "$POSTGRES_DB_LIST"
read -a POSTGRES_MAX_BACKUP_ARRAY <<< "$POSTGRES_MAX_BACKUP_LIST"
read -a POSTGRES_INIT_BACKUP_ARRAY <<< "$POSTGRES_INIT_BACKUP_LIST"
read -a POSTGRES_CRON_TIME_ARRAY <<< "$POSTGRES_CRON_TIME_LIST"
read -a POSTGRES_EXTRA_OPTS_ARRAY <<< "$POSTGRES_EXTRA_OPTS_LIST"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment