Latest blog entry

Shell script to backup a Mysql database and save it on a remote server using Ftp

July 23, 2010    |   Posted by admin   |    Category: Scripts

The following shell script will dump the mysql database and will save the .sql file on a remote location using Ftp. This script will create a backup file including the current date so you can have multiple copies of the backups of the same database under one directory.

Create a file called mysqlbkup.sh

# vi /root/mysqlbkup.sh

and paste the following code in the file as it is.

##############START OF THE SCRIPT##############

#!/bin/bash

# Specify the temporary backup directory.
BKUPDIR="/tmp"

# Specify Database Name
dbname="dbname_here"

# Save the current date
date=`date '+%Y-%m-%d'`

# Specify Ftp details of the remote server.
ftpserver="FtpServerIP"
ftpuser="your ftp user"
ftppass="your ftp password"

# Dump the mysql database with the current date and compress it.
# Save the mysql password in a file and specify the password file
# path in the below command.

/usr/bin/mysqldump -uroot -p`cat /path/to/pass/file` $dbname\
 | gzip > $BKUPDIR/$date.$dbname.sql.gz

# Change directory to the backup directory.
cd $BKUPDIR

# Upload the backup
ftp -n $ftpserver <<!EOF!
user $ftpuser $ftppass
binary
prompt
mput *.sql.gz
quit
!EOF!

# Remove the local backup file.
/bin/rm -f /$BKUPDIR/$date.$dbname.sql.gz

##############END OF THE SCRIPT##############

Make sure to specify your db_name, ftpserver, ftpuser and ftppass values in the script.

Save the file and schedule a cronjob to execute the file on daily basis, say during night hours at 1.00AM. Edit the cron file

# crontab -e

and set the following cronjob

0 1 * * * /bin/sh /root/mysqlbkup.sh

save the file and restart the crond service

# service crond service

The script will work on a Linux/Plesk server as well. You just have to replace the mysqldump line in the script with the following

/usr/bin/mysqldump -uadmin -p`cat /etc/psa/.psa.shadow` $dbname\
 | gzip > $BKUPDIR/$date.$dbname.sql.gz

Note: Leave a comment if you have any suggestions, questions OR have received any error message using this script.

Shell Script to Monitor Load Average on a Linux server

July 10, 2010    |   Posted by admin   |    Category: Scripts

Load Average on a server reflects the current state of the server. Higher the load average, poorer is the server performance hence it is a necessity to monitor the load average on the server.

The following shell script monitors the load average on the Linux server and inform the server administrator with the load average and the process that are running if the load average is greater than the defined threshold.

Create a file, say, /root/monit_loadaverage.sh and paste the following script in it:

############### START OF THE SCRIPT ###############

#!/bin/bash

# Define Variables
CUR_TIME=`date +"%A %b %e %r"`
HOSTNAME=`hostname`

# Retrieve the load average of the past 1 minute
Load_AVG=`uptime | cut -d'l' -f2 | awk '{print $3}' | cut -d. -f1`
LOAD_CUR=`uptime | cut -d'l' -f2 | awk '{print $3 " " $4 " " $5}'\
 | sed 's/,//'`

# Define Threshold. This value will be compared with the current
# load average. Set the value as per your wish.
LIMIT=5

# Compare the current load average with the Threshold value and
# email the server administrator if the current load average
# is greater.
if [ $Load_AVG -gt $LIMIT ]
then

#Save the current running processes in a file
/bin/ps auxf >> /root/ps_output

# Save the other values in a file
echo "Current Time :: $CUR_TIME" >> /tmp/monitload.txt
echo "Current Load Average :: $LOAD_CUR" >> /tmp/monitload.txt
echo "The list of current processes is attached\
 with the email for your reference." >> /tmp/monitload.txt
echo "Please Check... ASAP."  >> /tmp/monitload.txt

# Send an email to the administrator of the server
/usr/bin/mutt -s "ALERT!!! High 1 minute load average\
 on '$HOSTNAME'" -a /root/ps_output youremail@youremail.com <\
/tmp/monitload.txt

fi

# Remove the temporary log files
/bin/rm -f /tmp/monitload.txt
/bin/rm -f /root/ps_output

############### END OF THE SCRIPT ###############

Make sure you replace the Treshold (LIMIT) value and your email address with your own values in the script.

Now, schedule a cronjob to execute the script on per minute basis. Edit the cronjob file

# crontab -e

and place the following cronjob at the end of the file

* * * * * /bin/sh /root/monit_loadaverage.sh

restart the crond service

# service crond restart

In order to use “mutt” to send emails, you need to install the mutt package on the server. It allows you to send emails with attachments.

# yum install mutt

Note: Please place a comment if you receive any error message while executing this script OR you need some modifications in the script.

Script to terminate suspended accounts on a cPanel server

June 7, 2010    |   Posted by admin   |    Category: Scripts

How to check suspended accounts on a cPanel server and terminate them after a specific time frame?

Many a times you suspend an account on the server and it goes unnoticed for months thus wasting disk space of your server. You can use the following script and schedule it to execute, say once in a day which will delete the suspended accounts from the server.

Create a file called terminatesuspended.sh

# pico /root/terminatesuspended.sh

Add the following code

for i in `find /var/cpanel/suspended/ -mtime +30 |\
  cut -d'/' -f5 | sed '1 d'` 
 do 
 y | /scripts/killacct $i;
 done;

The username of suspended accounts are saved under the /var/cpanel/suspended directory. The find command will search the /var/cpanel/suspended/ directory for files which are 30 days old and will pass the username to the ‘killacct’ command.

Save the file and set a cronjob to execute the file once in a day. Edit the root cronjob file

# crontab -e

Add the following cronjob

0 1 * * * /bin/sh /root/terminatesuspended.sh

The above cronjob will terminate the account that is been suspended from more than 30 days at 1.00AM.

Comments Off

Script to email successful Ftp logins

December 1, 2009    |   Posted by admin   |    Category: Scripts

Shell Script to email Successful Ftp Logins.

This Shell script will search the server logs on daily basis and will email you the successful Ftp Logins of the day. The ftp logs are saved in the /var/log/messages file as by default there is no separate log file for Ftp in Linux.

Create a file /home/script/ftplogins.sh and paste the below code:

#!/bin/bash

#Retrieve the current date

CUR_DATE=`date +”%b %e”`

#Create a temporary file to store the logs
touch /tmp/out.txt

echo “List Follows” > /tmp/out.txt

#Search the successful attempts and save in the temporary file

/bin/grep “$CUR_DATE” /var/log/messages | grep pure-ftpd | grep logged >> /tmp/out.txt

#Email the contents of the file to your email address
/bin/mail -s “Successful Ftp Login Attempts on ‘$CUR_DATE'” youremail@yourdomain.com < /tmp/out.txt

Save the file. You now have to schedule a cron to execute the file once in a day to search logs. Edit the cron file

crontab -e

and add the following cron job

59 23 * * * /bin/sh /home/script/ftplogins.sh

Note:

1) This script will work with Pure-Ftpd server. You will have to edit the search string a bit according to your Ftp server.

2) If you copy/paste the script as it is in shell, the single and double quotes may change to dots (.) so make sure you correct them before executing the script.

Script to email failed Ftp login attempts

November 29, 2009    |   Posted by admin   |    Category: Scripts

Shell Script to search Failed Ftp Login Attempts

This Shell script will search the server logs on daily basis and will email you the Failed Ftp Login Attempts of the day. The ftp logs are saved in the /var/log/messages file as by default there is no separate log file for Ftp in Linux.

Create a file /home/script/failedftp.sh and paste the below code:

#!/bin/bash

#Retrieve the current date

CUR_DATE=`date +”%b %e”`

#Create a temporary file to store the logs
touch /tmp/out.txt

echo “List Follows” > /tmp/out.txt

#Search the failed attempts and save in the temporary file

/bin/grep “$CUR_DATE” /var/log/messages | grep pure-ftpd | grep failed >> /tmp/out.txt

#Email the contents of the file to your email address
/bin/mail -s “Failed Ftp Login Attempts on ‘$CUR_DATE’ ” youremail@yourdomain.com < /tmp/out.txt

Save the file. You now have to schedule a cron to execute the file once in a day to search logs. Edit the cron file

crontab -e

and add the following cron job

59 23 * * * /bin/sh /home/script/failedftp.sh

Note:

1) This script will work with Pure-Ftpd server. You will have to edit the search string a bit according to your Ftp server.

2) If you copy/paste the script as it is in shell, the single and double quotes may change to dots (.) so make sure you correct them before executing the script.

Comments Off