diff --git a/docker.yml b/docker.yml index 08ebe7f..0444814 100644 --- a/docker.yml +++ b/docker.yml @@ -162,7 +162,7 @@ marker: "# {mark} ANSIBLE MANAGED BLOCK" block: | ### DB Backup - g_echo_ok "Starte Backup von MySQL-Datenbanken (Docker)" + g_echo_ok "Starte Backup von MySQL und PostgreSQL Datenbanken (Docker)" DAYS=7 @@ -170,6 +170,9 @@ CONTAINER=$(docker ps --format \{\{.Names\}\}:\{\{.Image\}\}| grep 'mysql\|mariadb' | cut -d":" -f1 | grep -v mailcow) mkdir -p $BACKUPDIR + + + ### MYSQL ### for i in $CONTAINER do # get credentials @@ -184,14 +187,33 @@ g_echo_error "No dbdumpcmd found in container $i! No backup created!" continue fi - g_echo "Sichere Datenbank $MARIADB_DATABASE DB aus $i"; + g_echo "Sichere MySQL/MariaDB Datenbank $MARIADB_DATABASE DB aus $i"; docker exec $i $DBDUMPCMD --no-tablespaces -u $MARIADB_USR -p$MARIADB_PWD $MARIADB_DATABASE 2>${g_tmp}/dberr | gzip > $BACKUPDIR/$i-$MARIADB_DATABASE-$TIMESTAMP.sql.gz || g_echo_error "DB-Backup von $MARIADB_DATABASE war nicht erfolgreich $(cat ${g_tmp}/dberr)" # dont delete last old backups! OLD_BACKUPS=$(ls -1 $BACKUPDIR/$i*.gz |wc -l) if [ $OLD_BACKUPS -gt $DAYS ]; then find $BACKUPDIR -name "$i*.gz" -daystart -mtime +$DAYS -delete fi - done + done + + ### POSTGRESQL ## + CONTAINER=$(docker ps --format \{\{.Names\}\}:\{\{.Image\}\}| grep 'postgres' | cut -d":" -f1 | grep -v mailcow) + mkdir -p $BACKUPDIR + for i in $CONTAINER + do + # get credentials + POSTGRES_DATABASE=$(docker exec $i env | egrep "POSTGRES_DB" | tail -n1 |cut -d"=" -f2) + POSTGRES_PWD=$(docker exec $i env | egrep "POSTGRES_PASSWORD" | tail -n1 |cut -d"=" -f2) + POSTGRES_USR=$(docker exec $i env | egrep "POSTGRES_USER" | tail -n1 |cut -d"=" -f2) + # get dump path + g_echo "Sichere Datenbank $POSTGRES_DATABASE aus $i"; + docker exec -e PGPASSWORD=$POSTGRES_PWD $i /usr/bin/pg_dump -U $POSTGRES_USR $POSTGRES_DATABASE 2>${g_tmp}/dberr | gzip > $BACKUPDIR/$i-$POSTGRES_DATABASE-$TIMESTAMP.sql.gz || g_echo_error "DB-Backup von $POSTGRES_DATABASE war nicht erfolgreich $(cat ${g_tmp}/dberr)" + # dont delete last old backups! + OLD_BACKUPS=$(ls -1 $BACKUPDIR/$i*.gz |wc -l) + if [ $OLD_BACKUPS -gt $DAYS ]; then + find $BACKUPDIR -name "$i*.gz" -daystart -mtime +$DAYS -delete + fi + done backup: yes validate: /bin/bash -n %s