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