Board index Linux Shell Scripts

Moderator: chandranjoy

MySQL DB dump script

Postby chandranjoy » Mon Nov 01, 2010 5:21 pm

Code: Select all
#!/bin/bash

DESTDIR="/var/backups/mysql"

if [ ! -e ${DESTDIR} ]; then
        mkdir -p $DESTDIR || exit 1
fi

CURHOUR=`date "+%H"`

DATABASES=`echo "show databases" | mysql | grep -v -E "^Database$"`
for database in ${DATABASES}; do
        mysqldump --single-transaction "$database" | gzip -9 > "$DESTDIR/$database-$CURHOUR:00.sql.gz"
done
chandranjoy
Site Admin
 
Posts: 283
Joined: Fri Oct 23, 2009 11:19 pm

Another MySQL DB dump script

Postby chandranjoy » Wed Feb 16, 2011 7:55 pm

Code: Select all
#!/bin/bash

DESTDIR="/var/backups/mysql"
MYSQLDUMP="/usr/bin/mysqldump"
MYSQLARGS=""
MYSQL="/usr/bin/mysql"
MYCNF="$HOME/.my.cnf"
DEBIAN_CNF="/etc/mysql/debian.cnf"

if [ ! -e ${DESTDIR} ]; then
        mkdir -p $DESTDIR || exit 1
fi

if [ ! -f "$MYSQLDUMP" ]; then
        exit 1
fi

if [ ! -f "$MYCNF" ]; then
        if [ ! -f "$DEBIAN_CNF" ]; then
                exit 1
        fi
        MYSQLARGS="--defaults-extra-file=$DEBIAN_CNF"
fi

rm -f ${DESTDIR}/*

CURDATE=`date "+%Y%m%d"`

DATABASES=`echo "show databases" | $MYSQL $MYSQLARGS | grep -v -E "^Database$"`
for database in ${DATABASES}; do
        $MYSQLDUMP $MYSQLARGS "$database" | gzip -9 > "$DESTDIR/$database-$CURDATE.gz"
done
chandranjoy
Site Admin
 
Posts: 283
Joined: Fri Oct 23, 2009 11:19 pm


Return to Shell Scripts

Who is online

Users browsing this forum: No registered users and 0 guests


cron