instagram arrow-down
Kalle Lilja

Archives

Automated wallet.dat Backup

Keep your wallet.dat file safe with daily backups

Your wallet.dat file is the single most important file on your system in relation to your coin stockpile, it’s the file containing your set of keys, your coins. If this file were to get lost, or corrupted in any way, you’re doomed. That’s right: you’ve lost every single last one of your hard-earned coins.

The single best way of preventing the the misfortune of loosing your, well, fortune, is to back up this particular file, even better is to back it up to external media, such as a USB stick, and even better than that is to automate the whole process.
It’s also Very important to have the wallet encrypted with a passphrase prior to backup it up. If not, anyone could get a hold of your file, by for example swiping your usb sick, and as such, all your coins.

If it were to hit the fan, the recovery process is not all that intimidating, download your wallet software on the new computer, start it to get it going, close it again and replace the blank wallet.dat with your backed up one.
Windows: %AppData%\CoinName\wallet.dat.
Linux: ~\.CoinName\wallet.dat.
Mac: ~/Library/Application Support/CoinName/wallet.dat.

I’ll be using a plain and simple USB2.0 stick for my backups. I’d recommend formatting your drive and dedicating it to wallet backup. Formatting your drive ERASES ALL CONTENTS on it. Do not format the wrong drive and make sure to copy anything off the drive that you want to keep. FAT32 file system will do just fine.

I’ll be backing up my FakeCoin wallet, if you’re following this guide for any other coin, replace FakeCoin with your CoinName.

Windows

The backups will be performed by a .bat file and automated using Windows Task Scheduler Taskschd.msc.

WalletBackup.bat

Plug in your USB stick and navigate to it, D: in my example, Right click and select New > Text Document, you can name the file whatever you want, this will be deleted later on.
Populate the file with the following;

REM Copies the wallet.dat from FakeCoin to targetfolder wallet-backups
copy %AppData%\FakeCoin\wallet.dat wallet-backups
REM Renames the copied wallet.dat to YYYY-MM-DD_HH-MM-SS-wallet.dat
ren wallet-backups\wallet.dat "%date:~,4%-%date:~5,2%-%date:~8,2%_%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%-wallet.dat"
REM Keep only the last 14 files, as to not fill up the drive
for /f "skip=14 eol=: delims=" %%F in ('dir /b /o-d /a-d *.dat') do @del "%%F"

Save the file using Save As.., name it “WalletBackup.bat”, select “File-Type: All Files (*.*)”, save it in the root of the USB stick, where you created the original text file.
Go ahead and create the target folder wallet-backups in the same directory as well.

You can already perform the backup by double clicking the WalletBackup.bat, give it a go.
Remove the original text document, it’s no longer needed.

Task Scheduler

Running the backup manually is all well and good, but let’s automate this. In Windows we can use the built in tool Task Scheduler to, well, run tasks on a schedule. This will let us have the computer perform the double click action on the .bat file, for example daily at 21:00.
Open up Task Scheduler, Start – Windows Administrative Tools – Task Scheduler. (Or, Run – Taskschd.msc)
Select “Create Task…” in the top right corner.
General;
Give the task a name, and a description if desired. Leave the rest as default.
Triggers;
Select when to run the task.
On a schedule, Daily, recur every 1 days, 21:00:00. For example.
Actions;
This is were we’ll tell the task what to do, ie, double click on the WalletBackup.bat file.
Start a program, Program/script: “D:\WalletBackup.bat”, Start in: “D:\”

Leave everything else at default. Save the task.

That’s it, the backup script is now scheduled to run daily at 21:00:00 keeping your wallet.dat safe, secure, and up to date on the D:\ USB Stick.

Optional: autorun.inf

Using the autorun.inf file we can give the USB stick a custom icon, keeping it separate from any other USB stick.
Create a new text file much in the same way as when creating the backup. Paste in the following;

[autorun] 
icon=FAK-BACKUP.ico,0
label=FAK-BACKUP

You can probably guess whats about to happen here, save the file using Save As.., name it “autorun.inf”, select “File-Type: All Files (*.*)”, save it in the root of the USB stick, where you created the original text file.

The file will replace the original Drive icon of the USB drive with the FAK-BACKUP.ico file, now, where would we get a FAK-BACKUP.ico icon to use? We’ll have to create one.
I’ll be using the service realfavicongenerator to convert This image.
Upload the image to the service, download the package, extract the package, rename the favicon.ico file to FAK-BACKUP.ico, and place is in the root if the USB drive with the other files.
Unplug and plug back in your USB Drive.
Remove the original text document, it’s no longer needed.

Linux – Ubuntu

The backups will be performed by a .sh script and scheduled to run daily using crontab.
Original Source.

backupwallet.sh

Create the file /usr/local/bin/backupwallet.sh using the texteditor of your preference, I’ll be using nano.
sudo nano /usr/local/bin/backupwallet.sh
Populate the file with the following; (Adjust paths as necessary)

#!/bin/bash
# /usr/local/bin/backupwallet.sh
#
# Performs backup of FakeCoin wallet.
#

#
# Standard Options
#
TS=$(date "+%Y%m%d-%H%M")
WALLET=/tmp/wallet-${TS}
COINDAEMON=/usr/bin/fakecoind  # /path/to/fakecoind
RM=rm
RM_OPTS='--force'
USE_SHRED=0  # Flip to 1 to use `shred` instead of `rm`.
SHRED=shred
SHRED_OPTS='--force --iterations=9 --zero --remove'

#
# Storage Options
#
# CP - Storage on a local machine. Could be Dropbox/Google Drive folder.
CP=cp
CP_DEST='/media/username/USBSTICK/wallet-backups'  # '~/Dropbox/', etc.
#


do_clean() {
  # Remove temporary wallets.
  if [ 1 -eq $USE_SHRED ]; then
    $SHRED $SHRED_OPTS $WALLET
  else
    $RM $RM_OPTS $WALLET
  fi
}

do_fail() {
  do_clean
  echo failed!
  exit 1
}

# Perform the backup.
echo -n Making backup...
$COINDAEMON backupwallet $WALLET
[ ! -s "$WALLET" ] && do_fail  # If the backup does not exist or is empty, fail.
echo done.
echo -n Copying to backup location...
$CP $WALLET "$CP_DEST"
[ 0 -ne $? ] && do_fail  # If the $CP command returns a non-zero result, fail.
echo done.
do_clean
echo -n Destination cleanup...
ls -tr "$CP_DEST" | head -n -14 | xargs -d '\n' rm
echo done.

exit 0

CTRL-X – Y – enter to save with nano.

We’ll have to perform a few changes to the file in order to have it be run able by my user username, username is the user with the wallet to be backed up.
sudo chown username:username /usr/local/bin/backupwallet.sh && sudo chmod 755 /usr/local/bin/backupwallet.sh
As well as create the target folder.
mkdir /media/username/USBSTICK/wallet-backups

You can already perform the backup by running /usr/local/bin/backupwallet.sh, give it a go. /usr/local/bin/backupwallet.sh
Make sure fakecoind is running
The output will be a new file in your wallet-backup directory. wallet-YYYYMMDD-HHMM.

Cronjob

Scheduling a task to run in Linux is done using cron, this is managed by crontab.
Start your crontab, crontab -e, select 2 to configure with nano.
Add the following to the bottom of the file and save;

0 21 * * * /usr/local/bin/backupwallet.sh

That’s it, the backup script is now scheduled to run daily at 21:00:00 keeping your wallet.dat safe, secure, and up to date on the /media/kalle/USBSTICK USB Stick.

Mac

The backups will be performed by an automator workflow script and scheduled to run daily using crontab.

Automator

Automator is a GUI tool to design workflows and runbooks, as such there will be a bit of clicking involved.
Start off my finding “Automator” using spotlight, or navigating to Applications/Automator.

Create a new Workflow.
The workflow we’re about to create will find the wallet.dat file, copy it to the USB stick, find the newly copied file and rename it.
All of this can be accomplished within the Library/Files & Folders section.

Step 1, Find finder items,
The wallet.dat file will be located under ~/Library/Application Support/FakeCoin/wallet.dat, select your users home directory as the search location.
Query for ‘name contains “wallet.dat”‘.
Select Results, and hit Step in the top right to test the query.
If you have multiple wallets installed, you’ll have to get creative here.

Step 2, Copy Finder Items,
Select a target folder, for example USBSTICK/wallet-backup.
(Create the folder if it’s not present already).
Step through the task again, make sure wallet.dat is copied over to the usb stick.

Step 3, Find Finder Items
Set the Search folder to the same directory as used as target in the previous step, USBSTICK/wallet-backup.
Query for ‘name is “wallet.dat”‘.

Step 4, Rename Finder Items,
The goal here is to move away from the file having the plain name wallet.dat.
How you choose to add the timestamp is up to you.
I’ve gone for YYY-MM-DD_wallet.dat.

Save the Workflow. ~/WalletBackup.workflow.

You can already perform the backup by running workflow, give it a go by using the Run button in the top right corner of Automator.

Cronjob

Scheduling this workflow to run on it’s own can be done in a few ways, I’ll be using the cron, but it can even be done by iCal.

Open a new Terminal window and start off my setting nano as the default editor, export EDITOR=nano
Open up your crontab, crontab -e
Type in the following; (Swap username and path as necessary)

# Run WalletBackup Daily at 21:00
00 21 * * * automator /Users/username/WalletBackup.workflow > /User/username/cronlogs/WalletBackup.log

CTRL-X – Y – enter to save with nano.

Next up, create the log folder, mkdir /User/username/cronlogs

That’s it, the backup workflow is now scheduled to run daily at 21:00 keeping your wallet.dat safe, secure, and up to date on the /USBSTICK USB Stick.