Knowledge Base

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

bup-pp-db.sh (Source)

#!/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
: