From af731a05e74c8c86b24f16ab6f7087000e7f4fef Mon Sep 17 00:00:00 2001 From: Rob Pearce Date: Sat, 23 Mar 2019 12:35:10 +1100 Subject: [PATCH] Added 'forget' mode to age out old restic backups. When running in cron mode, filter out restic noise. --- bare.sh | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/bare.sh b/bare.sh index 56405a2..8bf9b85 100755 --- a/bare.sh +++ b/bare.sh @@ -1,5 +1,5 @@ #!/bin/bash -VALIDCOMMANDS="go ls init repos stats diff" +VALIDCOMMANDS="go ls init repos stats diff forget" RCFILE=${HOME}/.backup/config AUTHFILE=${HOME}/.backup/auth @@ -57,6 +57,7 @@ function usage-rc() { echo " export RSYNC_USER=backups" echo " export RSYNC_DIR=/home/backups/backups" echo " export RSYNC_OPTIONS=-Pavz" + echo " export KEEPDAYS=31 # days to keep restic backups for" echo " # optional:" echo " #export USMB_PREFIX=/DataVolume/shares" echo " #export NFS_SERVER=nfs.yourdomain.com" @@ -194,6 +195,10 @@ if [[ -z $B2_APP_KEY ]]; then echo "Error - \$B2_APP_KEY not set." exit 1 fi +if [[ -z $KEEPFOR ]]; then + echo "Error - \$KEEPFOR not set." + exit 1 +fi # Convert speed from Mbps to KBps if ! [[ -z $SPEED ]]; then @@ -328,6 +333,7 @@ for f in $REPOSTOBACKUP; do mount | grep -q ${DATAPATH} if [[ $? -ne 0 ]]; then # try mounting it. + mkdir -p $DATAPATH mount -t nfs ${NFSPATH} ${DATAPATH} >/dev/null if [[ $? -eq 0 ]]; then log "Mount nfs volume '$NFSPATH' to $DATAPATH: success" @@ -364,6 +370,24 @@ fi for f in $REPOSTOBACKUP; do REPO=${f} DATAPATH=$(getdatapath $REPO) + checktag "$f" usmb + if [[ $? -eq 0 ]]; then + USMBNAME=`echo "${DATAPATH}" | sed -e "s,${USMB_PREFIX}/\(.*\).*,\1,"` + USEUSMB=1 + else + USMBNAME="" + USEUSMB=0 + fi + + checktag "$f" nfs + if [[ $? -eq 0 ]]; then + NFSNAME=`echo "${DATAPATH}" | sed -e "s,${NFS_PREFIX}/\(.*\).*,\1,"` + NFSPATH="${NFS_SERVER}:${NFS_SERVER_BASE}/${NFSNAME}" + USENFS=1 + else + NFSNAME="" + USENFS=0 + fi export RESTIC_REPOSITORY="b2:${B2_BUCKET_PREFIX}-${REPO}" export RCLONE_REPOSITORY="remote:${B2_BUCKET_PREFIX}-${REPO}" export RSYNC_FULLDIR="${RSYNC_DIR}/${REPO}" @@ -436,7 +460,11 @@ for f in $REPOSTOBACKUP; do if [[ $mode == "restic" ]]; then if [[ $CMD == "go" ]]; then - ${RESTIC} backup $AUTHOPTS $CONNECTIONSOPTS $SPEEDOPTS $DATAPATH 2>&1 >> ${LOG} + if [[ $CRONMODE -eq 1 ]]; then + ${RESTIC} backup $AUTHOPTS $CONNECTIONSOPTS $SPEEDOPTS $DATAPATH 2>&1 | egrep "^(Added|processed|snapshot)" >> ${LOG} + else + ${RESTIC} backup $AUTHOPTS $CONNECTIONSOPTS $SPEEDOPTS $DATAPATH 2>&1 >> ${LOG} + fi rv=$? elif [[ $CMD == "ls" ]]; then ${RESTIC} snapshots $AUTHOPTS $CONNECTIONSOPTS 2>&1 >> ${LOG} @@ -455,6 +483,10 @@ for f in $REPOSTOBACKUP; do else ${RESTIC} init $AUTHOPTS $CONNECTIONSOPTS >/dev/null 2>&1 fi + elif [[ $CMD == "forget" ]]; then + # age out stuff after xxx days + FORGETOPTS="-d ${KEEPDAYS} --prune" + ${RESTIC} forget $AUTHOPTS $CONNECTIONSOPTS $FORGETOPTS 2>&1 >> ${LOG} else log "Error: invalid command $CMD" rv=0 @@ -489,6 +521,8 @@ for f in $REPOSTOBACKUP; do log "Creation of ${RSYNC_FULLDIR} failed." fi fi + elif [[ $CMD == "forget" ]]; then + log "Error: forget not supported for rsync mode." else log "Error: invalid command $CMD" rv=0 @@ -537,6 +571,8 @@ for f in $REPOSTOBACKUP; do log "Rclone bucket creation of $RCLONE_REPOSITORY failed." fi fi + elif [[ $CMD == "forget" ]]; then + log "Error: forget not supported for rclone mode." else log "Error: invalid command $CMD" rv=0