diff --git a/README.md b/README.md index 596f0bc..b17c801 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,52 @@ -# duplicati +# Duplicati +## Custom Docker Images + +### Duplicati with mySql Client + +[+ Information](docker/mysqlclient/README.md) + +## Scripts + + +### Environment Variables + +_Source: https://github.com/duplicati/duplicati/blob/master/Duplicati/Library/Modules/Builtin/run-script-example.sh_ + +- DUPLICATI__EVENTNAME + + Eventname is BEFORE if invoked as --run-script-before, and AFTER if invoked as --run-script-after. This value cannot be changed by writing it back! + +- DUPLICATI__OPERATIONNAME + + Operation name can be any of the operations that Duplicati supports. For example it can be "Backup", "Cleanup", "Restore", or "DeleteAllButN". This value cannot be changed by writing it back! + +- DUPLICATI__RESULTFILE + + If invoked as --run-script-after this will contain the name of the file where result data is placed. This value cannot be changed by writing it back! + +- DUPLICATI__REMOTEURL + + This is the remote url for the target backend. This value can be changed by echoing --remoteurl = "new value". + +- DUPLICATI__LOCALPATH + + This is the path to the folders being backed up or restored. This variable is empty operations other than backup or restore. The local path can contain : to separate multiple folders. This value can be changed by echoing --localpath = "new value". + +- DUPLICATI__PARSED_RESULT + + This is a value indicating how well the operation was performed. It can take the values: Unknown, Success, Warning, Error, Fatal. + +### Exit Codes + +_Source: https://github.com/duplicati/duplicati/blob/master/Duplicati/Library/Modules/Builtin/run-script-example.sh_ + +The following exit codes are supported: + +- 0: OK, run operation +- 1: OK, don't run operation +- 2: Warning, run operation +- 3: Warning, don't run operation +- 4: Error, run operation +- 5: Error don't run operation +- other: Error don't run operation diff --git a/docker/mysqlclient/Dockerfile b/docker/mysqlclient/Dockerfile new file mode 100644 index 0000000..10b7f8b --- /dev/null +++ b/docker/mysqlclient/Dockerfile @@ -0,0 +1,9 @@ +#check ../README.md for more information + +FROM duplicati/duplicati +RUN apt update && apt install -y default-mysql-client && rm -rf /var/lib/apt/lists/* +RUN mkdir duplicati-mysqlclient +RUN mkdir duplicati-mysqlclient/scripts +COPY /docker/mysqlclient/scripts/* /duplicati-mysqlclient/scripts/ +RUN chmod +x /duplicati-mysqlclient/scripts/* +EXPOSE 8200 \ No newline at end of file diff --git a/docker/mysqlclient/README.md b/docker/mysqlclient/README.md new file mode 100644 index 0000000..1b381e5 --- /dev/null +++ b/docker/mysqlclient/README.md @@ -0,0 +1,47 @@ +# Duplicati / mySql Client + +Using option run-script-before-required on duplicati job set value to /duplicati-mysqlclient/scripts/backupdb.sh. +And job will create a database dump configured using docker environment variables + +## Build Image + +```bash +#Executed on repository root folder +docker build -t duplicati-mysqlclient:latest -f docker/mysqlclient/Dockerfile . +``` + +### Portainer + +Images -> Build a new Image -> URL + +**Name:** duplicati-mysqlclient:latest + +**URL:** https://git.limbosolutions.com/kb/duplicati.git + +**Dockerfile path:** docker/mysqlclient/Dockerfile + +## Run Container + +### Docker Compose + +```yaml +version: "3" +services: + duplicati: + image: duplicati-mysqlclient + environment: + - MYSQL_HOST=${MYSQL_HOST} + - MYSQL_DATABASE=${MYSQL_DATABASE} + - MYSQL_USER=${MYSQL_USER} + - MYSQL_PASSWORD=${MYSQL_PASSWORD} + - DESTINATION=/db-dumps + volumes: + - duplicati-data:/data + - db-dumps:/db-dumps + ports: + - 8200:8200 + restart: unless-stopped +volumes: + duplicati-data: + db-dumps: +```` diff --git a/docker/mysqlclient/scripts/backupdb.sh b/docker/mysqlclient/scripts/backupdb.sh new file mode 100644 index 0000000..25b3aab --- /dev/null +++ b/docker/mysqlclient/scripts/backupdb.sh @@ -0,0 +1,48 @@ +#!/bin/bash +# mysql database backup. +# executed by duplicati run-script-before-required +# required environment variables: +# DESTINATION +# MYSQL_HOST +# MYSQL_USER +# MYSQL_PASSWORD +# MYSQL_DATABASE + +#any error must stop execution +set -e + +if [ $DUPLICATI__OPERATIONNAME == "Backup" ] + echo "Backup Job!!" +else + echo "Not a backup operation. exiting!" + #OK, run operation + exit +fi + + +if [ -z ${DESTINATION} ] +then + echo "\$DESTINATION environment variable cannot be empty empty" + #5: Error don't run operation + exit 5 +fi + +TARGETFILENAME=${MYSQL_NAME}_dump_$(date -d "today" +"%Y%m%d%H%M%S").sql +echo "dump file name:$TARGETFILENAME" + +if [ -f /tmp/${TARGETFILENAME} ] +then + rm /tmp/${TARGETFILENAME} +fi + + +mysqldump -h ${MYSQL_HOST} --user=${MYSQL_USER} -p"${MYSQL_PASSWORD}" ${MYSQL_DATABASE} > /tmp/${TARGETFILENAME} + + +if [ x$(find "$DESTINATION" -prune -empty) = x"$DESTINATION" ]; then + echo "folder is empty... ignoring clean db dump folder" +else + rm -r ${DESTINATION}/* +fi + +mv /tmp/${TARGETFILENAME} ${DESTINATION}/${TARGETFILENAME} diff --git a/docker/scripts/backupdb.sh b/docker/scripts/backupdb.sh deleted file mode 100644 index 9467fc3..0000000 --- a/docker/scripts/backupdb.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash -# Script example for creating a dump -# required environment variables: -# DB_BACKUP_TARGETFOLDER -# DB_HOST -# DB_USER -# DB_PASSWORD -# DB_NAME -# Duplicati UI configure as a pre script -# donwload from git -# curl https://git.limbosolutions.com/kb/duplicati/raw/branch/main/docker/scripts/backupdb.sh > backupdb.sh | chmod +x backupdb.sh - -set -e -if [ -z ${DB_BACKUP_TARGETFOLDER} ] -then - echo "\$DB_BACKUP_TARGETFOLDER cannot be empty empty" - exit -1 -fi - - - -TARGETFILENAME=db_dump_$(date -d "today" +"%Y%m%d%H%M%S").sql - -if [ -f /tmp/${TARGETFILENAME} ] -then - rm /tmp/${TARGETFILENAME} -fi - - -mysqldump -h ${DB_HOST} --user=${DB_USER} -p"${DB_PASSWORD}" ${DB_NAME} > /tmp/${TARGETFILENAME} - - -if [ x$(find "$DB_BACKUP_TARGETFOLDER" -prune -empty) = x"$DB_BACKUP_TARGETFOLDER" ]; then -echo "folder is empty... ignoring clean db dump folder" -else -rm -r ${DB_BACKUP_TARGETFOLDER}/* -fi - -mv /tmp/${TARGETFILENAME} ${DB_BACKUP_TARGETFOLDER}/${TARGETFILENAME} \ No newline at end of file diff --git a/docker/sqlclient.dockerfile b/docker/sqlclient.dockerfile deleted file mode 100644 index afabb01..0000000 --- a/docker/sqlclient.dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM duplicati/duplicati -RUN apt update && apt install -y default-mysql-client && rm -rf /var/lib/apt/lists/* -EXPOSE 8200 \ No newline at end of file