Restic backup script for Wordpress with docker-compose

This is my backup.sh for backing up a Wordpress site with a docker-compose setup using Restic.

See Wordpress docker-compose config with Traefik reverse proxy for the recommended docker-compose config.

In order to perform automatic daily backups using systemd, see How to create a systemd backup timer & service in 10 seconds

See How to install Restic REST server on Synology NAS for more info on how to activate the Restic REST server on a Synology NAS.

Backup script

Ensure to enter the correct username & password (restic and abc123 in this example), the correct IP address (10.1.2.3 in this example) and port (16383 in this example).

Also, create a .restic_password file with the backup encryption password. BACKUP THAT PASSWORD SEPARATELY - you can’t restore your backup without it!

#!/bin/bash
export NAME=$(basename $(pwd))
export RESTIC_REPOSITORY=rest:http://restic:[email protected]:16383/$NAME
export RESTIC_PASSWORD_FILE=.restic_password

if [ ! -f "${RESTIC_PASSWORD_FILE}" ]; then
   echo "Please create .restic_password with the backup encryption password AND BACKUP THAT PASSWORD SEPARATELY!!!"
   exit 1
fi
echo "Initing repo, please ignore any 'already exists' errors"
if [ ! -f ".restic_inited" ]; then
    # Run the restic init command
    restic init

    if [ $? -eq 0 ]; then # if init successful
        # Create the initialization file
        touch ".restic_inited"
        echo "Restic initialized"
    fi
fi

# Backup
source .env
docker-compose exec -T mariadb mysqldump -uroot -p${MARIADB_ROOT_PASSWORD} --all-databases | restic --verbose backup --stdin --stdin-filename="mariadb.sql"
# Backup files
restic --verbose backup backup.sh docker-compose.yml wordpress --exclude wordpress/wp-content/cache