#!/bin/bash # # kbgit-backup # John Simpson 2023-06-25 # # Make/update backups of all Keybase git repos you have access to # # 2024-01-26 jms1 - report errors but don't fully stop because of them # # 2025-01-18 jms1 - run 'git fetch --tags' in addition to 'git fetch --all' # ############################################################################### # # MIT License # # Copyright 2023-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. # ############################################################################### TARGET="" ############################################################################### function redline { if [[ -t 1 ]] then printf "\e[0;1;37;41m%s\e[0K\e[0m\n" "$*" else echo "***** $* *****" fi } function yellowline { if [[ -t 1 ]] then printf "\e[0;30;43m%s\e[K\e[0m\n" "$*" else echo "===== $* =====" fi } function blueline { if [[ -t 1 ]] then printf "\e[0;1;37;44m%s\e[0K\e[0m\n" "$*" else echo "===== $* =====" fi } function greenline { if [[ -t 1 ]] then printf "\e[0;1;37;42m%s\e[0K\e[0m\n" "$*" else echo "----- $* -----" fi } SET_X="${SET_X:-false}" function set_x { if [[ "${SET_X:-false}" == "true" ]] then local IFS=$' ' yellowline "$PS4$*" 1>&2 fi "$@" } ############################################################################### function usage { MSG="${*:-}" cat < $TDIR" echo '' ######################################## # If the repo's target directory already exists # - update the repo if [[ -d "$TDIR" ]] then if ! ( set_x cd "$TDIR" && set_x git fetch --all && set_x git fetch --tags ) then redline "ERROR, backup image was not updated" ERR_LIST+=( "git fetch failed: $URL" ) fi ######################################## # If the repo's target directory doesn't already exist # - clone the repo else if ! set_x git clone --mirror "$URL" "$TDIR" then redline "ERROR, backup image was not cloned" ERR_LIST+=( "git clone failed: $URL" ) fi fi done ######################################## # Recap errors if [[ "${#ERR_LIST[@]}" > 0 ]] then echo '' redline "List of repos with errors" echo '' for E in "${ERR_LIST[@]}" do echo "$E" done fi