Different approach to handling multiple repositories

I have written a bash script to help deal with the problems surrounding repositories which are unavailable to the UI.

If you want to code your own, here are the two relevant commands:

mv repository-<repo_name>.config repository-<repo_name>.config.INACTIVE

and

mv repository-<repo_name>.config.INACTIVE repository-<repo_name>.config

Of course, to be safe to use (this is about backups, after all) you need a lot of scaffolding, so here’s my script, which I called krepo and which is MIT-licenced. Share and enjoy.

#!/bin/bash

# ---

# The MIT License (MIT)
# 
# Copyright (c) 2024 John Botha
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# 

# ---

# This rests on the premise that Kopia ignores repository definitions with file names it sees as invalid. Thus we can hide repositories from Kopia so that unavailable ones aren't continually looked for on the network and don't clutter up the tray menu with useless entries.
#
# There is a fly in the ointment though: the file called "repository.config" must always exist. The reason I wrote this script is so that I can hide unavailable repositories, so to deal with this I made "repository.config" point to "$HOME/DONOTBACKUP/DUMMY-REPOSITORY/", which my standard ignore rules will ignore. I changed its description from "My Repository" to "DUMMY Repository", which helps minimise confusion.
#
# Real bummer: during testing I discovered that renaming "repository-XXX.config" to "INACTIVE.repository-XXX.config" would not hide it from Kopia, while renaming it to "repository-XXX.config.INACTIVE" would. It thus seems that its pattern matching is from the end. The reason this is a bummer is that if I could prepend "INACTIVE." then all the inactive repositories would be grouped together in a file listing, but now they're all over the place, making it more difficult to see at a glance.
#
# The script only works on repository files fitting this naming convention: "repository-*.config" and "repository-*.config.INACTIVE".

# ---

E_Help=11
E_Kopia=12;			T_Kopia="Kopia is running. Exiting."
E_Gone=13;			T_Gone=" does not exist. Exiting."

ConfDir="$HOME/.config/kopia"

# ---

ByeDie() {
  # Call with exit code to use, plus optional message.
  # If called with a message, display it on stdout. If in a terminal, wait for a key-press.
  STAGE_LEFT=$1
  1>&2 # All output to stderr from here to end
  shift
  # Display Text?
  [ $# -ge 1 ] && DT=1 && /usr/bin/echo "$@" || DT=0
  # Deal with being in a terminal
  [ -t 1 -a $STAGE_LEFT -ne 0 -a $DT -eq 1 ] \
  && read -n1 -sp "Press any key to exit..." JUNK && echo
  trap "" EXIT # Otherwise calling ByeDie() triggers the next trap (unexpected error)
  exit $STAGE_LEFT
} # ByeDie()

# This traps all errors, including those bypassing ByeDie.
trap \
'
  EC=$?
  [ $EC -eq 0 -o $EC -eq $E_Help ] \
  && ByeDie $EC \
  || ByeDie $EC Unexpected error caught: $EC
' EXIT

# ---

HelpBye() {
  cat <<EOH
SYNOPSIS
`basename $0` {activate|deactivate} {REPOSITORY_NAME}
`basename $0` {list}

Change the state of a Kopia repository (activate or deactivate) in $ConfDir. Alternatively, list the repositories and whether they are marked as INACTIVE.

To minimise the chance of disaster, no Kopia processes may be running when you de/activate a repository.
EOH
  exit $E_Help
} # HelpBye()

#---

ActivateRepository() {
  rfa="repository-$1.config"  # Repository file, active
  [ -s $rfa ] && exit 0  #  Already activated

  rfi="repository-$1.config.INACTIVE"  # Repository file, inactive
  [ ! -s $rfi ] && ByeDie $E_Gone Repository $1 $T_Gone  # Deactivated file does not exist.

  mv $rfi $rfa
} # ActivateRepository()

# ---

DeactivateRepository() {
  rfi="repository-$1.config.INACTIVE"  # Repository file, inactive
  [ -s $rfi ] && exit 0  # Already deactivated

  rfa="repository-$1.config"  # Repository file, active
  [ ! -f $rfa ] && ByeDie $E_Gone Reposotory $1 $T_Gone  # File does not exist

  mv $rfa $rfi
} # DeactivateRepository

# ---

ListRepositories() {
  for f in repository-*.config.INACTIVE repository-*.config
  do
    [ ! -e $f ] && continue  # Nothing to see here; move along.
    fn="`echo $f | cut -d - -f2`"
	if [[ "$fn" == *.INACTIVE ]] ; then
	  fn="`basename $fn .config.INACTIVE`"
	  echo $fn.INACTIVE
    else
	  basename $fn .config
	fi
  done
} # ListRepositories()

# ---

CanRun() {
  pgrep kopia >/dev/null 2>&1
  [ $? -eq 0 ] && ByeDie $E_Kopia $T_Kopia
  return 0
} # CanRun()

# ---

cd $ConfDir

case $1 in
  activate)
	CanRun && ActivateRepository $2
    ;;
  deactivate)
	CanRun && DeactivateRepository $2
    ;;
  list)
	ListRepositories
    ;;
  *)
	HelpBye
    ;;
esac