Wednesday, November 19, 2008

My sql backup script

#!/bin/bash
# Bcakup of Cyclone MySQL one/all databases for 7 days
# Umakanta Samantaray
# Date 10th Sep 2008
#
#
#

DBBACKUPDIR=/home/backups/mysql
DTSTAMP=`date "+%Y%m%d@%H%M"`
DBNAME=forecast_tracker_prod

# Only one database backup ie "forecast_tracker_prod"
if [ -d $DBBACKUPDIR ] ; then
mysqldump -u mysqlbackupadmin -pmysql@cyclone -h localhost --opt -B $DBNAME \
> $DBBACKUPDIR/cyclone.mysql.${DBNAME}.${DTSTAMP}.sql
bzip2 -sz $DBBACKUPDIR/cyclone.mysql.${DBNAME}.${DTSTAMP}.sql
find $DBBACKUPDIR -type f -name '*.sql.*' -mtime +7 -exec rm {} \;

else
echo $DBBACKUPDIR does not exist
exit 1
fi

# All databases in a single file.
# mysqldump -u mysqlbackupadmin -pmysql@cyclone -h localhost --opt --all-databases > $DBBACKUPDIR/cyclone.mysql.alldatabases.$DTSTAMP.sql

Log delete

#!/bin/bash
#
# Delete Torque log files which are older than 7days in cylone.
# Date 18th Sep 2008
# Umaknata Samantaray.
#
#
TORQUEACCLOG=/opt/torque/server_priv/accounting
TORQUESERLOG=/opt/torque/server_logs

if [ -d $TORQUEACCLOG ] ; then
cd $TORQUEACCLOG && find . -type f -name '*' -mtime +7 -exec rm {} \;
else
echo $TORQUEACCLOG does not exist.
fi

if [ -d $TORQUESERLOG ] ; then
cd $TORQUESERLOG && find . -type f -name '*' -mtime +7 -exec rm {} \;
else
echo $TORQUESERLOG does not exist.
fi




#!/bin/bash
# Reduce the size to 0 if the size of moab.log is more than 1GB
# Date 18th Sep 2008
# Umakanta Samantaray
#
MOABLOGPATH=/opt/moab/log
FILE=moab.log
if [ -d $MOABLOGPATH ] ; then
{
SIZE=`du -sk ${MOABLOGPATH}/${FILE} | cut -d'/' -f1`
if [ ${SIZE} -ge 1048576 ] ; then
cat /dev/null > ${MOABLOGPATH}/${file}
else
echo "Size of ${FILE} is less than 1GB."
fi
}
else
echo "${MOABLOGPATH} does not exist."
fi

SVN backup script

#!/bin/bash
#
# Backup of Subversion repos by svnadmin
# Umakanta Samantaray
# Nov 2008
#
# It will take full backup on Sunday and incremental backup daily at 00:00( set in crontab)
#
# full-backup tarball filenames will be of the form
# fullbackup.[repos name].YYYYMMDD@HHMM
# and Incremental will be incrembackup.[repos name].YYYYMMDD@HHMM
# This ensures that a numerical sort (sort -n) will produce proper date order
# Crontab log directory is /home/usamantaray/svnbackup.log
#

svn_repodir=/srv/svn/repos
backupdir=/home/backups/svnbackup
dtstamp=`date "+%Y%m%d@%H%M"`
date

# Create a file which will contain the repository directory list
tempdirlist=/tmp
cd $svn_repodir && ls -d */ | xargs -l basename > ${tempdirlist}/dirlist.`date "+%Y%m%d"` && cd -
sed -i '/infrastructure/,1 d' ${tempdirlist}/dirlist.`date "+%Y%m%d"`
sed -i '/livecat.original/,1 d' ${tempdirlist}/dirlist.`date "+%Y%m%d"`

if [ "`date +"%a"`" = "Sun" ] ; then
echo "Grab Full Backup"
{
cd $svn_repodir && echo > revision_no && cd -

value=1
while read line
do
value=`expr $value + 1`;
echo "`svnlook youngest ${svn_repodir}/${line}` ${line}" >> ${svn_repodir}/revision_no
svnadmin dump ${svn_repodir}/${line} > ${backupdir}/fullbackup.${line}.${dtstamp} \
&& find ${backupdir} -type f -name '*backup.*' -mtime +14 -exec rm {} \;

done < ${tempdirlist}/dirlist.`date "+%Y%m%d"`
cat ${svn_repodir}/revision_no
}
else
echo "Incremental Backup"
{
value=1
while read line
do
value=`expr $value + 1`;
RECENT=`svnlook youngest ${svn_repodir}/${line}`
REVNO=`cat ${svn_repodir}/revision_no | grep "\<${line}\>" | cut -d" " -f1`
LAST=`expr ${REVNO} + 1`
if [ "${REVNO}" ] ; then
{
if [ ${RECENT} != ${REVNO} ] ; then
{
echo "Incremental Backup of ${line} is Rev no ${LAST}:${RECENT}"
svnadmin dump --incremental -r ${LAST}:${RECENT} ${svn_repodir}/${line} > ${backupdir}/incrembackup.${line}.${dtstamp}
sed -i "/${REVNO} ${line}/,1 d" ${svn_repodir}/revision_no
echo "`svnlook youngest ${svn_repodir}/${line}` ${line}" >> ${svn_repodir}/revision_no
}
fi
}
fi
done < ${tempdirlist}/dirlist.`date "+%Y%m%d"`
}
fi

# remove the temp dir list file
if [ -d ${tempdirlist} ] ; then
cd ${tempdirlist} && find ./ -type f -name 'dirlist*' -exec rm {} \;
else
echo " ${tempdirlist} does not exist"
fi

exit