• Backup script

    From john ford@3:770/100 to All on Sat Feb 22 12:41:43 2020
    Backup script i made in bash, uses bash to config the bash script, dirty subarray trick

    script:
    #!/usr/local/bin/bash
    #settings
    DATE=$(date "+%m_%d_%y_%H:%M")
    source ~/.backuprc
    set -euf -o pipefail

    help="$0 {zfs|rsync} {job}"

    backup_rsync( )
    {
    tobed=$(eval echo \${"${rsync[$2]}"["source"]})
    dest=$(eval echo \${"${rsync[$2]}"["destination"]})
    location=$(eval echo \${"${rsync[$2]}"["location"]})
    excludes=$(eval echo \${"${rsync[$2]}"["exclude_file"]})
    basicflags=$(eval echo \${"${rsync[$2]}"["flags"]})
    if [ "$location" == "local" ]
    then
    destination=${locations["$2"]}
    flags="$basicflags"
    else
    if [ "$location" == "ssh" ]
    then
    sshinfo=$(eval echo \${"${rsync[$2]}"["ssh"]})
    server=$(eval echo \${"$sshinfo"["user_server"]})
    identfile=$(eval echo \${"$sshinfo"["identity"]})
    sshport=$(eval echo \${"$sshinfo"["port"]})
    dest="$server:$dest"
    flags="$basicflags -e 'ssh -i $identfile -p $sshport'"
    fi
    fi
    if [[ $excludes != 0 ]]
    then
    flags="$flags --exclude-from=$excludes"
    fi
    if [[ $(eval echo \${"${rsync[$2]}"["delete"]}) != 0 ]]
    then
    flags="$flags --delete"
    fi
    eval "rsync $flags $tobed $dest"
    }

    backup_zfs( )
    {
    tobed=$(eval echo \${"${zfs[$2]}"["source"]})
    destination=$(eval echo \${"${zfs[$2]}"["destination"]})
    location=$(eval echo \${"${zfs[$2]}"["location"]})
    recflags=$(eval echo \${"${zfs[$2]}"["recvflags"]})
    destroyflags=$(eval echo \${"${zfs[$2]}"["destroyflags"]})
    sendflags=$(eval echo \${"${zfs[$2]}"["sendflags"]})
    snapflags=$(eval echo \${"${zfs[$2]}"["snapflags"]})
    incremental=$(eval echo \${"${zfs[$2]}"["incremental"]})
    echo $incremental
    checkdest="zfs list $destination"
    if [ "$location" == "local" ]
    then
    receiver="zfs receive $recflags"
    destroyer="zfs destroy $destroyflags"
    else
    if [ "$location" == "ssh" ]
    then
    sshinfo=$(eval echo \${"${zfs[$2]}"["ssh"]})
    identfile=$(eval echo \${"$sshinfo"["identity"]})
    userserver=$(eval echo \${"$sshinfo"["user_server"]})
    sshport=$(eval echo \${"$sshinfo"["port"]})
    sshline="ssh -t $userserver -i $identfile -p $sshport"
    receiver="$sshline zfs receive $recflags"
    destroyer="$sshline zfs destroy $destroyflags"
    checkdest="$sshline $checkdest"
    fi
    fi
    eval "zfs snapshot $snapflags $tobed@$DATE"

    if [ "$incremental" == "yes" ]
    then
    if eval "$checkdest" && eval "ls ~/.$2.zfs.lastsnap"
    then
    lastsnap=$(cat ~/."$2".zfs.lastsnap)
    sendflags="$sendflags -i $tobed$lastsnap"
    #echo $sendflags
    fi
    echo "@$DATE" > ~/."$2".zfs.lastsnap
    else
    eval "$destroyer $destination"
    fi
    sleep 5
    eval "zfs send $sendflags $tobed@$DATE | $receiver $destination"
    }

    if [ $# == 0 ]
    then
    echo "$help"
    exit
    else
    case $1 in
    rsync)
    backup_rsync "$@"
    exit
    ;;
    zfs)
    backup_zfs "$@"
    exit
    ;;
    *)
    echo "$help"
    exit
    ;;
    esac
    fi

    The "config"
    DELETE=1
    HOMEDIR=/usr/home/mouse/
    HOMEZFS="mouser/usr/home/mouse"
    HOMEEXCLUDE="/usr/home/mouse/.rsync_excludes" GLOBALEXCLUDE="/usr/home/mouse/.rsync_global_excludes" DEFAULTSERVER="192.168.1.138"
    DEFAULTUSER="mouse"

    declare -A home_ssh=(
    ["user_server"]="$DEFAULTUSER@$DEFAULTSERVER"
    ["port"]="1478"
    ["identity"]="~/.ssh/id_rsa_home"
    )

    declare -A ssh=(
    ["home"]=home_ssh
    )

    declare -A full_rsync=(
    ["delete"]=0
    ["source"]="/"
    ["flags"]="-avP"
    ["destination"]="/suplbup/laptop/"
    ["exclude_file"]=$GLOBALEXCLUDE
    ["location"]="local"
    )

    declare -A fullssh_rsync=(
    ["delete"]=1
    ["source"]="/"
    ["flags"]="-avP"
    ["destination"]="/massdata/laptop/"
    ["exclude_file"]=$GLOBALEXCLUDE
    ["location"]="ssh"
    ["ssh"]=home_ssh
    )

    declare -A home_rsync=(
    ["delete"]=1
    ["source"]=$HOMEDIR
    ["flags"]="-avP"
    ["destination"]="/suplbup/laptop-home/"
    ["exclude_file"]=$HOMEEXCLUDE
    ["location"]="local"
    )

    declare -A music_rsync=(
    ["delete"]=0
    ["source"]="/usr/home/mouse/music/"
    ["flags"]="-avP"
    ["destination"]="/home/mouse/Music/"
    ["exclude_file"]=$HOMEEXCLUDE
    ["location"]="ssh"
    ["ssh"]=home_ssh
    )
    declare -A freenet_rsync=(
    ["delete"]=0
    ["source"]="/usr/home/mouse/Freenet/"
    ["flags"]="-avP"
    ["destination"]="/home/mouse/Freenet/"
    ["exclude_file"]=$HOMEEXCLUDE
    ["location"]="ssh"
    ["ssh"]=home_ssh
    )

    declare -A homessh_rsync=(
    ["delete"]=1
    ["source"]=$HOMEDIR
    ["flags"]="-avP"
    ["destination"]="/massdata/laptop-home/"
    ["exclude_file"]=$HOMEEXCLUDE
    ["location"]="ssh"
    ["ssh"]=home_ssh
    )

    declare -A homessh_zfs=(
    ["source"]=$HOMEZFS
    ["recvflags"]=""
    ["snapflags"]="-r"
    ["destroyflags"]="-r"
    ["sendflags"]="-v -R"
    ["destination"]="imagine/test"
    ["location"]="ssh"
    ["incremental"]="yes"
    ["ssh"]=home_ssh
    )

    declare -A home_zfs=(
    ["source"]=$HOMEZFS
    ["recvflags"]=""
    ["snapflags"]="-r"
    ["destroyflags"]="-r"
    ["sendflags"]="-v -R"
    ["destination"]="suplbup/rescuehome"
    ["location"]="local"
    ["incremental"]="yes"
    )

    declare -A full_zfs=(
    ["source"]=mouser
    ["recvflags"]=""
    ["snapflags"]="-r"
    ["destroyflags"]="-r"
    ["sendflags"]="-v -R"
    ["destination"]="suplbup/rescue"
    ["location"]="local"
    ["incremental"]="yes"
    )
    declare -A fullssh_zfs=(
    ["source"]=$HOMEZFS
    ["recvflags"]=""
    ["snapflags"]="-r"
    ["destroyflags"]="-r"
    ["sendflags"]="-v -R"
    ["destination"]="imagine/test"
    ["location"]="ssh"
    ["incremental"]="yes"
    ["ssh"]=home_ssh
    )

    declare -A zfs=(
    ["full"]=full_zfs
    ["full_ssh"]=fullssh_zfs
    ["home"]=home_zfs
    ["home_ssh"]=homessh_zfs
    )

    declare -A rsync=(
    ["music"]=music_rsync
    ["freenet"]=freenet_rsync
    ["full"]=full_rsync
    ["full_ssh"]=fullssh_rsync
    ["home"]=home_rsync
    ["home_ssh"]=homessh_rsync
    )

    --- Mystic BBS v1.12 A45 2020/02/18 (Windows/32)
    * Origin: Agency BBS | Dunedin, New Zealand | agency.bbs.nz (3:770/100)