Nov 022011
 

Article di Jimmy, pubblicato sul suo blog e su BGLUG, ovvero il LUG di Bergamo.

Gnucash è un’ottimo strumento per tenere un bilancio familiare, e dalla versione 2.4.0 supporta il collegamento ad un database SQLite, MySQL o PostgreSQL per l’archiviazione dei dati. Usando un database locale sono tuttavia legato ad un computer specifico per il suo utilizzo. Io voglio invece poter aprire una sessione di Gnucash da qualsiasi computer e collegarmi sempre ad un’unica base dati remota.

Aprire il database alla rete è decisamente poco sicuro, ma un tunnel SSH mi offre una alternativa leggera e affidabile.




Per implementare il tutto ho usato:

Lato server:

Lato client:

Tutti questi software girano indifferentemente su piattaforme  Linux, Windows e Mac, il che offre un’ampia flessibilità. Per me che ho a casa un server Linux collegato alla rete il tutto è stato molto semplice, ma non escludo che anche un hosting non troppo costoso offra un supporto sufficiente per implementare il tutto.

Server

La configurazione del server SSH non richiede niente di particolare, se non assicurarsi che il firewall sia correttamente configurato per permettere l’accesso alla porta 22, o qualunque altra voi abbiate impostato. Se volete una sicurezza maggiore consiglio di usare un’autenticazione tramite chiavi, come descritto qui.

Il prossimo passo è creare il database e l’utente che dovrà utilizzarlo. Dato che creeremo un tunnel SSH è anche possibile usare phpMyAdmin da remoto come spiegato ad esempio qui, ma la procedura è così semplice che io decido di farlo da terminale. Dopo aver ottenuto l’accesso al server la sequenza è la seguente:

# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4012
Server version: 5.1.49-3 (Debian)
 
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> create database gnucash;
Query OK, 1 row affected (0.00 sec)
 
mysql> grant all on gnucash.* to gnucash@localhost identified by 'PASSWORD' with grant option;
Query OK, 0 rows affected (0.12 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.13 sec)
 
mysql> exit
Bye
#

Il che significa che, dopo aver ottenuto l’accesso come utente root a mysql creo il database gnucash e con il comando GRANT creo direttamente l’utente gnucash, gli assegno i permessi per l’uso del database e imposto la PASSWORD di accesso. Nella documentazione di Gnucash non ho trovato nulla riguardo i permessi necessari all’utente per l’utilizzo del programma, quindi li assegno tutti, anche se probabilmente è possibile limitarli e migliorare la sicurezza dell’accesso.

Client

Prima di avviare Gnucash su un qualsiasi client è necessario creare il tunnel SSH, che si esegue abbastanza semplicemente su qualsiasi piattaforma.

Da un terminale si digita:

# ssh -L 3307:localhost:3306 -i /media/USB/private.key user@mysite.com

OpenSSH Client (Linux e Mac)

l’opzione -i specifica la posizione della chiave privata per l’autenticazione, se necessaria, che io tengo nella chiavetta USB, mentre con l’opzione -L creo il tunnel fino al mio server che si trova in miosito.com, specificando tre parametri:

3307 è la porta locale a cui mi dovrò collegare per entrare nel tunnel

localhost è l’indirizzo dove è disponibile il server MySQL. Dato che il mio tunnel “esce” sulla stessa macchina l’indirizzo è localhost.

3306 è la porta alla quale collegarmi per accedere al server MySQL

Putty (Windows)

Nella finestra di configurazione di Putty inserisco i seguenti dati:

Session: inserisco l’host name, miosito.com, e la porta del server SSH, 22 o altro.

Connection->Data: in Login details posso inserire il nome utente per il collegamento SSH, per evitare che mi venga chiesto ogni volta.

Connection->SSH->Auth: specifico la posizione del file contenente la private key per l’autenticazione, se necessaria.

Connection->SSH->Tunnels: in source port inserisco la porta locale di accesso, e in destination inserisco l’indirizzo del server MySQL e la porta di destinazione con la sintassi localhost:3306

Prima di aprire la connessione ho la possibilità, ritornando alla finestra Session, di salvare i parametri della sessione per poterli richiamare senza reinserirli nuovamente. Clicco su Open e dopo aver inserito la password il prompt del terminale mi indicherà che il tunnel è attivo.

GnuCash

Ora posso lanciare Gnucash e aprire un nuovo file indicando come formato dei dati mysql, come host la macchina da cui abbiamo aperto il tunnel e la porta di accesso, nel nostro caso 127.0.0.1:3307, il nome del database, gnucash, il nome dell’utente MySQL, gnucash, e la PASSWORD di accesso. Il programma crea in automatico tutte le tabelle necessarie a seconda della struttura dei conti da noi indicata.

Come nome di host ho indicato l’indirizzo IP perchè su macchine Linux, per motivi che ignoro, se indico localhost:3307 mi viene restituito un errore e il collegamento abortisce. Su macchine Windows funzionano entrambe le sintassi.

Sono pronto ora per inserire i dati. Ogni volta che vorrò modificarli da qualsiasi posto mi trovi sarà sufficiente aprire il tunnel e da Gnucash aprire il file esistente inserendo gli stessi parametri sopra indicati.

Se utilizzate una chiave privata è sconsigliabile sparpagliarla su tutte le macchine dove lavorate, mentre è pratico e abbastanza sicuro tenerla su una chiavetta USB dove può trovare posto anche Putty, che non necessita di installazione e occupa pochissimo spazio.

Auguro un buon pareggio di bilancio a tutti 🙂

Popular Posts:

Flattr this!

  19 Responses to “Bilancio online con Gnucash + MySQL + SSH”

  1. Best compliments for your translation, and many thanks for sharing 🙂

  2. Hi,
    good tutorial… for the localhost problem on linux, is likely to be related to IPv6 for localhost which is translated as ::1, if you edit the file resolve.conf reversing the order of localhost IPv6 and IPv4 should works.

    • Good idea, thanks for the feedback Sebastian

    • The error is because gnucash attempts to use a unix domain socket to connect to mysqld when the string “localhost” is used for hostname. Change the string “hostname” to “127.0.0.1” and it will use tcp/ip.

  3. Nice article.

    1) Will this setup also work if you replace mysql with postgresql?
    2) How to move ahead if you already got a gnucash traditional file and then want to move to a relational database storage? Just using “save as” will do the trick?

  4. Not sure why would I need to keep my budget on-line… 😕

    • Perhaps on cloud would be better as title 😛 ?
      Basically to access them from different computer, if you don’t have this need, just keep them without DB as flat file.

    • “mysql” does not imply “on-line.” It is easily possible to run mysqld on localhost. Why would one do this? Because mysql provides excellent ACID guarantees.

  5. How to have an accountant perform accounting/auditing duties in this case?

  6. Perfect guide thankyou very much! I thought there must be a way to tunnel into mysql via ssh just needed a nice guide 🙂

    I’m going to pop a small guide here as well, I installed MySQL and GNUCash on Debian 6.0.3 and it didn’t allow me to choose MySQL at all for new setups, so here is a guide to install with support for MySQL:

    wget http://downloads.sourceforge.net/sourceforge/gnucash/gnucash-2.4.9.tar.gz
    tar xvfz gnucash-2.4.9.tar.gz
    sudo aptitude build-dep gnucash
    sudo aptitude install texinfo subversion intltool libdbi0-dev libdbd-{sqlite3,pgsql,mysql} guile-1.8 guile-1.8-dev doxygen libdbi0-dev libdbd-{sqlite3,pgsql,mysql}
    cd /usr/share/guile/1.8
    sudo ln -s ../../slib slib
    sudo guile -c “(use-modules (ice-9 slib)) (require ‘new-catalog)”
    cd ~/gnucash-2.4.0/
    ./configure –prefix=$HOME/stable/gnucash –enable-debug –enable-doxygen –enable-error-on-warning –enable-compile-warnings –enable-dbi
    make all install

    and finally, run it

    ~/stable/gnucash/bin/gnucash

  7. How to convert an existing file based storage gnucash account to a (postgresql) based storage gnucash acount?
    Otherwise, very handy information!

  8. Thank for this guide.

    Im using ubuntu on server side and MAC OSx on client side.

    I’ve followed the steps but my client (mac) side gnucash only has xml and sqlite3 database options.

    Could there be some other dependancy missing on the OSX side?

    thank you
    daz

  9. I seem to be having some issues getting the GNUcash program to access the data on the remote server. The error mysql://’Username’@127.0.0.1:3307/database name experienced an error or encountered bad or corrupt data. The Ssh connection works and I can access the database from the server. Any ideas?

  10. script to open gnucash automatically from a ssh tunnel
    copy the original /usr/bin/gnucash to /usr/bin/gnucashbin
    and then create a new /usr/bin/gnucash with the source bellow and give it permission to execute.
    voa lá!

    —-

    #!/bin/bash
    CTRL_SOCKET=$HOME/.gnucash_ssh_tunnel
    SERVER=”postgresserveronsshserver:5432″
    LISTEN_PORT=5432
    SSH_SERVER=”[email protected]
    SSH_PORT=22

    ssh -M -S $CTRL_SOCKET -fnNT -L $LISTEN_PORT:$SERVER $SSH_SERVER -p $SSH_PORT
    /usr/bin/gnucashbin
    ssh -S $CTRL_SOCKET -O exit $SSH_SERVER -p $SSH_PORT

  11. ow, I forgot..
    to work properly, you need create a ssh key with “ssh-keygen” without a passphrase and copy the id file to the server with “ssh-copy-id [email protected]

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*