Pass backup name as variable to before/after script

I want to create a before/after script to stop docker containers, was wondering if there is a duplicati environment variable that passes the backup job name that can be used in the script?

Example:
Backup Name: Plex
Environment variable: DUPLICATI__[JOBNAME] = “Plex”

This way I can use the backup name in a variable in the script to do something specific based on the backup name passed to the script.

There is DUPLICATI__backup_name which is set to the name of the backup.

Some other variables (but it seems not this one) are documented in the manual article.

You could also try to use remoteurl or local path for this.

Thank you @Jojo-1000 that is working perfectly. :slight_smile:

<<<=== Backup
Script start: Sun Jun 25 09:07:36 PM CAT 2023
Duplicati job name: cloudcmd
Finding docker container: cloudcmd
Duplicati running BEFORE event script
Found container: cloudcmd
Stopping container: cloudcmd
Container cloudcmd running state: false
Container cloudcmd stopped…
Script stopped: Sun Jun 25 09:07:49 PM CAT 2023
===>>>
<<<=== Backup
Script start: Sun Jun 25 09:08:15 PM CAT 2023
Duplicati job name: cloudcmd
Finding docker container: cloudcmd
Duplicati running AFTER event script
Found container: cloudcmd
Starting container: cloudcmd
Container cloudcmd running state: true
Container cloudcmd started…
Script stopped: Sun Jun 25 09:08:17 PM CAT 2023
===>>>

The script if anyone is interested.

Script is meant to backup individual containers based on the container name. Make sure to name the backup job the same as your docker container.

#!/bin/bash
logfile=/config/docker_backup.log # /cofig is the location within duplicati docker _data directory

# Get duplicati job name which should be same as docker container name for script to work
dupdocker=${DUPLICATI__backup_name}
#dupdocker=ombi

# Get duplicati script event: BEFORE/AFTER
dupevent=${DUPLICATI__EVENTNAME}
#dupevent=BEFORE

# Get duplicati operation event: "Backup", "Cleanup", "Restore", or "DeleteAllButN"
dupopevent=${DUPLICATI__OPERATIONNAME}

if [ $dupopevent == "Backup" ]; then
    printf "<<<=== ${dupopevent}\n" | tee -a $logfile
    printf "Script start: $(date)\n" | tee -a $logfile
    printf "Duplicati job name: ${dupdocker}\n" | tee -a $logfile

    printf "Finding docker container: ${dupdocker}\n" | tee -a $logfile
    runcont=$(docker ps -a -f name=${dupdocker} --format '{{.Names}}')

    # Check if duplicati event if BEFORE script
    if [ $dupevent == "BEFORE" ]; then
    printf "Duplicati running BEFORE event script\n" | tee -a $logfile

    # Check if docker container was found
    if [ $dupdocker == $runcont ]; then
        printf "Found container: ${runcont}\n" | tee -a $logfile
        printf "Stopping container: ${runcont}\n" | tee -a $logfile

        # Stop running docker container
        $(docker stop $runcont > /dev/null 2>&1)

        # Check state of docker container
        docstate=$(docker inspect -f {{.State.Running}} $runcont)
        printf "Container ${runcont} running state: ${docstate}\n" | tee -a $logfile

        if [ $docstate == "false" ]; then
        printf "Container ${runcont} stopped...\n" | tee -a $logfile
        else
        printf "Container ${runcont} still running, should be stopped!!!\n" | tee -a $logfile
        fi

    else
        printf "No container found named: ${dupdocker}\n" | tee -a $logfile
    fi
    else
    printf "Duplicati running AFTER event script\n" | tee -a $logfile

    # Check if docker container was found
    if [ $dupdocker == $runcont ]; then
        printf "Found container: ${runcont}\n" | tee -a $logfile
        printf "Starting container: ${runcont}\n" | tee -a $logfile

        # Start stopped docker container
        $(docker start $runcont > /dev/null 2>&1)

        # Check state of docker container
        docstate=$(docker inspect -f {{.State.Running}} $runcont)
        printf "Container ${runcont} running state: ${docstate}\n" | tee -a $logfile

        if [ "$docstate" == "true" ]; then
        printf "Container ${runcont} started...\n" | tee -a $logfile
        else
        printf "Container ${runcont} still not running, should be started!!!\n" | tee -a $logfile
        fi
    else
        printf "No container found named: ${dupdocker}\n" | tee -a $logfile
    fi
    fi
    printf "Script stopped: $(date)\n" | tee -a $logfile
    printf "===>>>\n" | tee -a $logfile
fi

Note: This is my first bash script, so use at own risk :slight_smile:

check https://github.com/Sn00zEZA/docker/tree/main/duplicati for updates to the script