#!/bin/bash # # kbgit-backups-rsync # John Simpson 2025-01-17 # # Use rsync to back up all Keybase git repos the current user has access to. # Note that the backed-up directories will all be BARE repos. They won't be # usable as "working directories", but you can `git clone` from them in order # to access the content stored in the various branches. # ############################################################################### # # MIT License # # Copyright 2025 John Simpson # # 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. # ############################################################################### ############################################################################### # # usage function usage { MSG="${*:-}" cat <&2 fi "$@" } ######################################## # Show the same output that set_x would show, # but don't actually run the command function show_x { if [[ "${SET_X:-false}" == "true" ]] then local IFS=$' ' yellowline "$PS4$*" 1>&2 fi } ############################################################################### ############################################################################### ############################################################################### while getopts ':hx' OPT do case $OPT in h) usage ;; x) SET_X=true ;; *) usage "ERROR: unknown option '-$OPTARG'" ;; esac done shift $((OPTIND-1)) ######################################## # Get the backup directory from the command line. # - make sure it *doesn't* end with "/", this will be added below as needed BACKUP_DIR="${1:-}" if [[ -z "$BACKUP_DIR" ]] then usage fi BACKUP_DIR="${BACKUP_DIR%/}" ############################################################################### # # Find KBFS if [[ -d /keybase/private ]] then KBFS="/keybase" elif [[ -d "/Volumes/Keybase ($( whoami ))" ]] then KBFS="/Volumes/Keybase ($( whoami ))" else echo "ERROR: can't find KBFS, cannot continue" exit 1 fi ############################################################ # Do the deeds for REPO_DIR in $KBFS/*/*/.kbfs_git/* do TLF="${REPO_DIR%/.kbfs_git/*}" # "/keybase/team/TTT" SLF="${TLF#$KBFS/}" # "team/TTT" REPO_NAME="${REPO_DIR#*/.kbfs_git/}" # repo name blueline "keybase://$SLF/$REPO_NAME" if [[ ! -d "$BACKUP_DIR/$SLF/$REPO_NAME" ]] then set_x mkdir -p "$BACKUP_DIR/$SLF/$REPO_NAME" fi set_x rsync -crv "$REPO_DIR/" "$BACKUP_DIR/$SLF/$REPO_NAME/" done