Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

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.

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