-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Both InnoDB and PostgreSQL - as well as many other databases - use a technique called multi-version concurrency control (MVCC) to provide transaction isolation: transactions should not see the work of other, uncommitted transactions. MVCC means that, when a row is updated, the database stores both the old and new versions of the row.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Thursday, October 14, 2010

Mysql Backup Script - BASH & PERL

This BASH script will take the backup every day and at the end of the month, it will ARCHIVE the Last Day backup to Another Directory.
This Script will also send E-Mail if any thing goes wrong and Even send mails on Completion of Backup Process with Time taken.
#!/bin/bash
# set -x
start_time=`date +%k" Hour "-" "%M" Min "-" "%S" Sec."`
MyUSER="root"
MyPASS="hexademon"
MyHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
MYSQLADMIN="$(which mysqladmin)"
DEST="/mysqlstatsbck"
ARCHIVE="/mysqlarchive"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"
MAIL="$(which mail)"
SUBJECT1="Some thing goes wrong with Mysql Database. Backup operation not Performed"
EMAIL="delhi.manish@gmail.com"
EMAILMESSAGE="/tmp/emailmessage.txt"
HOST="$(hostname)"
DEST="/mysqlstatsbck"
MBD="$DEST/mysql-`(date +"%d-%m-%Y")`"

# Get data in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y")"

# File to store current backup file
FILE=""

# Store list of databases
DBS=""

# DO NOT BACKUP these databases
IGGY="test"

# Get all database list first
mysql_alive=`"$MYSQLADMIN" -uroot -phexademon -s ping|grep alive|wc -l`

if [ "$mysql_alive" == 1 ];then
{
if [ -d "$DEST" ] && [ -w "$DEST" ];then
{
echo "Ready to Perform Backup"
}
else
{
echo "Backup Directory either does not exists or not Writeable by User: `echo $USER|tr [a-z] [A-Z]`" > $EMAILMESSAGE
/bin/mail -s "$SUBJECT1" "$EMAIL" < $EMAILMESSAGE
exit 0
}
fi
[ ! -d $MBD ] && mkdir -p $MBD || :

# Only root can access it!
$CHOWN 0.0 -R $DEST
$CHMOD 0600 $DEST
DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"
for db in $DBS
do
skipdb=-1
if [ "$IGGY" != "" ];
then
for i in $IGGY
do
[ "$db" == "$i" ] && skipdb=1 || :
done
fi

if [ "$skipdb" == "-1" ] ; then
FILE="$MBD/$db.$HOST.$NOW.gz"
$MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE
fi
done

end_time=`date +%k" Hour "-" "%M" Min "-" "%S" Sec."`
echo "Backup Operation Started at $start_time  and Ended at $end_time" > $EMAILMESSAGE
/bin/mail -s "Mysql Database Backup Process Detail" "$EMAIL" < $EMAILMESSAGE
}
else
{
echo "Oops Can't Connect to Database, may be your database server is DOWN" > $EMAILMESSAGE
/bin/mail -s "$SUBJECT1" "$EMAIL" < $EMAILMESSAGE
exit 0;
echo
}
fi

dim_m=`date +%m`
dim_y=`date +%Y`
days=`cal $dim_m $dim_y | egrep -v '[A-Za-z]' | wc -w`
date_d=`date +%d`

if [ "$date_d" == "$days" ];
then
echo "Archiving Data"
cd $DEST
find .  -type d -mtime -1 -exec ls  -d {} \; | cut -d/ -f2| grep -vE '\.' | xargs mv `awk '{print $1}'` "$ARCHIVE/"
rm -rf $DEST/*
#else
#echo "Today is not the Last day of the month, so nothing to archve";
fi


Mysql Backup Script - PERL


#!/usr/bin/perl
use File::Basename;

$MyUSER="root";
$MyHOST="localhost";
$MyPASS="hexademon";
$BSE="Bse";

$DBS=`mysql -u $MyUSER -h $MyHOST -p$MyPASS -$BSE "show databases"`;

$backup_folder = '/mysqlstatsbackup/Databases';

my $config_file = "/tmp/dbbackup.config";

open(DATA, ">$config_file");
print DATA <<"MYDATABASE";
$DBS
MYDATABASE
close(DATA);

my @databases = removeComments(getFileContents($config_file));

chdir($backup_folder) or die("Cannot go to folder '$backup_folder'");

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$mon++;
#Zero padding
$mday = '0'.$mday if ($mday<10);
$mon = '0'.$mon if ($mon<10);

my $folder = "$year-$mon-$mday";
mkdir($folder) or die("Cannot create a folder called '$folder'");

foreach my $database (@databases) {
next if ($database eq '');
chomp($database);

my $table = '';
if(index($database,' ')+1) { #Get just 1 table in the database - if there is a ' '(space) in the db name
my @parts = split(' ',$database);
$database = $parts[0];
$table = $parts[1];
}
print "Backing up $database ... ";
my $file = $database;
$file .= '_' . $table if($table ne '');
$file .= ".sql";
`mysqldump -u root -p$MyPASS $database $table > $folder/$file`;
print "Done\n";
}

print "Compressing the folder ... ";
`tar -czf $folder.tar.gz $folder/`;
print "Done\nRemoving Folder ... ";
`rm -rf $folder`;
print "Done\n\n";

sub getFileContents {
my $file = shift;
open (FILE,$file) || die("Can't open '$file': $!");
my @lines=;
close(FILE);
return @lines;
}

sub removeComments {
my @lines = @_;

@cleaned = grep(!/^\s*#/, @lines); #Remove Comments
@cleaned = grep(!/^\s*$/, @cleaned); #Remove Empty lines
return @cleaned;
}

1 comment: