Sunday, April 20, 2008

clear session files - Ruby

Session is important for web application as track user. Session grow likely same as log files. This will cause memory overhead. We can clear session using Linux crontab command.

This will remove 300 minutes old session files by every day 00:10 hrs.

Place this command in the crontab file in the /usr/bin/ directroy.

Can edit the crontab file using following command.

crontab -e

* * * * * find absolute_path -name 'sess.*' -amin +300 -exec rm -rf {} \;
Explanation:
10 0 * * * find /home/tmp -name 'ruby_sess.*' -amin +300 -exec rm -rf {} \;

find - find the file that start with the name sess.

min +300
- find the session file with 300 minutes old

exec rm -rf - execute the command remove the files which is 300 minutes old.

Wednesday, April 16, 2008

Rotate log files

Logging is come into picture when something went wrong in application and have to point out the root cause of the problem.

Some times application running out of disk space. This is not because of our code but because of log files generated by apps.

Logrotate is effective utility for manage files which grow rapidliy like catalina.out and etc....

To rotate ghe old log files we can use log4j in java ,log4r in Ruby. There is another cool way to Rotate the log in unix based servers.

There is a configuration file called logrotate.conf in /etc directory. Add the following code

# Rotate MyApps logs
1) /home/MyApps/log/*.log {
2)daily
3) missingok
4)rotate 5
5)compress
6)delaycompress
7)notifempty
8)copytruncate
9)create 0666 devgroup devgroup
}

This will take care of log file to rotate.

Explanation:
1) /home/MyApps/log/*.log {
Absolute path to log file of project
2)daily
rotate log files daily
3) missingok
If log file is missing dont raise any error.
4)rotate 5
Log files rotated <5> times
for Example the directory /log look like:
production.log
production.log.1
produciton.log.2.gz
produciton.log.3.gz
production.log.4.gz
5)compress
Old rotated file compressed to by default gzip
6)delaycompress
postpone compression of previous log file to next rotate
current log would be production.log and previous log would be
production.log.1 not production.log.1.gz
for Example the directory /log look like:
production.log
production.log.1
produciton.log.2.gz
produciton.log.3.gz
production.log.4.gz
7)notifempty
don't rotate if log file is empty
8)copytruncate
Truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a new one.
9)create 0666 devgroup devgroup
The file permission, user and group name of the rotated files
file permission====> 0666
username =====> devgroup
groupname =====> devgroup

Go and start play with logrotate.

Monday, April 7, 2008

Database backup script

The script to take a backup of postgres database

Steps:
Step i)
Write a shell script to dump the database.
Create a .sh file using vi editor inside the /bin as follows...
vi /bin/backup-script.sh

1) #!/bin/bash
2) BACKUP_DIR="/home/MyApps/regular_backup"
3) PGHOST="192.168.100.167"
4) PGUSER="postgres"
5) /usr/bin/pg_dumpall -h $PGHOST -U $PGUSER | gzip > $BACKUP_DIR/backup.gz


Explanation:
The first line #!/bin/bash is just a declaration that this is a BASH script.

2) BACKUP_DIR="/home/MyApps/regular_backup"
This line tells the target folder where our .gz file to be stored

3) PGHOST="192.168.100.167"
4) PGUSER="postgres"
Host IP where our postgres database is running and user name. If some other database have to add password .

5) /usr/bin/pg_dump -h $PGHOST -U $PGUSER | gzip > $BACKUP_DIR/backup.gz
This key statement is dump the database using command[/usr/bin/pg_dumpall] and user name and password[-h $PGHOST -U $PGUSER] and zip it up using gzip and target file backup.gz

after saved this file should the file permission to 755
chmod 755 /bin/backup-script.sh

Step ii) Add this script to crontab [Crontab (CRON TABle)is a program that manipulates the CRON daemon] and configure time to run script.

1) Edit crontab file in the directory /usr/bin/crontab file using crontab -e -e for edit/add entry in the crontab file.

2) Add this line in crontab file
10 0 * * * /bin/backup-script.sh >/dev/null 2>&1

This crontab run the script /bin/backup-script.sh on the time frame.

10 0 * * * is 10 minute 0 hours of every day every month end every year run the script which we have created in step 1[/bin/backup-script.sh]

Your script is ready and will run and backup the database everyday morning 00:10

Sunday, April 6, 2008

Online Video download

One of the place to surf online English films