Bitcoin Forum
June 19, 2024, 06:47:16 AM *
News: Voting for pizza day contest
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: A Script for backing up wallets...  (Read 687 times)
mintymark (OP)
Sr. Member
****
Offline Offline

Activity: 286
Merit: 251


View Profile
July 19, 2014, 02:36:28 PM
 #1

This is a script for backing up wallets for possibly multiple coins from the BitcoinQT client/wallet.

Its a basic script - makes backups when needed and not otherwise and keeps 10 backups which are rotated per coin.
There is the possibility to rsync the entire backup to another machine.

Change the config settings near the start, satisfy yourself that there is nothing untoward going on (there isn't but you should look at the script before you use it anyway!!)
Run it from your crontab every half hour or every few days.

You can do wallback.sh -v for additional info
and/or
wallback.sh -n for a dry run.

Here is the script:

Enjoy ;-)

Code:
#!/bin/bash

######################################################################
# This script is meant to back up crypto coin wallets
# whenever they change. It makes a backup only if the file changes
# It rotates the backups through a fied number (nb) that you can set below.
######################################################################
##  configuration:  ##
# number of backups for each coin
nb=10
# These are the coins you want to backup
# each must be the prices directory that exists in
# $HOME, eg $HOME/.bitcoin/wallet.dat
# note the capitalisation carefully
coinlist="bitcoin AuroraCoin HoboNickels namecoin litecoin devcoin" 
netback=mark@myotherpc # empty for no network backup
terminalonly= # set this to disable crontab running perhaps during debug. Dontforget to reset!! 
               # This STOPS all crontab invocations. 
               
## end of configuration ##

# warn about terminalonly if set and exit if needed.
if [[ "$terminalonly" ]]
then
  if [ ! -t 1 ]
  then
    echo "Terminal only at the moment - aborting "
    exit
  else
    echo
    echo "**** Terminal only is currently SET"
  fi
fi 

# command line arguments
verb=0
while getopts "vn" o; do
    case "${o}" in
        v)
            verb=1 # verbose
            # verb=2 # ultra verbose
            ;;
        n)  dryrun="# DRYRUN"
            ;;
        *)
            echo "$0: usage error!"
            exit 1
            ;;
    esac
done
shift $((OPTIND-1))
#dryrun="# DRYRUN"

# new coins - add to this list - the name must be the name of the $HOME/.name directory
for coin in $coinlist
do

[[ $verb -ge 2 ]] && echo "Processing coin $coin"

src=/home/mark/.$coin/wallet.dat

mkdir -p ~/wallback/$coin

# This is the newest file
n=$(ls -rt1 ~/wallback/$coin/* 2> /dev/null | tail -1)
n=${n%%.*}
n=${n##*wallet}
new=$n ; # eg "00"
[[ $verb -ge 2 ]] && echo "new=$new"


# this is file to be overwritten in the case of many files.
n=$(ls -t1 ~/wallback/$coin/* 2> /dev/null | tail -1)
n=${n%%.*}
n=${n##*wallet} # eg "08"
[[ $verb -ge 2 ]] && echo "next overwrite file=$n"


# file count
nc=$(ls -1 ~/wallback/$coin/* 2> /dev/null | wc -l )
[[ -z "$nc" ]] && nc=0
[[ $verb -ge 2 ]] && echo "filecount = $nc"


# too few files so we need a new one ?
# search out any gaps that are missing and create
if [[ $nc -lt $nb ]]
then
 [[ $verb -ge 2 ]] && echo "Too few files $nc < $nb , we need a new one"
 cc=0
 while  [[ $cc -lt $nb ]]
 do
  let cc=$cc+1
  let nc=$((10#$nc))+1
  if [[ $nc -ge $nb ]]
  then
   let nc=$nc-$nb
  fi
  nc=$(printf "%02d" $nc)
  f=~/wallback/$coin/wallet$nc.dat
  [[ -e "$f" ]] || break
 done
 # should now have non existant file
  n=$nc
fi
 
f=~/wallback/$coin/wallet$n.dat
[[ $verb -ge 2 ]] && echo "n is $n file is $f"
# f, n are now new file

# $((10#$n)) sets up arithmetic context so 09 and 9 both give 9 , otherwise 09 gets interpreted as invalid octal number! 
if [[ $((10#$n)) -ge $nb ]]
then
 let n=$n-$nb
fi
n=$(printf "%02d" $((10#$n)))
f=~/wallback/$coin/wallet$n.dat

if [[ ! -z "$new" ]]
then
  f=~/wallback/$coin/wallet$new.dat
  if [[ $src -nt $f ]]
  then
    [[ $verb -ge 1 ]] && echo "$dryrun cp  $src  $f"
    [[ "$dryrun" ]] || cp  "$src"  "$f"
    flag=1
  else
    [[ $verb -ge 1 ]] && echo "$coin - No copy"
  fi
else
  [[ $verb -ge 1 ]] && echo "$dryrun cp  $src $f"
  [[ "$dryrun" ]] || cp  "$src"  "$f"
  flag=1
fi

done

# This does an rsync to a local network.
# it only copies if changes have been made
if [[ "$flag" ]] && [[ ! -z $netback ]]
then
 [[ $verb ]] && vv='-v'
 rsync $vv -a ~/wallback/ $netback:wallback || echo "Rsync failed exit code $?"
fi


[[ All Tips gratefully received!!  ]]
15ta5d1N8mKkgC47SRWmnZABEFyP55RrqD
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!