Compare commits

...

4 commits
v3 ... main

Author SHA1 Message Date
d150b7c25e
Fix backup script: remove unnecessary database name from filename
All checks were successful
Build a dev image / build (push) Successful in 11s
Release of a new version / build (release) Successful in 5s
2025-10-18 23:58:07 +02:00
5b172385ca
Enhance backup script: add default history variable and improve filename formatting
All checks were successful
Build a dev image / build (push) Successful in 11s
2025-10-18 23:57:40 +02:00
2ab7be67fa
Add error handling and improve backup scripts for consistency
All checks were successful
Build a dev image / build (push) Successful in 6s
Release of a new version / build (release) Successful in 7s
2025-10-18 23:15:26 +02:00
5bfbda5e5e
Fix typos and improve Docker availability checks in backup scripts
All checks were successful
Build a dev image / build (push) Successful in 11s
2025-10-18 19:42:14 +02:00
4 changed files with 35 additions and 11 deletions

View file

@ -5,6 +5,6 @@ RUN apk add --no-cache restic docker-cli docker-cli-compose curl zstd bash
COPY backup.sh /backup.sh
COPY backup_local.sh /backup_local.sh
COPY backup_restic.sh /backup_restic.sh
RUN chmod +x /backup.sh
RUN chmod +x /backup.sh /backup_local.sh /backup_restic.sh
ENTRYPOINT [ "/backup.sh" ]

View file

@ -1,11 +1,14 @@
#!/bin/bash
set -e
if [ "$1" = "local" ]; then
source /backup_local.sh
elif [ "$1" = "restic" ]; then
source /backup_restic.sh
elif [ "$1" = "loop" ]; then
# Inifinite loop to keep the container running and let Ofelia scheduler (or any other) manage the execution
# Infinite loop to keep the container running and let Ofelia scheduler (or any other) manage the execution
echo "Entering infinite loop mode. Use external scheduler to trigger backups."
while true; do
sleep 86400
done

View file

@ -1,10 +1,13 @@
#!/bin/bash
set -e
# Environment variables
#
# CONTAINER - name of the container where the database is running
# NOTIFY_URL - optional, URL to send notification to
# TARGET_DIR - directory where to store the backup
# HISTORY - number of backups to keep (default: 3)
if [ -z "$CONTAINER" ]; then
echo "CONTAINER environment variable is not set."
@ -16,7 +19,12 @@ if [ -z "$TARGET_DIR" ]; then
exit 1
fi
if [ ! `docker info` ]; then
if [ -z "$HISTORY" ]; then
HISTORY=3
fi
docker info > /dev/null 2>&1
if [ ! $? -eq 0 ]; then
echo "Docker is not available."
exit 1
fi
@ -30,8 +38,9 @@ fi
echo "Starting backup for container: $CONTAINER"
if [ `docker exec -i $CONTAINER test -e /usr/bin/mariadb-dump && echo "yes" || echo "no"` = "yes" ]; then
DBTYPE="mariadb"
FILENAME=`date +"%Y%m%d_%H%M%S"`_$DBTYPE_$CONTAINER_$DB_NAME.sql.zst.tmp
FILENAME=`date +"%Y%m%d_%H%M%S"`_$DBTYPE_$CONTAINER.sql.zst
docker exec -i $CONTAINER mariadb-dump \
--all-databases \
--user=root \
--password=$MARIADB_ROOT_PASSWORD \
--add-drop-trigger \
@ -42,12 +51,12 @@ if [ `docker exec -i $CONTAINER test -e /usr/bin/mariadb-dump && echo "yes" || e
--events \
--routines \
--single-transaction \
--triggers | zstd -1 > $TARGET_DIR/
mv $TARGET_DIR/$DBTYPE_$CONTAINER_$DB_NAME.sql.zst.tmp $TARGET_DIR/$DBTYPE_$CONTAINER_$DB_NAME.sql.zst
--triggers | zstd -1 > $TARGET_DIR/$FILENAME.tmp
mv $TARGET_DIR/$FILENAME.tmp $TARGET_DIR/$FILENAME
elif [ `docker exec -i $CONTAINER test -e /usr/bin/pg_dump && echo "yes" || echo "no"` = "yes" ]; then
DBTYPE="pgsql"
FILENAME=`date +"%Y%m%d_%H%M%S"`_$DBTYPE_$CONTAINER_$DB_NAME.sql.zst
docker exec -i $CONTAINER pg_dump --username=$DB_USER --password=$PGPASSWORD $DB_NAME | zstd -1 > $TARGET_DIR/$FILENAME.tmp
FILENAME=`date +"%Y%m%d_%H%M%S"`_$DBTYPE_$CONTAINER.sql.zst
docker exec -i $CONTAINER pg_dumpall --username=$DB_USER --password=$PGPASSWORD | zstd -1 > $TARGET_DIR/$FILENAME.tmp
mv $TARGET_DIR/$FILENAME.tmp $TARGET_DIR/$FILENAME
else
echo "Unsupported database type or database client not found in the container."
@ -56,6 +65,14 @@ fi
echo "Backup completed successfully."
# Remove old backups
echo "Removing old backups, keeping last $HISTORY backups..."
cd $TARGET_DIR
ls -1t *_$DBTYPE_$CONTAINER.sql.zst | tail -n +$((HISTORY + 1)) | xargs -r rm --
cd -
echo "Old backups removed."
if [ -n "$NOTIFY_URL" ]; then
curl $NOTIFY_URL
fi

View file

@ -1,5 +1,7 @@
#!/bin/bash
set -e
# Environment variables
#
# CONTAINER - name of the container where the database is running
@ -22,7 +24,8 @@ if [ -z "$RESTIC_REPOSITORY" ]; then
exit 1
fi
if [ ! `docker info` ]; then
docker info > /dev/null 2>&1
if [ ! $? -eq 0 ]; then
echo "Docker is not available."
exit 1
fi
@ -31,6 +34,7 @@ echo "Starting backup for container: $CONTAINER"
if [ `docker exec -i $CONTAINER test -e /usr/bin/mariadb-dump && echo "yes" || echo "no"` = "yes" ]; then
DBTYPE="mariadb"
docker exec -i $CONTAINER mariadb-dump \
--all-databases \
--user=root \
--password=$MARIADB_ROOT_PASSWORD \
--add-drop-trigger \
@ -41,10 +45,10 @@ if [ `docker exec -i $CONTAINER test -e /usr/bin/mariadb-dump && echo "yes" || e
--events \
--routines \
--single-transaction \
--triggers | restic backup --stdin --stdin-filename=$DBTYPE_$CONTAINER_$DB_NAME.sql
--triggers | restic backup --stdin --stdin-filename=$DBTYPE_$CONTAINER.sql
elif [ `docker exec -i $CONTAINER test -e /usr/bin/pg_dump && echo "yes" || echo "no"` = "yes" ]; then
DBTYPE="pgsql"
docker exec -i $CONTAINER pg_dump --username=$DB_USER --password=$PGPASSWORD $DB_NAME | restic backup --stdin --stdin-filename=$DBTYPE_$CONTAINER_$DB_NAME.sql
docker exec -i $CONTAINER pg_dumpall --username=$DB_USER --password=$PGPASSWORD | restic backup --stdin --stdin-filename=$DBTYPE_$CONTAINER.sql
else
echo "Unsupported database type or database client not found in the container."
exit 1