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.sh /backup.sh
COPY backup_local.sh /backup_local.sh COPY backup_local.sh /backup_local.sh
COPY backup_restic.sh /backup_restic.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" ] ENTRYPOINT [ "/backup.sh" ]

View file

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

View file

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

View file

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