sql dump adder

this script looks in a specific directory for sql dumps labeled xxx_20080101_xxx_alumn.sql.gz, xxx_20080101_xxx_const.sql.gz & xxx_20080101_xxx_log.sql.gz to ungzip and dump back into sql (assuming the db is there already). change as needed.

#!/bin/bash
# this script assumes that you have the sql backups gzipped
FILE_DIR=/export/tmp/backup/temp

if [ $# -lt 2 ]; then
        printf "\nneeds date of backup and switch: $0 <switch> xxxxyyzz\n"
        printf "ex: $0 test 200x0101\n"
        if [ $# -lt 1 ]; then
                printf "list of switches:\n----------\n"
                printf "alumn - gzip alumn backup then load to mysql\n"
                printf "log   - gzip logs backup then load to mysql\n"
                printf "const - gzip const backup then load to mysql\n"
                printf "all   - gzip all backups then load to mysql\n\n"
        fi
exit
fi

case $1 in

'const'|'log'|'alumn')
cd $FILE_DIR
SQL_FILES=`ls *$2*$1.sql.gz`
if [ $? -ne 0 ] ; then
        printf "date format either wrong or non-existant\n"
        exit
fi
#gzip -d *$2*$1.sql.gz
printf "\nLoading databases:\n"
for file in $SQL_FILES ; do
        abbr=`echo $file | awk '{gsub(/\./," ");print $2}'`
        printf "\n$abbr\n$file\n"
        #mysql -uroot $abbr < $file
done
exit
;;

'all')
cd $FILE_DIR
gzip -d *$2*.sql.gz
SQL_FILES=`ls *$2*.sql`
if [ $? -ne 0 ] ; then
        printf "date format either wrong or non-existant\n"
        exit
fi
printf "\nLoading databases:\n"
for file in $SQL_FILES ; do
        abbr=`echo $file | awk '{gsub(/\./," ");print $2}'`
        printf "\n$abbr\n$file\n"
        # printf "mysql $abbr < $file\n"
        mysql -uroot $abbr < $file
done
exit
;;

esac
«
»

    Leave a Reply

    Your email address will not be published. Required fields are marked *