July 8, 2016

Scheduling daily backup job using crontab

1. cd /etc/cron.daily
2. Create a symbolic link to the job
ln -s /home/auser/mysqlbackups/db_to_s3_backup.sh db_backup

This will make it run at 4.02am daily per crontab definition

> cat /etc/crontab
...
.....
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
~
...


Crontab format. Ref: https://www.pantz.org/software/cron/croninfo.html
# Minute   Hour   Day of Month       Month          Day of Week        Command    
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)                
    0        2          12             *                *            /usr/bin/find

Centos - running commands at system initialization

Did this to restart DB and Web server during system boot.

1. Create a initialization script at /root/instance_init.sh

Commands are chained using && to make sure it executes in sequence

#!/bin/bash
echo "Step: staring mysql" &&
service mysqld start &&
echo "Step: restoring database" &&
runuser -l auser -c '/home/auser/mysqlrestore/s3_to_db_restore.sh' &&
echo "Step: starting jboss" &&
runuser -l auser -c '/usr/local/jboss-6.1.0.Final/bin/startjboss.sh'

2. Open > vi /etc/rc.d/rc.local
append a line:
sh /root/instance_init.sh


Backup and Restore MySql backup to/from AWS S3

Steps followed from a Centos 6 machine:

1. yum install s3cmd
2. Created a configuration file .s3cfg under user's home directory.
Defined access and secret keys in the file.
access_key=<access key>
secret_key=<secret key>

3. Created a backup script file:
#!/bin/sh
mysqlpass="your password"
db_schema_name="your db schema"
s3bucket="s3://your bucket name"
user_home="/home/auser"
script_home="/home/auser/mysqlbackups"
backup_file="some.backup.sql.gz"

mysqldump -u root -p$mysqlpass $db_schema_name > $script_home/temp.sql
gzip $script_home/temp.sql
rm -rf $script_home/$backup_file
mv $script_home/temp.sql.gz $script_home/$backup_file
/usr/bin/s3cmd -c $user_home/.s3cfg put $script_home/$backup_file $s3bucket/$backup_file

4. Created a restore script file:
#!/bin/sh
mysqlpass="your password"
db_schema_name="your db schema"
s3bucket="s3://your bucket name"
user_home="/home/auser"
script_home="/home/auser/mysqlbackups"
backup_file="some.backup.sql"

cd $script_home
/usr/bin/s3cmd -c $user_home/.s3cfg get $s3bucket/$backup_file.gz
gzip -d $backup_file.gz
mysql -u root -p$mysqlpass $db_schema_name < $backup_file
rm -rf $backup_file

Killing JBoss process

ps -ef | grep java | awk 'NR==1{print $2}' | xargs kill -9

NR==1 gets the first process.

JBoss 6 root context

Suppose you have an application (say named example) deployed in JBoss 6.
By default your application will be accessed by <Your URL>/example.

Suppose you want to make the application available at <Your URL> i.e the root location.
Then follow the following 2 steps:

1. Change the context of the ROOT.war application

To do that, create a file named jboss-web.xml  under <base folder>/jboss-6.1.0.Final/server/default/deploy/ROOT.war/WEB-INF.

<?xml version="1.0"?>

<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 5.0//EN"
"http://www.jboss.org/j2ee/dtd/jboss-web_5_0.dtd">

<jboss-web>
<context-root>root</context-root>
</jboss-web>

2. Add a jboss-web.xml  under WEB-INF of your application
<?xml version="1.0"?>

<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 5.0//EN"
"http://www.jboss.org/j2ee/dtd/jboss-web_5_0.dtd">

<jboss-web>
<context-root>/</context-root>
</jboss-web>

Now running <Your URL> in a browser will point to your example application.

July 6, 2016

AWS instance - how to find meta data of an EC2 instance

Login to the instance and:

> curl http://169.254.169.254/latest/meta-data/
ami-id
ami-launch-index
ami-manifest-path
block-device-mapping/
hostname
instance-action
instance-id
instance-type
kernel-id
local-hostname
local-ipv4
mac
metrics/
network/
placement/
profile
public-hostname
public-ipv4
public-keys/
ramdisk-id
reservation-id
security-groups
services/

Example:
> curl http://169.254.169.254/latest/meta-data/ami-id