Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions db-container-backup/db-container-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ DAYS=2
BACKUPDIR=/home/xcad/backup


# backup all mysql/mariadb containers
# backup all mysql/mariadb containers and all databases

CONTAINER=$(docker ps --format '{{.Names}}:{{.Image}}' | grep 'mysql\|mariadb' | cut -d":" -f1)

Expand All @@ -21,19 +21,35 @@ if [ ! -d $BACKUPDIR ]; then
fi

for i in $CONTAINER; do
MYSQL_DATABASE=$(docker exec $i env | grep MYSQL_DATABASE |cut -d"=" -f2)

MYSQL_PWD=$(docker exec $i env | grep MYSQL_ROOT_PASSWORD |cut -d"=" -f2)

docker exec -e MYSQL_DATABASE=$MYSQL_DATABASE -e MYSQL_PWD=$MYSQL_PWD \
$i /usr/bin/mysqldump -u root $MYSQL_DATABASE \
| gzip > $BACKUPDIR/$i-$MYSQL_DATABASE-$(date +"%Y%m%d%H%M").sql.gz
docker exec -e MYSQL_PWD=$MYSQL_PWD $i /usr/bin/mysqldump \
--all-databases --ignore-database=mysql -u root \
| gzip > $BACKUPDIR/$i-$(date +"%Y%m%d%H%M").sql.gz

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 all postgres containers and all databases

POSTGRES_CONTAINER=$(docker ps --format '{{.Names}}:{{.Image}}' | grep 'postgres' | cut -d":" -f1)

for i in $CONTAINER; do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. You are checking again the same variable and not POSTGRES_CONTAINER

It should be :

for i in $POSTGRES_CONTAINER; do

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, here i made a type. You are rigth, thanks.


POSTGRES_USER=$(docker exec $i env | grep POSTGRES_USER |cut -d"=" -f2)

docker exec -t $i pg_dumpall --exclude-database=template1 \
-c -U $POSTGRES_USER | gzip > $BACKUPDIR/$i-$(date +"%Y%m%d%H%M").sql.gz

OLD_POSTGRES_BACKUPS=$(ls -1 $BACKUPDIR/$i*.gz |wc -l)
if [ $OLD_POSTGRES_BACKUPS -gt $DAYS ]; then
find $BACKUPDIR -name "$i*.gz" -daystart -mtime +$DAYS -delete
fi
done

# bitwarden backup

Expand Down