Syncencrypt windows construction

From GnuCash
Jump to: navigation, search

Script for windows for gnucash encryption and synchronization

This script is fully compatible with syncencrypt_linux_construction.

A: Prerequisites

  1. Have gnucash installed
  2. Have cygwin installed. Required packages:
    • gnupg
    • ssh
    • tar
  3. Make sure cygwin's "bin"-folder is in the path (typically c:\cygwin\bin). In CMD.EXE you should be able to type "bash [Enter]" and the cygwin prompt opens.
  4. Have an ftp/sftp server with write access.
  5. Create a local directory where you will put everything. (eg "C:\Users\username\Documents\GnuCash")

B: gnucash_syncencrypt.cmd file

  • Create a new file in your working directory (see prerequisites) named "gnucash_syncencrypt.cmd" and paste the following text into it and save it.
  • Replace the first line with the path to your working directory.
  • Note: Most of the commands are 1:1 copy from the gnucash-bin.cmd file.
cd D:\Users\username\Documents\GnuCash

bash gnucash_syncencrypt_pre.sh

setlocal
chcp 1252
set PATH=C:\Program Files (x86)\gnucash\bin;C:\Program Files (x86)\gnucash\lib;C:\Program Files (x86)\gnucash\lib\gnucash;%PATH%
set GUILE_WARN_DEPRECATED=no
set GNC_MODULE_PATH=C:\Program Files (x86)\gnucash\lib\gnucash
set GUILE_LOAD_PATH=C:\Program Files (x86)\gnucash\share\gnucash\guile-modules;C:\Program Files (x86)\gnucash\share\gnucash\scm;C:\Program Files (x86)\gnucash\share\guile\1.6;%GUILE_LOAD_PATH%
set LTDL_LIBRARY_PATH=C:\Program Files (x86)\gnucash\lib
set QOF_LIB_DIR=C:\Program Files (x86)\gnucash\lib\gnucash
set SCHEME_LIBRARY_PATH=
start /WAIT gnucash-bin gnucash

bash gnucash_syncencrypt_post.sh

C: Bash script gnucash_syncencrypt_pre.sh

  • Create a new file in your working directory (see prerequisites) named "gnucash_syncencrypt_pre.sh" and paste the following text into it and save it.
  • See notes in the file for adjustments. These are working directory, Book name, and the sftp commands.
  • Note: This is a modified script based on [1] by Jose Antonio Martin and Johannes Buchner.
#!/bin/bash
# gpgarmor - based on code by Jose Antonio Martin
# rewritten by Johannes Buchner
# 
# This shell script will wrap around any program and protect the data files
# by encrypting it using tar and gpg.
# You can put a link to this script on your desktop or in the menu.
# 
# Adjusted for cygwin on windows
# Adjust the following options:

# executing directory (where your data file lives)
DIR="/cygdrive/d/Users/username/Documents/GnuCash"
# File which is parameter for the program (PROG). 
# The encrypted file will be called the $BOOK.tar.gz.asc
BOOK="gnucash"
# All files that should be protected (archived and encrypted).
FILES="${BOOK} ${BOOK}.*.gnucash ${BOOK}.*.log"



INTERFACE_ERR='echo' # INTERFACE_PASS will be read from shell if this is set

function do_error {
    $INTERFACE_ERR "$@"
    cd - &> /dev/null
    exit 1
}


function get_file {
    
    rm $BOOK.tar.gz.asc.bak
    mv $BOOK.tar.gz.asc $BOOK.tar.gz.asc.bak

# see http://cygwin.com/ml/cygwin/2003-12/msg00059.html
    cat <<++EOT++ > sftp.tmp
cd gnucash
get $BOOK.tar.gz.asc
bye
++EOT++

    sftp -b sftp.tmp -i identityfile user@host

    rm sftp.tmp
    
}



cd "$DIR"

get_file

test -f "$BOOK.tar.gz.asc" || \
    if test -f "$BOOK"; then
        echo "Found not-encrypted file (first-run)"
        run_and_encrypt || 
            do_error "file \"$BOOK\" not found"
    fi

# Found encrypted file
ppg --quiet --decrypt --output "$BOOK".tar.gz "$BOOK".tar.gz.asc || 
    do_error "gpg decryption failed"



# untar
tar -zxkf "$BOOK.tar.gz"  &> /dev/null
shred $BOOK.tar.gz "$BOOK.tar.gz.asc"
rm -f $BOOK.tar.gz "$BOOK.tar.gz.asc"
exit 0

D: Bash script gnucash_syncencrypt_post.sh

  • Create a new file in your working directory (see prerequisites) named "gnucash_syncencrypt_post.sh" and paste the following text into it and save it.
  • See notes in the file for adjustments. These are working directory, Book name, and the sftp commands.
  • Note: This is a modified script based on [2] by Jose Antonio Martin and Johannes Buchner.
#!/bin/bash
# gpgarmor - based on code by Jose Antonio Martin
# rewritten by Johannes Buchner
# 
# This shell script will wrap around any program and protect the data files
# by encrypting it using tar and gpg.
# You can put a link to this script on your desktop or in the menu.
# 
# Adjusted for cygwin on windows
# Adjust the following options:

# executing directory (where your data file lives)
DIR="/cygdrive/d/Users/username/Documents/GnuCash"
# File which is parameter for the program (PROG). 
# The encrypted file will be called the $BOOK.tar.gz.asc
BOOK="gnucash"
# All files that should be protected (archived and encrypted).
FILES="${BOOK} ${BOOK}.*.gnucash ${BOOK}.*.log"



INTERFACE_ERR='echo' # INTERFACE_PASS will be read from shell if this is set

function do_error {
    $INTERFACE_ERR "$@"
    cd - &> /dev/null
    exit 1
}

function encrypt_and_upload {

    tar -czf "$BOOK.tar.gz" $FILES || 
        do_error "tar failed on $BOOK"

    gpg --quiet -ca --output "$BOOK".tar.gz.asc "$BOOK".tar.gz || 
        do_error "gpg encryption failed"
	
    shred $FILES $BOOK.tar.gz  > /dev/null
    rm -f $FILES $BOOK.tar.gz 
    put_file
    cd - &> /dev/null
    exit 0
}



function put_file {
    # see http://cygwin.com/ml/cygwin/2003-12/msg00059.html
    cat <<++EOT++ > sftp.tmp
cd gnucash
put $BOOK.tar.gz.asc
bye
++EOT++

    sftp -b sftp.tmp -i identityfile user@host
    rm sftp.tmp  
}

encrypt_and_upload

E: Putting all in place

Try the script:

  • Open a windows command (cmd.exe)
  • Navigate to the folder you specified before
  • Make sure the gnucash file (variable "BOOK") is present (Note: If there are no *.gnucash or *.log files the tar command will fail. So modify/save the gnucash file before to obtain these)
  • Enter "gnucash_syncencrypt.cmd"


Creating a shortcut

  • Right click on your desktop and choose New -> Shortcut
  • Find c:\windows\system32\cmd.exe and press Next
  • Give a name for your shortcut and press Finish
  • Right-click on newly created shortcut and choose "Properties"
  • In Target change to >> C:\WINDOWS\system32\cmd.exe /C "pathto.cmdfile" << where you have to put the whole path to the cmd file we created above.
  • In "Start in:" specify the working directory you chose above

Thats it. By running the shortcut the encrypted gnucash files will be fetched from sftp, locally decrypted and opened with gnucash. Once Gnucash was closed they are locally encrypted, uploaded to sftp and the unencrypted local copies are overwritten.