summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbackup_repos.sh41
1 files changed, 41 insertions, 0 deletions
diff --git a/backup_repos.sh b/backup_repos.sh
new file mode 100755
index 0000000..e34d1ea
--- /dev/null
+++ b/backup_repos.sh
@@ -0,0 +1,41 @@
+#!/usr/bin/env sh
+
+export GIT_TERMINAL_PROMPT=0
+BASE=/mnt/ssd-data/repos/
+
+pick_branch() {
+ git -C "$1" for-each-ref --format='%(refname:short)' 'refs/remotes/origin/*' \
+ | sed 's#^origin/##' \
+ | grep -E '^(main|master|dev)$' \
+ | head -n1
+}
+
+repos=$(2>1 ssh git.l-3.space | awk '/tslil\// {print $2}')
+
+echo Found $repos
+printf "\n\n"
+
+for repo in $repos; do
+ REMOTE="ssh://git.l-3.space/$repo.git"
+ DIR="$BASE/$repo"
+ echo Looking at "$REMOTE" for "$DIR"
+ if [ -d "$DIR/.git" ]; then
+ echo "Syncing existing repo $DIR from $REMOTE ..."
+ if git -C "$DIR" fetch -q origin \
+ && BRANCH="$(pick_branch "$DIR")" && [ -n "$BRANCH" ] \
+ && git -C "$DIR" reset --hard -q "origin/$BRANCH" \
+ && git -C "$DIR" clean -fdxq; then
+ echo "OK: $DIR synced to remote from $BRANCH."
+ else
+ echo "Sync failed, removing $DIR and re-cloning..."
+ rm -rf "$DIR"
+ git clone -q "$REMOTE" "$DIR"
+ echo "OK: $DIR re-cloned from $REMOTE."
+ fi
+ else
+ echo "No repo at $DIR, cloning..."
+ git clone -q "$REMOTE" "$DIR"
+ echo "OK: $DIR cloned from $REMOTE."
+ fi
+ printf "\n\n"
+done