#!/usr/bin/env bash
usage() {
cat << ENDPOD

=head1 NAME

    backup_nas - back up FSs on a NAS using zfs copy

=head1 SYNOPSIS

    zfs_replication_bin/backup_nas {filesystem1 filesystem2} 

=head1 DESCRIPTION

Find the filesystems on the current NAS using zfs list, then back them
up to datacenter1 or datacenter2 using replication_ABC.sh

Unless names of FSs are specified on the command line, the script
attempts to identify FSs to back up from zfs list. We exclude the NAS
box's own OS and similar FSs.

The script much be run from a directory (~mohan/sysinfo) where
zfs_replication_bin and directories like {destnas}/{sourcenas}
exist. The responsibility for getting to that directory must lie
outside this script, because how each source nas mounts the homedirs
and cds to that directory is idiosyncratic. All paths referenced
within this script are relative to that directory.

Logs go in {destnas}/{sourcenas}/{fs}/backup_{fs}.log

The "tag" used by replication_ABC.sh (used to name snapsnots) is
backup_{fs} . This allows the same snapshots to be used even if the
source and destination NAS boxes change in the future.

=cut

ENDPOD
}

if [ ! -d "zfs_replication_bin" ]; then
    echo "This script must be run from ~mohan/sysinfo"
    exit -1
fi

fspat=''
IFS='|'
fspat="$*"
unset IFS

hostname=`hostname`
if [[ $hostname =~ ^age ]]; then
    isephi=1
    destnas='datacenter2'
else
    isephi=0
    destnas='datacenter1'
fi

if [[ $hostname =~ ^([a-zA-Z0-9]+) ]]; then
    sourcenas=${BASH_REMATCH[1]}
else
    echo hostname starts with strange characters
    exit -1
fi
    
dontbackup='(\.system|agingadmin_20210529|iocage|nfs_diag|vm_restore2|vm_backups2|tmp|usr|var|ROOT|tank/ekx1[[:space:]])'

echo "sourcenas:$sourcenas destnas:$destnas fspat:$fspat dontbackup:$dontbackup";

DIR="$destnas/$sourcenas";

# set IFS to newline only
IFS="
"
for line in $(zfs list)
do
    name=""
    if [[ $line =~ ^(e?tank.?|zroot|pool.)/([_a-zA-Z0-9]+) ]]; then
        name=${BASH_REMATCH[2]}
    else
        continue
    fi

    if [[ $line =~ ^tank.?/(ek[a-z0-9]+|[_a-zA-Z0-9]+_replication)/([_a-zA-Z0-9]+) ]]; then
        name=${BASH_REMATCH[2]}
    fi
    # special exception
    if [ "x$name" = "xagedisk7a" ]; then
        name='agedisk7'
    fi
    if [[ $line =~ $dontbackup ]]; then
#        echo skipping $name because it matches dontbackup
        continue
    fi
#    echo name:$name
    if [ "x$fspat" != "x" ]; then
        if [[ $name =~ ^($fspat)$ ]]; then
            # do this one
            echo backing up $name
        else
            continue
        fi
    fi

    [[ $line =~ ^([_/a-zA-Z0-9]+) ]]
    fullname=${BASH_REMATCH[1]}
    
    LOGDIR="$DIR/$name"

    echo $line
    echo name:$name fullname:$fullname LOGDIR:$LOGDIR
    
    test -d $LOGDIR || mkdir $LOGDIR
    LOGFILE="$LOGDIR/backup_${name}.log"
    LFS=$fullname
    RFS=$fullname
    if [ $sourcenas = 'ns1' ]; then

        #        RFS=`echo $RFS | sed -e 's/zroot/...;`

        # I think the way this works is the double forward slash
        # kicks off the substitution during interpolation
        # then the next forward slash after that means
        # separate the pattern from the replacement
        # then any slashes after that are literally
        # interpreted as slash characters in the replacement string
        
        # change this? Maybe not for ns1.
        RFS=${RFS//zroot/tank/ns1_replication}
        
        #        $RFS =~ s!zroot!tank/ns1_replication!;
    elif [ $sourcenas = 'freenas2' ]; then
        # Change this to ekx3 or something
        RFS=${RFS//tank/tank/freenas2_replication}
    elif [ $sourcenas = 'agetruenas7' ]; then
        echo not set up yet
        exit -1
    fi
    TAG="backup_${name}"
    SSHOPT=${SSHOPT:-" -i /data/ssh/replication"}
    SENDOPT="zfssendopt=${ZFSSENDOPT:- -w }"
    RECVOPT="zfsrecvopt="
    # if ... then set send/recv opts on specific FSs/NASs?
    echo About to run replication cmd:
    echo ./zfs_replication_bin/replication_ABC.sh "${LFS}" "${destnas}.nber.org" "${RFS}" "${TAG}" "${SSHOPT}" "${SENDOPT}" "${RECVOPT}" '>>' $LOGFILE '2>&1 &'
    
         ./zfs_replication_bin/replication_ABC.sh "${LFS}" "${destnas}.nber.org" "${RFS}" "${TAG}" "${SSHOPT}" "${SENDOPT}" "${RECVOPT}" >> $LOGFILE 2>&1 &
    
done

