Compare commits

..

No commits in common. "main" and "v3" have entirely different histories.
main ... v3

4 changed files with 11 additions and 35 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 /backup_local.sh /backup_restic.sh
RUN chmod +x /backup.sh
ENTRYPOINT [ "/backup.sh" ]

View file

@ -1,14 +1,11 @@
#!/bin/bash
set -e
if [ "$1" = "local" ]; then
source /backup_local.sh
elif [ "$1" = "restic" ]; then
source /backup_restic.sh
elif [ "$1" = "loop" ]; then
# 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."
# Inifinite loop to keep the container running and let Ofelia scheduler (or any other) manage the execution
while true; do
sleep 86400
done

View file

@ -1,13 +1,10 @@
#!/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."
@ -19,12 +16,7 @@ if [ -z "$TARGET_DIR" ]; then
exit 1
fi
if [ -z "$HISTORY" ]; then
HISTORY=3
fi
docker info > /dev/null 2>&1
if [ ! $? -eq 0 ]; then
if [ ! `docker info` ]; then
echo "Docker is not available."
exit 1
fi
@ -38,9 +30,8 @@ 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.sql.zst
FILENAME=`date +"%Y%m%d_%H%M%S"`_$DBTYPE_$CONTAINER_$DB_NAME.sql.zst.tmp
docker exec -i $CONTAINER mariadb-dump \
--all-databases \
--user=root \
--password=$MARIADB_ROOT_PASSWORD \
--add-drop-trigger \
@ -51,12 +42,12 @@ if [ `docker exec -i $CONTAINER test -e /usr/bin/mariadb-dump && echo "yes" || e
--events \
--routines \
--single-transaction \
--triggers | zstd -1 > $TARGET_DIR/$FILENAME.tmp
mv $TARGET_DIR/$FILENAME.tmp $TARGET_DIR/$FILENAME
--triggers | zstd -1 > $TARGET_DIR/
mv $TARGET_DIR/$DBTYPE_$CONTAINER_$DB_NAME.sql.zst.tmp $TARGET_DIR/$DBTYPE_$CONTAINER_$DB_NAME.sql.zst
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.sql.zst
docker exec -i $CONTAINER pg_dumpall --username=$DB_USER --password=$PGPASSWORD | zstd -1 > $TARGET_DIR/$FILENAME.tmp
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
mv $TARGET_DIR/$FILENAME.tmp $TARGET_DIR/$FILENAME
else
echo "Unsupported database type or database client not found in the container."
@ -65,14 +56,6 @@ 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,7 +1,5 @@
#!/bin/bash
set -e
# Environment variables
#
# CONTAINER - name of the container where the database is running
@ -24,8 +22,7 @@ if [ -z "$RESTIC_REPOSITORY" ]; then
exit 1
fi
docker info > /dev/null 2>&1
if [ ! $? -eq 0 ]; then
if [ ! `docker info` ]; then
echo "Docker is not available."
exit 1
fi
@ -34,7 +31,6 @@ 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 \
@ -45,10 +41,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.sql
--triggers | restic backup --stdin --stdin-filename=$DBTYPE_$CONTAINER_$DB_NAME.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_dumpall --username=$DB_USER --password=$PGPASSWORD | restic backup --stdin --stdin-filename=$DBTYPE_$CONTAINER.sql
docker exec -i $CONTAINER pg_dump --username=$DB_USER --password=$PGPASSWORD $DB_NAME | restic backup --stdin --stdin-filename=$DBTYPE_$CONTAINER_$DB_NAME.sql
else
echo "Unsupported database type or database client not found in the container."
exit 1