Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

My Photoprism backup process

I have a customized Photoprism instance. I need to document it more thoroughly, but this is the description of my backup process.

Of course it's in cron. File /etc/cron.d/70_photoprism_cron includes line:

45  06  *  *  *  photoprism   /home/photoprism/photoprism/bup-pp-db.sh cron 1>/dev/null 2>&1

files/2023/02/listings/bup-pp-db.sh (Source)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/sh
# File: bup-pp-db.sh
# Locations: vm4:/home/photoprism/photoprism/
# Author: bgstack15
# Startdate: 2023-01-01-1 20:55
# Title: Photoprism backup script
# Purpose: Make single command that bups Photoprism
# History:
# Usage:
#    in cron entry, probably in 
# Documentation:
#    In 2022-12, I moved the directories database/ and storage/ that Photoprism uses back to vm4 local filesystems. To back up the app, I just need to run this script with cron.
#    This process should only just over 1 minute
# Dependencies:
#    photoprism user is in group docker.
#    docker-compose
#    /home/photoprism/photoprism/docker-compose.yml
#    gzip
# Reference:
#    https://docs.photoprism.app/getting-started/advanced/backups/
#    logging heavily inspired by photoprism/autoimport.sh

workdir="$( dirname "$( readlink -f "${0}" 2>/dev/null )" 2>/dev/null || echo "${PWD}" )"
#echo "workdir=${workdir}"
test -z "${CONFFILE}" && CONFFILE="${workdir}/bup-pp-db.conf"
test -e "${CONFFILE}" && . "${CONFFILE}"
test -z "${LOGFILE}" && LOGFILE="/mnt/public/Support/Systems/vm4/var/log/bup-pp-db.$( date "+%F" ).log"

main() {
   which lecho 1>/dev/null 2>&1 || alias lecho=echo
   lecho "START bup-pp-db.sh"
   test -z "${OUTDIR}" && OUTDIR=/mnt/public/Support/Systems/vm4/pp/photoprism
   echo "echo using OUTDIR=${OUTDIR}"
   test "${USER}" != "photoprism" && { echo "For best results, run ${0} as user photoprism. Pausing for 10 seconds..." 2>&1 ; sleep 10 ; }
   # test network mount
   test -w "${OUTDIR}" || { echo "Unable to write to path ${OUTDIR}. Is it mounted? Aborted." ; exit 1 ; }
   # Docker-compose must run from the directory where docker-compose.yml exists
   cd /home/photoprism/photoprism
   docker-compose exec -T photoprism photoprism backup -i - | gzip > "${OUTDIR:-/mnt/public/Support/Systems/vm4/pp/photoprism}/photoprism-db.sql.$( date "+%F" ).gz"
   lecho "STOP bup-pp-db.sh"
}

# Determine if this script was dot-sourced
sourced=0
if [ -n "$ZSH_EVAL_CONTEXT" ]; then 
  case $ZSH_EVAL_CONTEXT in *:file) sourced=1;; esac
elif [ -n "$KSH_VERSION" ]; then
  [ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] && sourced=1
elif [ -n "$BASH_VERSION" ]; then
  (return 0 2>/dev/null) && sourced=1 
else # All other shells: examine $0 for known shell binary filenames
  # Detects `sh` and `dash`; add additional shell filenames as needed.
  case ${0##*/} in sh|dash) sourced=1;; esac
fi

# So, if not dot-sourced, and this is run by cron, add logging
if test $sourced -eq 0;
then
   if echo " ${@} " | grep -q cron ;
   then 
      main 2>&1 | plecho | tee -a "${LOGFILE}"
      printf '\n' | tee -a "${LOGFILE}"
   else
      main
   fi
fi
:

The script refers to a conf file but I don't use one. It's there for easy modification of paths and so on without having to modify the script. Of course, since I'm backing up to a network directory, it checks for the network mount before running the backup command.

Most of the shell script is shell-based wrapping for the bare command

docker-compose exec -T photoprism photoprism backup -i - | gzip > $OUTPUTFILE

which is documented in the Photoprism docs.

Comments