Syncencrypt windows construction

From GnuCash
Revision as of 08:53, 11 August 2011 by Marekschmidt (talk | contribs) (change gpg -> gnupg)
Jump to: navigation, search

Script for windows for gnucash encryption and synchronization

A: Prerequisites

  1. Have gnucash installed
  2. Have cygwin installed. Required packages:
    • gnupg
    • ssh
    • tar
  3. Have an ftp/sftp server with write access.

B: gnucash_syncencrypt.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

#!/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

#!/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