Dec 20

Hi it has been along time since my last blog, i found this usefull script on this forum http://forums.asmallorange.com/topic/13649-shell-script-to-monitor-file-changes/. I just want to share it 🙂

just go to your shell and create scan.sh files

# vim scan.sh

and type this

####################################################################################################################

#!/bin/bash

#Directory to search
myDir=/home/username/public_html

#Set frequency of command in minutes, this should match how often you run the cron job
myFrequency=’-30′

#email address for mailing the results
myEmail=email@address.com

#Create datestamp for subject line
#This makes each subject line unique to prevent message collapsing in Gmail
myDate=`date +%y-%m-%d`
myTime=`date +%H:%M`

#Test if files have been edited
fileCount=`find $myDir -mmin $myFrequency -type f | wc -l`
if [ $fileCount -gt 0 ]
then
#Write the subject line and set correct form of the word “files” (singular or plural)
if [ $fileCount -eq 1 ]
then
mySubject=”Attention! $fileCount File Modified on $myDate at $myTime”
else
mySubject=”Attention! $fileCount Files Modified on $myDate at $myTime”
fi

#execute find command and email the results
find $myDir -mmin $myFrequency -type f | mail -s “$mySubject” $myEmail
#else nothing happens
fi

####################################################################################################################

then save it chmod 755 and run it (./scan.sh)

or you can create crontab to run every 30 minute

# crontab -e

*/30 * * * * /root/scan.sh

Aug 24

It’s been a while i haven’t post anything 🙂 , i got this request on how to backup all of mysql databaseand since the hdd is not quite big it also required to delete the old backup. So here is the backup script

###################################################################################################

#!/bin/sh

DATE=`date +%d%b-%H%M%S`

unset PATH
# USER VARIABLES
MYSQLUSER=user
MYSQLPWD=password
MYSQLHOST=localhost
MYSQLBACKUPDIR=/home/backup/mysql/mysql_backup$DATE

FILE=$MYSQLBACKUPDIR.tgz; # No need to modify this one

# PATH VARIABLES – It depend on your server program
MK=/bin/mkdir;
RM=/bin/rm;
GREP=/bin/grep;
MYSQL=/usr/local/mysql/bin/mysql;
MYSQLDUMP=/usr/local/mysql/bin/mysqldump;
XARGS=/usr/bin/xargs;
FIND=/usr/bin/find;
TAR=/bin/tar;
UUENCODE=/usr/bin/uuencode;

# CREATE MYSQL BACKUP

# Delete the old ones. The +7 means 7 days ago
$FIND /home/backup/mysql/ -mtime +7 |$XARGS $RM -rf

# Create new backup dir
$MK -p $MYSQLBACKUPDIR

#Dump new files
for i in $(echo ‘SHOW DATABASES;’ | $MYSQL -u$MYSQLUSER -p$MYSQLPWD -h$MYSQLHOST|$GREP -v ‘^Database$’); do
$MYSQLDUMP                                                    \
-u$MYSQLUSER -p$MYSQLPWD -h$MYSQLHOST                         \
-Q -c -C –add-drop-table –add-locks –quick –skip-lock-tables   \
$i > $MYSQLBACKUPDIR/$i.sql;
done;

# collect and compress the sqls
$TAR -cvzpf $MYSQLBACKUPDIR.tgz $MYSQLBACKUPDIR
$RM -r $MYSQLBACKUPDIR

###################################################################################################

Then save it , i save it as mysql_backup file. Then open the crontab to run it weekly

# crontab -e

then put this

0 0 * * 0 /root/mysql_backup > /dev/null 2>&1