git_infra

Git infra scripts for git.bracken.jp
git clone https://git.bracken.jp/git_infra.git
Log | Files | Refs | LICENSE

backup_git.bracken.jp.sh (1434B)


      1 #!/bin/sh
      2 
      3 # Backup script for git repos, typically invoked via a cron job on the backup
      4 # server.
      5 #
      6 # Invoke with a backup tag, typically "daily", "weekly", "monthly" and the
      7 # number of previous backups to be retained. Older backups that fall out of
      8 # retention are deleted.
      9 
     10 # Fail if any step fails, so we don't collect half-baked backups.
     11 set -e
     12 
     13 TAG=$1
     14 RETAIN_COUNT=$2
     15 
     16 # Check script arguments.
     17 if [ -z "$TAG" -o -z "$RETAIN_COUNT" ]; then
     18   echo "usage: $0 BACKUP_TAG RETAIN_COUNT"
     19   exit 1
     20 fi
     21 
     22 USER=git
     23 HOST=git.bracken.jp
     24 BACKUP_SRC_DIR="/home/git"
     25 BACKUP_SRC_FILES='*.git git_infra'
     26 BACKUP_OUT_DIR="/volume1/Backups/git.bracken.jp/$TAG"
     27 BACKUP_OUT_FILE="$(date +"%Y-%m-%d_%H.%m").txz"
     28 
     29 # Create the new backup.
     30 echo "[git backup] Backup of $HOST:$BACKUP_SRC_DIR ($TAG) starting"
     31 echo "[git backup] Backing up files: $BACKUP_SRC_FILES"
     32 mkdir -p "$BACKUP_OUT_DIR"
     33 ssh $USER@$HOST                                       \
     34   tar Jcf - -C "$BACKUP_SRC_DIR" "$BACKUP_SRC_FILES"  \
     35   > "$BACKUP_OUT_DIR/$BACKUP_OUT_FILE"
     36 echo "[git backup] Wrote archive $BACKUP_OUT_DIR/$BACKUP_OUT_FILE"
     37 
     38 # Delete archives past the retain count.
     39 EXPIRED_ARCHIVES="$(find "$BACKUP_OUT_DIR" -maxdepth 1 -type f  \
     40                     | sort -r | tail -n +$(($RETAIN_COUNT + 1)))"
     41 for f in $EXPIRED_ARCHIVES; do
     42   echo "[git backup] Deleting expired archive: $f"
     43   rm -f "$f"
     44 done
     45 
     46 echo "[git backup] Backup of $HOST:$BACKUP_SRC_DIR ($TAG) complete"