nbak (1577B)
1 #!/usr/bin/env sh 2 3 # Description: Backup nnn configuration 4 # - config dir content 5 # - environment config 6 # - shell functions and aliases 7 # 8 # Shell: POSIX compliant 9 # Author: Léo Villeveygoux 10 11 nnn_aliases="n nnn" 12 13 outdir="nnn-$(whoami)@$(hostname)" 14 15 outfile="${outdir}.tar.bz2" 16 17 shellname="$(basename "$SHELL")" 18 19 conffile="config.txt" 20 21 configdir="${XDG_CONFIG_HOME:-$HOME/.config}/nnn" 22 23 workdir="$PWD" 24 25 tempdir="$(mktemp -d)" 26 27 mkdir "$tempdir/$outdir" 28 29 if [ ! -d "$tempdir" ]; then 30 echo "Can't create work directory." >&2 31 exit 1 32 fi 33 34 cd "$tempdir/$outdir" || exit 1 35 36 # Backing up config dir content 37 cp -r "$configdir" . || exit 1 38 39 # Environment config 40 env | sed "s/'/'\\\\''/" |\ 41 awk '/^NNN_/{print "export '\''"$0"'\''"}' > "$conffile" 42 43 # Shell functions/aliases 44 case "$shellname" in 45 bash) 46 for name in $nnn_aliases ; do 47 if [ "$(bash -ic "type -t $name")" = "function" ] ; then 48 bash -ic "type $name" | tail -n+2 >> "$conffile" 49 elif bash -ic "alias $name" >/dev/null 2>&1 ; then 50 bash -ic "alias $name" >> "$conffile" 51 fi 52 done 53 ;; 54 zsh) 55 for name in $nnn_aliases ; do 56 if zsh -ic "functions $name" ; then 57 zsh -ic "functions $name" >> "$conffile" 58 elif zsh -ic "alias $name" ; then 59 echo alias "$(zsh -ic "alias $name")" >> "$conffile" 60 fi 61 done 62 ;; 63 64 *) 65 echo "Unknown shell, skipping alias/function checking." >&2 66 ;; 67 esac 68 69 cd .. || exit 1 70 71 printf "Saving as '%s' ... " "$workdir/$outfile" 72 73 tar caf "$workdir/$outfile" "$outdir" && echo "Done" || echo "Failed" 74 75 cd "$workdir" && rm -rf "$tempdir"