Skip to content

Instantly share code, notes, and snippets.

@bendechrai
Last active August 29, 2015 14:03
Show Gist options
  • Save bendechrai/8bbde547642404a4986c to your computer and use it in GitHub Desktop.
Save bendechrai/8bbde547642404a4986c to your computer and use it in GitHub Desktop.
.gnupg file backup helper
#!/bin/bash
# Helper script used to back up your gnupg directory
#
# Author: Ben Dechrai <ben@ctoforhire.com.au>
# License: GPLv3 <http://www.gnu.org/licenses/gpl-3.0.txt>
#
# Installation instructions:
# wget https://gist.githubusercontent.com/bendechrai/8bbde547642404a4986c/raw/8049dcd7def5b2c96c28c7dd3b09933d2d5c9190/gnupg_backup -O ~/.gnupg/backup
if [ -z "$1" ]
then
echo Usage: $0 /path/to/backup
exit
fi
if [ ! -f gpg.conf -o ! -f pubring.gpg -o ! -f random_seed -o ! -f secring.gpg -o ! -f trustdb.gpg ]
then
echo "Couldn't find these files in the current directory: gpg.conf, pubring.gpg, random_seed, secring.gpg, trustdb.gpg"
exit
fi
# Get the current dir name, and move up a directory ready to take a backup
sourcedir="$(cd "$(dirname "$0")"; pwd -P)"
sourcedir="$(basename $sourcedir)"
cd ..
# Rebuild the destination directory to ensure its in an expected format
destdir="$(dirname $1)/$(basename $1)"
if [ ! -d $destdir ]
then
echo $destdir is not a directory
exit
fi
if [ ! -w $destdir ]
then
echo $destdir is not writeable
exit
fi
# Generate the filename once for multiple use
filename="$destdir/gnupg.$(date +%Y-%m-%d.%H-%M-%S).tbz2"
read -p "You're about to back up your public and secret GnuPG data from $sourcedir to $filename. Are you sure this destination is secure and uncompromised? (yN)" yn
case $yn in
[Yy]* ) tar cjfv $filename $sourcedir; echo Backed up to $filename;;
* ) echo "Aborted";;
esac
# Back in to the source directory
cd $sourcedir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment