katratxo on Software Development

tail -f /var/log/brain | grep -i software

Archive for February 2010

Making an snapshot of your Openbravo instance

with 22 comments

I have created a small ant script that automates the process of making a ‘snapshot’ of your instance. By snapshot I mean a database dump and the whole Openbravo folder. This archive file (zip format) does not exclude any file, so .class files, Mercurial metadata, etc is also included in the file, so is a ‘real snapshot’

When is this script useful?

This tool is useful when:

  • You are a newbie developing with Openbravo framework, you have an instance up and running, and you want to start testing something. Making an snapshot of your current state, gives you a easy way to step back if something goes wrong.
  • You just want to archive the current state of your instance.
  • You want to make a copy of your current instance. You can make a snapshot and restore in another folder. A couple of tweaks in the Openbravo.properties and voila! you have a copy of your instance.
  • You want to move your running instance to a another environment (e.g for testing).
  • Etc.

How to use it?

Note: The following commands are for GNU/Linux users. If you use other operating system, the script still works but the unzip process is different, e.g. I have tested on Windows using the built in extracting tool for zip files.

You can grab a copy of the snapshot.xml and place it inside your Openbravo sources folder, eg.

~/src/openbravo/pi $ wget http://bitbucket.org/iperdomo/labs/raw/8db9dcb650d3/openbravo/snapshot.xml

Making a snapshot

Once you have the snapshot.xml in the sources folder just run it (the default target is snapshot).

~/src/openbravo/pi $ ant -f snapshot.xml

Depending on your machine’s resources the script takes about 7 minutes. Is slower on Windows machines (and more slow if you have an antivirus checking every file that you touch). Here you have an example of the output.

     [echo] Creating instance snapshot...
     [echo] Making temp folder...
     [echo] basedir /home/iperdomo/src/openbravo/pi
     [echo] Creating a database backup...
     [exec] pg_dump: reading schemas
     [exec] pg_dump: reading user-defined functions
     [exec] pg_dump: reading user-defined types
     [exec] pg_dump: reading procedural languages
     [exec] pg_dump: reading user-defined aggregate functions
     [exec] pg_dump: reading user-defined operators
     [exec] pg_dump: reading type casts
     [exec] pg_dump: finding inheritance relationships
     [exec] pg_dump: reading column info for interesting tables

... some more pg_dump logging ...

     [exec] pg_dump: dumping contents of table s_resourceunavailable
     [exec] pg_dump: dumping contents of table s_timeexpense
     [exec] pg_dump: dumping contents of table s_timeexpenseline
     [exec] pg_dump: dumping contents of table s_timetype
     [echo] Making a zip...
      [zip] Building zip: /home/iperdomo/src/openbravo/openbravo-2010-02-19_13-18-45.zip

Total time: 7 minutes 33 seconds

The script will call pg_dump with all the necessary parameters read from Openbravo.properties, and creates a .backup in the temp folder inside the sources. Then it archives the whole content in a zip file in the parent folder. The name of the zip is the name of the context plus a timestamp.

Restoring a snapshot

To restore an snapshot is as simple as removing the current folder and extracting the contents of the zip file. Then run the restore target from the script.

~/src/openbravo $ rm -rf pi

~/src/openbravo $ unzip /home/iperdomo/src/openbravo/openbravo-2010-02-19_13-18-45.zip -d pi

Example output of unzip

Archive:  /home/iperdomo/src/openbravo/openbravo-2010-02-19_13-18-45.zip
   creating: pi/.hg/
   creating: pi/.hg/store/
   creating: pi/.hg/store/data/
   creating: pi/.hg/store/data/.settings/
   creating: pi/.hg/store/data/_web_content/
   creating: pi/.hg/store/data/_web_content/_m_e_t_a-_i_n_f/
   creating: pi/.hg/store/data/_web_content/_w_e_b-_i_n_f/
   creating: pi/.hg/store/data/config/
   creating: pi/.hg/store/data/config/eclipse/
   creating: pi/.hg/store/data/database/model/sequences/

... some more log ...

  inflating: pi/web/skins/Default/Popup/_ParticularItems/Workflow/iconTask.png  
  inflating: pi/web/skins/Default/Popup/_ParticularItems/Workflow/iconWorkflow.png  
  inflating: pi/web/skins/Default/RTLFlippedImages.txt

After extracting the contents of the zip file, change the directory to the Openbravo sources and execute the restore target, e.g.

~/src/openbravo $ cd pi

~/src/openbravo/pi $ ant -f snapshot.xml restore

This will drop the database and use pg_restore to restore the .backup file stored in the temp folder. Here you have some example of the log output.

Buildfile: snapshot.xml

     [echo] Checking if openbravo.backup file exists in /home/iperdomo/src/openbravo/pi/temp folder...

     [echo] Deleting the database ...
      [sql] Executing commands
      [sql] 1 of 1 SQL statements executed successfully
     [echo] Creating the database...
      [sql] Executing commands
      [sql] 1 of 1 SQL statements executed successfully
     [echo] Restoring the backup file...
     [exec] pg_restore: connecting to database for restore
     [exec] pg_restore: creating SCHEMA public
     [exec] pg_restore: creating COMMENT SCHEMA public
     [exec] pg_restore: creating PROCEDURAL LANGUAGE plpgsql
     [exec] pg_restore: creating FUNCTION a_amortization_process(character varying)

... some other pg_restore log ...

     [exec] pg_restore: setting owner and privileges for FK CONSTRAINT s_timeexpenseline_s_timeexpens
     [exec] pg_restore: setting owner and privileges for FK CONSTRAINT s_timeexpenseline_s_timetype
     [exec] pg_restore: setting owner and privileges for FK CONSTRAINT s_timetype_ad_client
     [exec] pg_restore: setting owner and privileges for FK CONSTRAINT s_timetype_ad_org

Total time: 36 seconds

That’s it, you have restored your snapshot.

How to use the script to copy an instance?

You follow the same procedure, to create the snapshot. Then you unzip the archive and another folder.

~/src/openbravo $ unzip openbravo-2010-02-19_13-18-45.zip -d pi2

After the extraction of the archive contents, you modify the Openbravo.properties and change the following properties:

# New name of the context

# Should point to the new path

# New name of the database

After changing the Openbravo.properties file, execute the restore process as usual.


Using the script with non default settings

The script assumes that you are working in a development environment with the ‘standard’ configuration of PostgreSQL. If your database is not in localhost or not using the default port 5432, you can override those properties when making the backup and restoring it.

~/src/openbravo/pi $ ant -f snapshot.xml -Dhost=otherHost -Dport=otherport

Why is not this part of Openbravo’s standard distribution?

As you can see the script uses pg_dump and pg_restore for dumping and restoring the database, so only PostgreSQL is supported. If you are willing you can send me a patch to include support for Oracle

That’s all, I hope this script will save you some time


Written by katratxo

February 22, 2010 at 12:25 pm