Skip to content

Instantly share code, notes, and snippets.

@silvasur
Created April 10, 2011 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silvasur/912277 to your computer and use it in GitHub Desktop.
Save silvasur/912277 to your computer and use it in GitHub Desktop.
Easily create encrypted backups of your Bitcoin wallet
#!/bin/bash
# backup-bitcoins.sh - Easily create encrypted backups of your Bitcoin wallet
# You need this tools / programs to run backup-bitcoins.sh:
# * 7z (usually in the package 7zip)
# * uuidgen (usually already installed)
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright (C) 2004 Sam Hocevar
# 14 rue de Plaisance, 75014 Paris, France
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
CONFFILE=~/.backup-bitcoins
touch $CONFFILE
get_passwd() {
while true; do
echo -n "Password: " >&2
read -s passwd1
echo -ne "\nConfirm Password: " >&2
read -s passwd2
echo -ne "\n" >&2
if [ "$passwd1" == "$passwd2" ]; then
echo "$passwd1"
passwd1=""
passwd2=""
break
else
sleep 2
echo "Passwords do not match. Try again." >&2
fi
done
}
crypt_wallet() {
WALLETFILE="$1"
NEWFILE="$2"
PASSWORD="$3"
7z a -p"$PASSWORD" "$NEWFILE" "$WALLETFILE"
PASSWORD=""
}
backup() {
PASSWD=`get_passwd`
TEMPFILE="/tmp/$(uuidgen -r).7z"
crypt_wallet ~/.bitcoin/wallet.dat "$TEMPFILE" "$PASSWD"
for destination in `cat "$CONFFILE"`; do
while true; do
if cp -f "$TEMPFILE" "$destination"; then
echo "Backed up to $destination"
break
else
echo -ne "Could not copy to $destination. Retry?\ny/n: " >&2
read yesno
case "$yesno" in
n|N|no) break; ;;
*) continue; ;;
esac
fi
done
done
rm -f "$TEMPFILE"
PASSWD=""
}
list_destinations() {
count=0
for destination in `cat "$CONFFILE"`; do
echo "#$count: $destination"
count="$[$count+1]"
done
}
delete_destination() {
mv "$CONFFILE" "$CONFFILE.old"
total=`wc -l < "$CONFFILE.old"`
head -n $1 "$CONFFILE.old" > "$CONFFILE"
tail -n "$[$total-($1+1)]" "$CONFFILE.old" >> "$CONFFILE"
}
add_destination() {
for destination in `cat "$CONFFILE"`; do
if [ "$destination" == "$1" ]; then return; fi
done
echo "$1" >> "$CONFFILE"
}
short_usage() {
prgname=`basename "$1"`
echo "Usage: $prgname command [additional parameters]" >&2
}
offer_help() {
prgname=`basename "$1"`
echo "Type \"$prgname help\" for more inforamtion." >&2
}
case $1 in
add|add_destination)
if [ "$2" == "" ]; then
echo "$0: Command $1 needs one additional parameter." >&2
offer_help
exit 1
else
add_destination "$2"
fi
;;
delete|remove|delete_destination|remove_destination)
if [ "$2" == "" ]; then
echo "$0: Command $1 needs one additional parameter." >&2
offer_help
exit 1
else
delete_destination "$2"
fi
;;
list|list_destinations)
list_destinations
;;
backup)
backup
;;
help)
short_usage
cat <<-FOO
Where command is one of these:
add|add_destination new_destination
adds the new_destination to the backup destinations
list
lists all backup destinations
delete|remove|delete_destination|remove_destination index
deletes the destination with the index from the backup destinations
backup
backup the bitcoin wallet
help
print this help
FOO
;;
*)
prgname=`basename "$0"`
echo "$prgname: Unknown command: $1" >&2;
short_usage "$0"
offer_help "$0"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment