My Docker Volume Backup 1.0

Backup and compress your docker volumes with crontab, jq, 7z and bash

  1. pigers
    Written over QTS 4.4.2

    Settings file - DockerVolumeBackupSettings.json
    Code (Javascript):
    1. {
    2.    "Docker":{
    3.     "Emby":"/share/CACHEDEV1_DATA/Container/container-station-data/lib/docker/volumes/emby_config/_data/BACKUP/",
    4.     "Portainer":"/share/CACHEDEV1_DATA/Container/container-station-data/lib/docker/volumes/portainer/",
    5.     "Qbittorrent":"/share/CACHEDEV1_DATA/Container/container-station-data/lib/docker/volumes/qbit/",
    6.     "Radarr":"/share/CACHEDEV1_DATA/Container/container-station-data/lib/docker/volumes/radarr/_data/Backups/",
    7.     "Sonarr":"/share/CACHEDEV1_DATA/Container/container-station-data/lib/docker/volumes/sonarr/_data/Backups/",
    8.     "Lidarr":"/share/CACHEDEV1_DATA/Container/container-station-data/lib/docker/volumes/lidarr/_data/Backups/",
    9.     "Jackett":"/share/CACHEDEV1_DATA/Container/container-station-data/lib/docker/volumes/jackett/",
    10.     "SmokePing_Data":"/share/CACHEDEV1_DATA/Container/container-station-data/lib/docker/volumes/smokeping_data/",
    11.     "SmokePing_Config":"/share/CACHEDEV1_DATA/Container/container-station-data/lib/docker/volumes/smokeping_config/",
    12.     "JDownloader":"/share/CACHEDEV1_DATA/Container/container-station-data/lib/docker/volumes/jdownloader_config/",
    13.     "NetBootXYZ_Config":"/share/CACHEDEV1_DATA/Container/container-station-data/lib/docker/volumes/netbootxyz_config/",
    14.     "NextCloud_config":"/share/CACHEDEV1_DATA/Container/container-station-data/lib/docker/volumes/nextcloud_config/"
    15. },
    16.     "BackupLocation":"/share/CACHEDEV2_DATA/DATA2/export/DockerVolumes/",
    17.     "BackupsToKeep":"7",
    18.     "Compression7z":"-mx=9",
    19.     "Archiver":"/usr/local/sbin/7z"
    20. }
    Script - DockerVolumeBackup.sh (do not forget to chmod +x DockerVolumeBackup.sh !)
    Code (Bash):
    1. #!/usr/bin/env bash
    2. #expand PATH VARIABLE .. just in case
    3. PATH=$PATH:/usr/local/sbin/
    4. command -v jq >/dev/null 2>&1 || { echo "I require jq but it's not installed.  Aborting." >&2; exit 1; }
    5.  
    6. #get current date in file name friendly format
    7. cdate=$(date +%Y-%m-%d_%H-%M)
    8.  
    9. #Settings for backup
    10. JSON="/share/CACHEDEV2_DATA/DATA2/autoruns/DockerVolumeBackupSettings.json"
    11. # check if file exists
    12. if [ ! -f $JSON ]; then
    13. echo "Cannot locate file with settings data ! Aborting !" >&2;
    14. exit 1;
    15. fi
    16.  
    17. compression_level=$(jq -r '.Compression7z' $JSON)
    18. archiver=$(jq -r '.Archiver' $JSON)
    19.  
    20. # set backup location
    21. BACKUP_LOCATION=$(jq -r '.BackupLocation' $JSON)
    22. # get all dockers to backup from JSON
    23. DOCKERS=$(jq -r '.Docker | keys[] as $k | "\($k)"' $JSON)
    24. # convert to bash array
    25. array=($DOCKERS)
    26. # loop over array and do magic
    27. for i in ${array[@]}; do
    28. DOCKER_NAME="$i"
    29. DOCKER_PATH=$(jq -r ".Docker.$i" $JSON)
    30. # build command to execute
    31. CMD="$archiver a $compression_level $BACKUP_LOCATION$cdate"_"$DOCKER_NAME.7z $DOCKER_PATH"
    32. echo "Executing -> "$CMD
    33. #execute it ?
    34. eval $CMD
    35. done
    36. #keep last X backups
    37. BACKUPS_TO_KEEP=$(jq -r '.BackupsToKeep' $JSON)
    38. echo "Keeping last $BACKUPS_TO_KEEP backups in $BACKUP_LOCATION..."
    39. find $BACKUP_LOCATION -type f -mtime +$BACKUPS_TO_KEEP -delete
    Add to qnap's crontab line (example):
    0 4 * * * /share/CACHEDEV2_DATA/DATA2/autoruns/DockerVolumeBackup.sh 1>/dev/null 2>&1 and do not forget to reload crontab service afterwards !