aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorGravatar lvgx <l@vgx.fr>2020-04-23 19:35:58 +0200
committerGravatar GitHub <noreply@github.com>2020-04-23 23:05:58 +0530
commit7dab9d0d86730f2953edb3c38187ac022b55ec96 (patch)
tree32794be4a21851bb0e02e79cf49d8bfc69a346c2 /plugins
parent00148360e4452ae24a43a15b0833c49b7b4e4347 (diff)
downloadnnn-7dab9d0d86730f2953edb3c38187ac022b55ec96.tar.gz
Add nbak plugin to backup all nnn config (#528)
* Add nbak plugin to backup all nnn config * nbak: check cd, quote env * nbak: print backup file name * nbak: add --show option, to show shell config * nbak: fix shellcheck warning 'type' is POSIX complient, AND we check that we're actually running bash, but shellcheck can't understand this... Then '-o' is POSIX complient too, but shellcheck thinks it's "not well defined". * nbak: variable renames, archive hierarchy changes * nbak: fix variable expansion * nbak: remove --show option * nbak: call interactive bash/zsh to get fun/aliases * Add nbak entry in plugins/README.md * nbak: change archive hierarchy * plugins/README.md: make nbak description shorter
Diffstat (limited to 'plugins')
-rw-r--r--plugins/README.md1
-rwxr-xr-xplugins/nbak73
2 files changed, 74 insertions, 0 deletions
diff --git a/plugins/README.md b/plugins/README.md
index 109c7a8..9cafd3e 100644
--- a/plugins/README.md
+++ b/plugins/README.md
@@ -50,6 +50,7 @@ Plugins are installed to `${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins`.
| moclyrics | Show lyrics of the track playing in moc | sh | [ddgr](https://github.com/jarun/ddgr), [moc](http://moc.daper.net/) |
| mocplay | Append (and/or play) selection/dir/file in moc | sh | [moc](http://moc.daper.net/) |
| mp3conv | Extract audio from multimedia as mp3 | sh | ffmpeg |
+| nbak | Backs up `nnn` config | sh | tar, awk |
| nmount | Toggle mount status of a device as normal user | sh | pmount, udisks2 |
| nuke | Sample file opener (CLI-only by default) | sh | _see in-file docs_ |
| oldbigfile | List large files by access time | sh | find, sort |
diff --git a/plugins/nbak b/plugins/nbak
new file mode 100755
index 0000000..8ff5474
--- /dev/null
+++ b/plugins/nbak
@@ -0,0 +1,73 @@
+#!/usr/bin/env sh
+
+# Description: Backup of all nnn config
+#
+# Shell: POSIX compliant
+# Author: Léo Villeveygoux
+
+nnn_aliases="n nnn"
+
+outdir="nnn-$(whoami)@$(hostname)"
+
+outfile="${outdir}.tar.bz2"
+
+shellname="$(basename "$SHELL")"
+
+conffile="config.txt"
+
+configdir="${XDG_CONFIG_HOME:-$HOME/.config}/nnn"
+
+workdir="$PWD"
+
+tempdir="$(mktemp -d)"
+
+mkdir "$tempdir/$outdir"
+
+if [ ! -d "$tempdir" ]; then
+ echo "Can't create work directory." >&2
+ exit 1
+fi
+
+cd "$tempdir/$outdir" || exit 1
+
+# Backing up config dir content
+cp -r "$configdir" . || exit 1
+
+# Environment config
+env | sed "s/'/'\\\\''/" |\
+ awk '/^NNN_/{print "export '\''"$0"'\''"}' > "$conffile"
+
+# Shell functions/aliases
+case "$shellname" in
+ bash)
+ for name in $nnn_aliases ; do
+ if [ "$(bash -ic "type -t $name")" = "function" ] ; then
+ bash -ic "type $name" | tail -n+2 >> "$conffile"
+ elif bash -ic "alias $name" >/dev/null 2>&1 ; then
+ bash -ic "alias $name" >> "$conffile"
+ fi
+ done
+ ;;
+ zsh)
+ for name in $nnn_aliases ; do
+ if zsh -ic "functions $name" ; then
+ zsh -ic "functions $name" >> "$conffile"
+ elif zsh -ic "alias $name" ; then
+ echo alias "$(zsh -ic "alias $name")" >> "$conffile"
+ fi
+ done
+ ;;
+
+ *)
+ echo "Unknown shell, skipping alias/function checking." >&2
+ ;;
+esac
+
+cd .. || exit 1
+
+printf "Saving as '%s' ... " "$workdir/$outfile"
+
+tar caf "$workdir/$outfile" "$outdir" && echo "Done" || echo "Failed"
+
+cd "$workdir" && rm -rf "$tempdir"
+