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

pica-backup: Modify mysql-run.sh and postgre-run.sh so that multiple instances...

pica-backup: Modify mysql-run.sh and postgre-run.sh so that multiple instances of the scripts can be run at the same time with different parameters
parent b13704ec
No related branches found
No related tags found
1 merge request!5CheckMK stable image
FROM debian:latest
MAINTAINER antoine@barbare.me
FROM registry.picasoft.net:5000/pica-debian
RUN apt-get update && \
apt-get install -y --no-install-recommends \
......@@ -9,12 +8,12 @@ RUN apt-get update && \
apt-get clean \
mkdir /scripts/{mysql,postgresql}
ENV CRON_TIME="0 0 * * *" \
MYSQL_DB="--all-databases"
COPY mysql-run.sh /scripts/mysql/mysql-run.sh
COPY postgres-run.sh /scripts/postgresql/postgres-backup.sh
COPY postgres-run.sh /scripts/postgresql/postgres-run.sh
COPY run.sh /scripts
ENV CRON_TIME="0 0 * * *"
VOLUME ["/backup"]
CMD ["/bin/bash"]
CMD ["/scripts/run.sh"]
......@@ -20,9 +20,11 @@ MYSQL_PASS=${MYSQL_PASS:-${MYSQL_ENV_MYSQL_PASS}}
BACKUP_CMD="mysqldump -h${MYSQL_HOST} -P${MYSQL_PORT} -u${MYSQL_USER} -p${MYSQL_PASS} ${EXTRA_OPTS} ${MYSQL_DB} > $BACKUP_FOLDER"'${BACKUP_NAME}'
backup_script_name="${MYSQL_SERVICE_NAME}-backup.sh"
echo "=> Creating backup script"
rm -f /backup.sh
cat <<EOF >> /backup.sh
rm -f "/$backup_script_name"
cat <<EOF >> "/$backup_script_name"
#!/bin/bash
MAX_BACKUPS=${MAX_BACKUPS}
......@@ -33,7 +35,7 @@ if ${BACKUP_CMD} ;then
echo " Backup succeeded"
else
echo " Backup failed"
rm -rf $BACKUP_FOLDER\${BACKUP_NAME}
rm -rf ${BACKUP_FOLDER}\${BACKUP_NAME}
fi
if [ -n "\${MAX_BACKUPS}" ]; then
......@@ -41,16 +43,18 @@ if [ -n "\${MAX_BACKUPS}" ]; then
do
BACKUP_TO_BE_DELETED=\$(ls $BACKUP_FOLDER -N1 | sort | head -n 1)
echo " Backup \${BACKUP_TO_BE_DELETED} is deleted"
rm -rf $BACKUP_FOLDER\${BACKUP_TO_BE_DELETED}
rm -rf ${BACKUP_FOLDER}\${BACKUP_TO_BE_DELETED}
done
fi
echo "=> Backup done"
EOF
chmod +x /backup.sh
chmod +x /$backup_script_name
restore_script_name="${MYSQL_SERVICE_NAME}-restore.sh"
backup_log="${MYSQL_SERVICE_NAME}_mysql_backup.log"
echo "=> Creating restore script"
rm -f /restore.sh
cat <<EOF >> /restore.sh
rm -f /$restore_script_name
cat <<EOF >> /$restore_script_name
#!/bin/bash
echo "=> Restore database from \$1"
if mysql -h${MYSQL_HOST} -P${MYSQL_PORT} -u${MYSQL_USER} -p${MYSQL_PASS} < \$1 ;then
......@@ -60,25 +64,24 @@ else
fi
echo "=> Done"
EOF
chmod +x /restore.sh
chmod +x /$restore_script_name
touch /mysql_backup.log
tail -F /mysql_backup.log &
touch /$backup_log
tail -F /$backup_log &
if [ -n "${INIT_BACKUP}" ]; then
echo "=> Create a backup on the startup"
/backup.sh
/${backup_script_name}
elif [ -n "${INIT_RESTORE_LATEST}" ]; then
echo "=> Restore lates backup"
echo "=> Restore latest backup"
until nc -z $MYSQL_HOST $MYSQL_PORT
do
echo "waiting database container..."
sleep 1
done
ls -d -1 $BACKUP_FOLDER* | tail -1 | xargs /restore.sh
ls -d -1 $BACKUP_FOLDER* | tail -1 | xargs /$restore_script_name
fi
echo "${CRON_TIME} /backup.sh >> /mysql_backup.log 2>&1" > /crontab.conf
echo "${CRON_TIME} /$backup_script_name >> /$backup_log 2>&1" >> /crontab.conf
crontab /crontab.conf
echo "=> Running cron job"
exec cron -f
......@@ -10,9 +10,11 @@ BACKUP_FOLDER=${BACKUP_FOLDER:-"/backup/"}
BACKUP_CMD="pg_dump -w -c > $BACKUP_FOLDER"'${BACKUP_NAME}'
backup_script_name="${POSTGRES_SERVICE_NAME}"
echo "=> Creating backup script"
rm -f /backup.sh
cat <<EOF >> /backup.sh
rm -f "/$backup_script_name"
cat <<EOF >> "/$backup_script_name"
#!/bin/bash
MAX_BACKUPS=${MAX_BACKUPS}
export PGHOST=$POSTGRES_HOST
......@@ -36,16 +38,19 @@ if [ -n "\${MAX_BACKUPS}" ]; then
do
BACKUP_TO_BE_DELETED=\$(ls $BACKUP_FOLDER -N1 | sort | head -n 1)
echo " Backup \${BACKUP_TO_BE_DELETED} is deleted"
rm -rf $BACKUP_FOLDER\${BACKUP_TO_BE_DELETED}
rm -rf ${BACKUP_FOLDER}\${BACKUP_TO_BE_DELETED}
done
fi
echo "=> Backup done"
EOF
chmod +x /backup.sh
chmod +x /$backup_script_name
restore_script_name="${POSTGRES_SERVICE_NAME}-restore.sh"
backup_log="${POSTGRES_SERVICE_NAME}_postgres_backup.log"
echo "=> Creating restore script"
rm -f /restore.sh
cat <<EOF >> /restore.sh
rm -f "/$restore_script_name"
cat <<EOF >> /$restore_script_name
#!/bin/bash
export PGHOST=$POSTGRES_HOST
export PGPORT=$POSTGRES_PORT
......@@ -61,25 +66,25 @@ else
fi
echo "=> Done"
EOF
chmod +x /restore.sh
chmod +x /$restore_script_name
touch /postgres_backup.log
tail -F /postgres_backup.log &
touch /$backup_log
tail -F /$backup_log &
if [ -n "${INIT_BACKUP}" ]; then
echo "=> Create a backup on the startup"
/backup.sh
/$backup_script_name
elif [ -n "${INIT_RESTORE_LATEST}" ]; then
echo "=> Restore lates backup"
echo "=> Restore latest backup"
until nc -z $POSTGRES_HOST $POSTGRES_PORT
do
echo "waiting database container..."
sleep 1
done
ls -d -1 $BACKUP_FOLDER* | tail -1 | xargs /restore.sh
ls -d -1 $BACKUP_FOLDER* | tail -1 | xargs /$restore_script_name
fi
echo "${CRON_TIME} /backup.sh >> /postgres_backup.log 2>&1" > /crontab.conf
echo "${CRON_TIME} /$backup_script_name >> /$backup_log 2>&1" >> /crontab.conf
crontab /crontab.conf
echo "=> Running cron job"
exec cron -f
......@@ -2,17 +2,18 @@
# DB_TYPE : MYSQL|POSTGRES
# MYSQL POSTGRESQL
#
# MYSQL POSTGRESQL
# MYSQL_SERVICE_NAME_LIST POSTGRES_SERVICE_NAME_LIST
# 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_DB_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
# MYSQL_BACKUP_FOLDER_LIST POSTGRESQL_BACKUP_FOLDER_LIST
#####################
# Create the arrays #
......@@ -21,23 +22,29 @@
IFS=,
#MYSQL
read -a MYSQL_HOST_ARRAY <<< "$MYSQL_HOST_LIST"
read -a MYSQL_SERVICE_NAME_ARRAY <<< "$MYSQL_SERVICE_NAME_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_DB_ARRAY <<< "$MYSQL_DB_LIST"
read -a MYSQL_MAX_BACKUPS_ARRAY <<< "$MYSQL_MAX_BACKUPS_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"
read -a MYSQL_BACKUP_FOLDER_ARRAY <<< "$MYSQL_BACKUP_FOLDER_LIST"
#POSTGRES
read -a POSTGRES_HOST_ARRAY <<< "$POSTGRES_HOST_LIST"
read -a POSTGRES_SERVICE_NAME_ARRAY <<< "$POSTGRES_SERVICE_NAME_LIST"
read -a POSTGRES_PORT_ARRAY <<< "$POSTGRES_PORT_LIST"
read -a POSTGRES_USER_ARRAY <<< "$POSTGRES_PASS_LIST"
read -a POSTGRES_USER_ARRAY <<< "$POSTGRES_USER_LIST"
read -a POSTGRES_PASS_ARRAY <<< "$POSTGRES_PASS_LIST"
read -a POSTGRES_DB_ARRAY <<< "$POSTGRES_DB_LIST"
read -a POSTGRES_MAX_BACKUPS_ARRAY <<< "$POSTGRES_MAX_BACKUPS_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"
read -a POSTGRES_BACKUP_FOLDER_ARRAY <<< "$POSTGRES_BACKUP_FOLDER_LIST"
###########################
# Call the backup scripts #
......@@ -50,15 +57,18 @@ NB_CALLS=${#MYSQL_HOST_ARRAY[@]}
for (( i=0; i < $NB_CALLS; i++ ))
do
MYSQL_SERVICE_NAME=${MYSQL_SERVICE_NAME_ARRAY[i]} \
MYSQL_HOST=${MYSQL_HOST_ARRAY[i]} \
MYSQL_PORT=${MYSQL_PORT_ARRAY[i]} \
MYSQL_USER=${MYSQL_USER_ARRAY[i]} \
MYSQL_PASS=${MYSQL_PASS_ARRAY[i]} \
MYSQL_DB=${MYSQL_DB_ARRAY[i]} \
MAX_BACKUPS=${MYSQL_MAX_BACKUPS_ARRAY[i]} \
INIT_BACKUP=${MYSQL_INIT_BACKUP_ARRAY[i]} \
CRON_TIME=${MYSQL_CRON_TIME_ARRAY[i]} \
EXTRA_OPTS=${MYSQL_EXTRA_OPTS_ARRAY[i]} \
/script/mysql/mysql-run.sh
BACKUP_FOLDER="/backup/${MYSQL_BACKUP_FOLDER_ARRAY[i]}/" \
/scripts/mysql/mysql-run.sh
done
#Execute PostgreSQL backups
......@@ -66,13 +76,18 @@ NB_CALLS=${#POSTGRES_HOST_ARRAY[@]}
for (( i=0; i < $NB_CALLS; i++ ))
do
POSTGRES_SERVICE_NAME=${POSTGRES_SERVICE_NAME_ARRAY[i]} \
POSTGRES_HOST=${POSTGRES_HOST_ARRAY[i]} \
POSTGRES_PORT=${POSTGRES_PORT_ARRAY[i]} \
POSTGRES_USER=${POSTGRES_USER_ARRAY[i]} \
POSTGRES_PASS=${POSTGRES_PASS_ARRAY[i]} \
POSTGRES_DB=${POSTGRES_DB_ARRAY[i]} \
MAX_BACKUPS=${POSTGRES_MAX_BACKUP_ARRAY[i]} \
INIT_BACKUP=${POSTGRES_INIT_BACKUP_ARRAY[i]} \
CRON_TIME=${POSTGRES_CRON_TIME_ARRAY[i]} \
EXTRA_OPTS=${POSTGRES_EXTRA_OPTS_ARRAY[i]} \
/script/postgresql/postgres-run.sh
BACKUP_FOLDER="/backup/${POSTGRES_BACKUP_FOLDER_ARRAY[i]}/" \
/scripts/postgresql/postgres-run.sh
done
exec cron -f
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