Monday, May 19, 2014

Automated database backups

Automated Database Backups

Postgres

Assuming we are using Linux, create a file called .pgpass in the user’s home directory with the following details:
hostname:port:database:username:password

Then from the command-line run the following command:
pg_dump –U user dbname | gzip > ~/backup/db/$(date +%Y-%m-%d).psql.gz

Mysql

Assuming we are using Linux, create a file called ~/.my.cnf in the user’s home directory with the following details:
[mysqldump]
user=mysqluser
password=secret

Then from the command-line
mysqldump –u user dbname | gzip > ~/backup/db/$(date +%Y-%m-%d).psql.gz

Crontab

If the command is successful, then create and add this command to a script, for example runBackup.sh
Then schedule the job to run by editing your crontab:
crontab -e
For example, to run once a week on Saturday at 2AM:
0 2 * * 6 ~/runBackup.sh

 

References




Wednesday, May 7, 2014

Automated tomcat start up on server reboot



Automated start up on server reboot


Create a new tomcat user using the following command:
adduser tomcat

Create the following file /etc/init.d/tomcat:
#!/bin/bash
#
# tomcat       
#
# chkconfig:
# description: Start up the Tomcat servlet engine.

# Source function library.
. /etc/init.d/functions


RETVAL=$?
CATALINA_HOME="/usr/local/apache-tomcat-7.0.52"

case "$1" in
 start)
        if [ -f $CATALINA_HOME/bin/startup.sh ];
          then
            echo $"Starting Tomcat"
            /bin/su tomcat $CATALINA_HOME/bin/startup.sh
        fi
        ;;
 stop)
        if [ -f $CATALINA_HOME/bin/shutdown.sh ];
          then
            echo $"Stopping Tomcat"
            /bin/su tomcat $CATALINA_HOME/bin/shutdown.sh
        fi
        ;;
 *)
        echo $"Usage: $0 {start|stop}"
        exit 1
        ;;
esac

exit $RETVAL

Edit the file and change CATALINE_HOME to the location of where tomcat is found.

Grant execution write of the new file using the following command:
chmod 755 /etc/init.d/tomcat

Reassign the owner of the tomcat folder and all subfolders to the tomcat user:
chown tomcat:tomcat –R /usr/local/apache-tomcat-7.0.52


To start up on server reboot:
ln –s ../init.d/tomcat S71tomcat
 

Sunday, January 5, 2014

Spring STS doesn't start up after many months of use

The problem:


After many months of using Spring STS IDE, I tried to reload the program but it always fails without any notifications or error messages. Even the "-clean" option failed to work.

When I insepected the log file found in the workspace/.metadata/log I noticed the following error:



 !ENTRY org.eclipse.core.jobs 2 2 2014-01-06 07:44:26.601  
 !MESSAGE Job found still running after platform shutdown. Jobs should be canceled by the plugin that scheduled them during shutdown: org.eclipse.core.internal.refresh.RefreshJob  
 !ENTRY org.eclipse.core.jobs 2 2 2014-01-06 07:44:26.601  
 !MESSAGE Job found still running after platform shutdown. Jobs should be canceled by the plugin that scheduled them during shutdown: org.eclipse.core.internal.refresh.RefreshJob  
 !ENTRY org.eclipse.ui 4 0 2014-01-06 07:44:26.602  
 !MESSAGE Error occurred during status handling  
 !STACK 0  
 java.lang.NullPointerException  
      at org.eclipse.ui.internal.statushandlers.StatusHandlerRegistry.<init>(StatusHandlerRegistry.java:61)  
      at org.eclipse.ui.internal.statushandlers.StatusHandlerRegistry.getDefault(StatusHandlerRegistry.java:91)  
      at org.eclipse.ui.statushandlers.StatusManager.getStatusHandler(StatusManager.java:135)  
      at org.eclipse.ui.statushandlers.StatusManager.handle(StatusManager.java:189)  
      at org.eclipse.ui.statushandlers.StatusManager.handle(StatusManager.java:231)  
      at org.eclipse.ui.statushandlers.StatusManager$StatusManagerLogListener.logging(StatusManager.java:305)  
      at org.eclipse.core.internal.runtime.RuntimeLog.logToListeners(RuntimeLog.java:160)  
      at org.eclipse.core.internal.runtime.PlatformLogWriter.logged(PlatformLogWriter.java:100)  
      at org.eclipse.equinox.log.internal.ExtendedLogReaderServiceFactory.safeLogged(ExtendedLogReaderServiceFactory.java:86)  
      at org.eclipse.equinox.log.internal.ExtendedLogReaderServiceFactory.logPrivileged(ExtendedLogReaderServiceFactory.java:205)  
      at org.eclipse.equinox.log.internal.ExtendedLogReaderServiceFactory.log(ExtendedLogReaderServiceFactory.java:178)  
      at org.eclipse.equinox.log.internal.ExtendedLogServiceFactory.log(ExtendedLogServiceFactory.java:65)  
      at org.eclipse.equinox.log.internal.ExtendedLogServiceImpl.log(ExtendedLogServiceImpl.java:87)  
      at org.eclipse.equinox.log.internal.LoggerImpl.log(LoggerImpl.java:54)  
      at org.eclipse.core.internal.runtime.PlatformLogWriter.logging(PlatformLogWriter.java:43)  
      at org.eclipse.core.internal.runtime.RuntimeLog.log(RuntimeLog.java:96)  
      at org.eclipse.core.internal.jobs.JobManager.doShutdown(JobManager.java:606)  
      at org.eclipse.core.internal.jobs.JobManager.shutdown(JobManager.java:252)  
      at org.eclipse.core.internal.jobs.JobActivator.stop(JobActivator.java:55)  
      at org.eclipse.osgi.framework.internal.core.BundleContextImpl$2.run(BundleContextImpl.java:771)  
      at java.security.AccessController.doPrivileged(Native Method)  
      at org.eclipse.osgi.framework.internal.core.BundleContextImpl.stop(BundleContextImpl.java:764)  
      at org.eclipse.osgi.framework.internal.core.BundleHost.stopWorker(BundleHost.java:510)  
      at org.eclipse.osgi.framework.internal.core.AbstractBundle.suspend(AbstractBundle.java:566)  
      at org.eclipse.osgi.framework.internal.core.Framework.suspendBundle(Framework.java:1206)  
      at org.eclipse.osgi.framework.internal.core.StartLevelManager.decFWSL(StartLevelManager.java:592)  
      at org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStartLevel(StartLevelManager.java:257)  
      at org.eclipse.osgi.framework.internal.core.StartLevelManager.shutdown(StartLevelManager.java:215)  
      at org.eclipse.osgi.framework.internal.core.InternalSystemBundle.suspend(InternalSystemBundle.java:284)  
      at org.eclipse.osgi.framework.internal.core.Framework.shutdown(Framework.java:692)  
      at org.eclipse.osgi.framework.internal.core.Framework.close(Framework.java:600)  
      at org.eclipse.core.runtime.adaptor.EclipseStarter.shutdown(EclipseStarter.java:400)  
      at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:200)  
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)  
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
      at java.lang.reflect.Method.invoke(Method.java:601)  
      at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:636)  
      at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591)  
      at org.eclipse.equinox.launcher.Main.run(Main.java:1450)  
      at org.eclipse.equinox.launcher.Main.main(Main.java:1426)  

Solution

It appears that it may have gotten itself into a bad state. To reset this persisted state we can use the "-cleanPersistedState" option, when launching the application.


Reference: http://stackoverflow.com/questions/15372023/eclipse-will-not-start-and-i-havent-changed-anything

Wednesday, November 27, 2013

How to revoke access to tables prior to postgres 9.0

SELECT 'REVOKE SELECT ON TABLE ' || relname || ' FROM some_user;' FROM pg_class JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace WHERE nspname = 'public';

Thursday, September 5, 2013

Postgres grant read only

Prior to postgres 9.0 you can use this:



 SELECT 'GRANT SELECT ON ' || relname || ' TO xxx;'  a
 FROM pg_class JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace  
 WHERE nspname = 'public' AND relkind IN ('r', 'v')  

Postgres 9+ you can use this:



 GRANT SELECT ON ALL TABLES IN SCHEMA public TO xxx;  

Reference:


http://stackoverflow.com/questions/760210/how-do-you-create-a-read-only-user-in-postgresql

Thursday, August 15, 2013

Blocked loading mixed active content

Problem

With the latest versions of firefox, a javascript file fail to load from an external site such as google using the following code:


 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.js"></script>  

The connection was secure running on HTTPS. However the connection to load the javascript file was not secure running purely on HTTP.

Solution


To get the javascript file to load properly I simply changed the SRC URL to use HTTPS as shown below:

 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.js"></script>  

Monday, August 5, 2013

ClassFormatException Invalid byte tag in constant pool: 60 with Grails in Tomcat 7.0.42

The Problem

I encountered this error when deploying my grails application to the latest version of Tomcat 7.0.42.

The stacktrace is shown below:


 SEVERE: Unable to process Jar entry [com/ibm/icu/impl/data/LocaleElements_zh__PINYIN.class] from Jar [jar:file:/usr/local/apache-tomcat-7.0.42/webapps/mutations/WEB-INF/lib/icu4j-2.6.1.jar!/] for annohas been forcibly unregistered.  
 tations  
 org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 60  
     at org.apache.tomcat.util.bcel.classfile.Constant.readConstant(Constant.java:133)  
     at org.apache.tomcat.util.bcel.classfile.ConstantPool.<init>(ConstantPool.java:60)  

The offending jar file was the
icu4j-2.6.1.jar

The Solution

I updated the BuildConfig.groovy file to remove any dependencies to the offending JAR file and updated the dependency to use the latest version of the icu4j which at the time was version 51.1. To do that I inserted this code:


   inherits("global") {  
           excludes "icu4j"  
   }  
   dependencies {  
           runtime 'com.ibm.icu:icu4j:51.1'  
   }  

Environment

Grails version: 2.2.4
Tomcat version: 7.0.42