Compare commits

...

2 Commits

2 changed files with 97 additions and 76 deletions

View File

@ -1,14 +1,17 @@
# required format:
# SOURCE TARGET KEEPDAYS ENABLED
# dataset [user@host:]dataset number Y or N
# SOURCE TARGET KEEPDAYS MODE ENABLED
# dataset [user@host:]dataset number backup or sync Y or N
# we are securing this elsewhere; if we are using this method, we'll need to configure the target host
# 'user' needs an authorized and restricted public key for SSH access and sudoer rights on 'remotehost' to execute the following as root:
# /usr/sbin/zfs recv -Feuv datapool/backup, /usr/sbin/zfs destroy -r datapool/backup/*
data/home user@remotehost:datapool/backup 7 Y
data/home user@remotehost:datapool/backup 7 backup Y
# this goes to another pool on this host so no further configuration is necessary
data/config backup/data 5 Y
data/config backup/data 5 backup Y
# we no longer back this up
data/volatile backup/data 2 N
data/volatile backup/data 2 backup N
# we only sync this one
data/replica remote/dump 31 sync Y

View File

@ -76,6 +76,8 @@ function backup() {
R_SNAPSHOTS=( $(${R_RMOD} zfs list -rt snapshot -d1 -Ho name -S creation ${SAVETO}/$( basename ${DATASET}) 2>/dev/null) )
check_dataset ${SAVETO}/$( basename ${DATASET}) && R_USED_BEFORE=$(snapuse ${SAVETO}/$( basename ${DATASET}))
if [ $MODE == "backup" ]
then
# determine current timestamp
DATE=$(date +%Y-%m-%d-%H%M)
@ -84,6 +86,7 @@ function backup() {
# take a snapshot
zfs snapshot -r ${NEWSNAP}
fi
# check if source is encrypted
if [ $ENCRYPTION_FEATURE != "disabled" ]
@ -107,22 +110,32 @@ function backup() {
if [ ${#R_SNAPSHOTS[*]} -ge 1 ]
then
# remote snapshot(s) found
R_SNAPMODIFIER="I $(dirname ${DATASET})/$(basename ${R_SNAPSHOTS[*]:(-1)})"
R_SNAPMODIFIER="I $(dirname ${DATASET})/$(basename ${R_SNAPSHOTS[0]})"
fi
# send any previous snapshots
zfs send -Rcv${RAW_MOD}${R_SNAPMODIFIER} ${LASTSNAP} | ${RMOD:-$R_RMOD} zfs recv -Feu${RESUME_MOD}v ${SAVETO} 2>&1 >> ${LOGFILE}
if [[ ${PIPESTATUS[*]} =~ [1-9]+ ]]
then
# replication failure
logger -t $(basename ${0%.sh}) -p user.notice "failed to replicate ${LASTSNAP} to ${TARGET:-local}:${SAVETO}"
else
let CHANGES++
fi
}
else
# ensure this does not remain in effect
unset SNAPMODIFIER R_SNAPMODIFIER
fi
if [ $MODE == "backup" ]
then
# send backup
zfs send -Rcv${RAW_MOD}${SNAPMODIFIER} ${NEWSNAP} | ${RMOD:-$R_RMOD} zfs recv -Feu${RESUME_MOD}v ${SAVETO} 2>&1 >> ${LOGFILE}
# if replication is unsuccessful, omit the aging check so as to prevent data loss
if [ $? -eq 0 ]
if [[ ! ${PIPESTATUS[*]} =~ [1-9]+ ]]
then
let CHANGES++
THRESHOLD=$(( $KEEP * 24 * 3600 ))
for SNAPSHOT in ${SNAPSHOTS[*]}
do
@ -135,8 +148,8 @@ function backup() {
fi
done
else
MY_EXIT_CODE=$?
logger -t $(basename ${0%.sh}) -p user.notice "failed to replicate ${NEWSNAP} (rc: $MY_EXIT_CODE) to ${TARGET:-local}:${SAVETO}, no aging"
logger -t $(basename ${0%.sh}) -p user.notice "failed to replicate ${NEWSNAP} to ${TARGET:-local}:${SAVETO}, no aging"
fi
fi
# re-evaluate snapshot data usage
@ -148,6 +161,9 @@ function backup() {
R_RMOD="ssh ${USER}@${TARGET}"
fi
if [ ${CHANGES:-0} -gt 0 ]
then
# calculate data volume change
R_USED_AFTER=$(snapuse ${SAVETO}/$( basename ${DATASET}))
L_DELTA=$(( $L_USED_AFTER - $L_USED_BEFORE ))
R_DELTA=$(( $R_USED_AFTER - ${R_USED_BEFORE:-0} ))
@ -176,6 +192,7 @@ function backup() {
logger -t $(basename ${0%.sh}) -p user.notice "$_DELTA ${WHAT:-allocated} in \"${WHERE}\" by backing up \"${DATASET}\""
fi
done
fi
# reset remote configuration
unset R_RMOD RMOD RAW_MOD
@ -274,12 +291,13 @@ LOGDIR="/var/log"
# determine session logfile
LOGFILE="${LOGDIR:-/tmp}/${ME}_${RUNDATE}.txt"
while read -u 4 DATASET SAVETO KEEP ENABLED
while read -u 4 DATASET SAVETO KEEP MODE ENABLED
# alternate file descriptor in use because SSH might be involved and we can not pass '-n' to it because we need stdin for 'zfs recv'
do
let COUNTER++
if [ ${ENABLED} == "Y" ]
then
# split this function -> snapshot(), sync(), age() ? -> a new backup() would call all three
backup
fi
done 4<<< "$(egrep -v '^(#|$)' "${CFGFILE}")" # graciously overlook any comments or blank lines