Shorter fast gpg invocation
[dotfiles.git] / bin / bin / zsnap.zsh
index a14979b750616313e817e7b73f5e939bd655b92b..f9f97024a7d9c62aefa4d8d814f1fd476158f1e2 100755 (executable)
@@ -1,65 +1,73 @@
-#!/bin/zsh -e
+#!/usr/bin/env zsh
+
+set -e
 
 usage() {
-cat <<EOF
+    cat <<EOF
 Usage: $0 DATASET@SNAPPREFIX snap-initial
        $0 DATASET@SNAPPREFIX snap
        $0 DATASET@SNAPPREFIX sync-initial DST
        $0 DATASET@SNAPPREFIX sync DST
        $0 DATASET@SNAPPREFIX clean COUNT
 EOF
-exit 1
+    exit 1
 }
 
-enccmd="gpg --compress-level 0 --encrypt --recipient offline --cipher-algo AES-128"
+enccmd="/home/stargrave/bin/gpg-e-fast.sh --recipient offline"
 
-[ $# -ge 2 ] || usage
+[[ $# -ge 2 ]] || usage
 
-what="$1"
-action="$2"
+what=$1
+action=$2
 
 now=$(date "+%Y%m%d%H%M")
-snaps=$(zfs list -t snap -d 1 -o name -H | grep $what | sort -nr)
-latest=$(echo $snaps | sed -n 1p)
+depth=${#${(s./.)what}}
+snaps=$(zfs list -t snap -d $depth -o name -H | grep $what | sort -nr)
+snaps=(${(f)snaps})
+latest=${snaps[1]}
+latest_filename=${latest:gs#/#%}
 
-case "$action" in
+case $action in
     snap-initial)
         zfs snap -r $what-$now
-        echo $what-$now
+        print $what-$now
         ;;
     snap)
-        [ -n "$latest" ]
-        [ "$what-$now" != "$latest" ]
+        [[ -n $latest ]]
+        [[ "$what-$now" != $latest ]]
         zfs snap -r $what-$now
-        echo $what-$now
+        print $what-$now
         ;;
     sync-initial)
-        dst="$3"
-        [ -d "$dst" ] || usage
-        set PIPE_FAIL
-        zfs send -Rv $latest | zstd | ${=enccmd} > $dst/$latest.zfs.zst.gpg
+        dst=$3
+        [[ -d $dst ]] || usage
+        setopt PIPE_FAIL
+        zfs send -Rv $latest | zstd | ${=enccmd} > \
+            $dst/$latest_filename.zfs.zst.gpg
         sync
-        touch $dst/$latest.from
+        touch $dst/$latest_filename.from
         ;;
     sync)
-        dst="$3"
-        [ -d "$dst" ] || usage
-        latest_dst=$(find $dst -type f -name "*.from" | sort -rn | sed -n 1p)
-        [ -n "$latest_dst" ]
-        latest_dst=$(basename ${latest_dst%.from})
-        [ "$latest_dst" != "$latest" ]
-        set PIPE_FAIL
-        zfs send -Rv -i $latest_dst $latest | zstd | ${=enccmd} > $dst/$latest.zfs.zst.gpg
+        dst=$3
+        [[ -d $dst ]] || usage
+        latest_dst=($dst/*(.Onn[1]))
+        [[ $latest_dst ]]
+        latest_dst=${latest_dst[1]}
+        latest_dst=${${latest_dst##*/}%.from}
+        [[ $latest_dst != $latest_filename ]]
+        setopt PIPE_FAIL
+        zfs send -Rv -i ${latest_dst:gs#%#/} $latest | zstd | ${=enccmd} > \
+            $dst/$latest_filename.zfs.zst.gpg
         sync
-        echo "$latest_dst" > $dst/$latest.from
+        print $latest_dst > $dst/$latest_filename.from
         ;;
     clean)
-        count="$3"
-        [ -n "$count" ] || usage
+        count=$3
+        [[ -n $count ]] || usage
         count=$(( $count + 1 ))
-        for snap in $(echo $snaps) ; do
+        for snap in $snaps ; do
             count=$(( $count - 1 ))
-            [ $count -le 0 ] || continue
+            [[ $count -le 0 ]] || continue
             zfs destroy -Rv $snap
         done
         ;;