Add new comment

Want to support HowtoForge? Become a subscriber!
Submitted by Anonymous (not registered) on Sat, 2008-10-11 09:27.
# USERNAME
MyUSER="`cat /etc/mysql/debian.cnf | grep user | uniq | awk -F'=' '{print $2}'`"
# PASSWORD
MyPASS="`cat /etc/mysql/debian.cnf | grep password | uniq | awk -F'=' '{print $2}' | \
sed -e 's/^ //'`"
# Hostname
MyHOST="localhost"
# Servername
SERVER="$HOSTNAME"
# mysqldump options
MYSQLDUMPOPTS="--opt"
# Linux bin paths, change this if it can't be autodetected via which command
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"

# Backup Dest directory, change this if you have someother location
DEST="/root/backups"
# Main directory where backup will be stored
MBD="$DEST/mysql"
#create if not existing
if [ ! -e "$MBD" ]; then
        mkdir -p $MBD
fi
# Get data in dd-mm-yyyy format
NOW="$(date +"%m-%d-%Y")"
FNAME="mysql-backup"
# File to store current backup file
FILE=""
# Store list of databases
DBS=""
TMP="/tmp/$NOW"
mkdir -p $TMP
n=5;
# the first archive number must be zero
l=0;

while [ ! $n -le 0 ]; do
        let "m = $n - 1"
        if [ -e $MBD/$FNAME.$m.tar.gz ]; then
                mv $MBD/$FNAME.$m.tar.gz $MBD/$FNAME.$n.tar.gz
        fi
        let "n = $n - 1"
done



# DO NOT BACKUP these databases
# IGGY="test"

[ ! -d $MBD ] && mkdir -p $MBD || :
[ ! -d $TMP ] && mkdir -p $TMP || :
# Only root can access it!
$CHOWN root.root -R $DEST
$CHMOD 600 $DEST

# Get all database list first
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="$db.$SERVER.$NOW.sql"
       # do all inone job in pipe,
       # connect to mysql using mysqldump for select mysql database
       # and pipe it out to gz file in backup dir :)
       $MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $MYSQLDUMPOPTS $db > $TMP/$FILE
   fi
done
tar czvf $MBD/$FNAME.0.tar.gz $TMP &> /dev/null
rm /tmp/$NOW -r
exit 0
Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.

Reply

*
*
The content of this field is kept private and will not be shown publicly.


*

  • Images can be added to this post.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <div>
  • Lines and paragraphs break automatically.