- 论坛徽章:
- 0
|
#!/bin/sh
#
# Copyright (C), 2007 bug All Rights Reserved
# Title : Database Backup Script
# Author : BUG
# File : db_bak.sh
# Version : 1.1.0
# Date : 2008-03-21
# Email : efbbb27115@homtail.com
# License : General Public License (GPL) v2
# Description : Database Backup Script
#
initial() {
echo -en "\33[2J"
echo -en "\33[0;0H"
echo -en "\33[32m"
echo "------------------------------------------------------------"
echo "--- Database Backup Script ---"
echo "------------------------------------------------------------"
echo -en "\33[37m"
stty -echo
umask 077
mysqln=`echo 'show databases;' | /usr/local/mysql/bin/mysql -psjldafhkg | sed -n '3,$p'`
mysqlu="root"
mysqlp="********"
binpath="/usr/local/mysql/bin"
dstpath="/pri/bak/sql_bak/"
runuser=`ps aux | grep $$ | sed -n "1p" | awk '{ print $1 }'`
mkdir -p $dstpath/`date +%y%m%d`
exec 3>>$dstpath/`date +%y%m%d`/db_bak.log
}
initial
echo -e "\n[`date +%y/%m/%d\ %H:%M:%S`] Database Backup Script start by $runuser PID:$$" >&3
if [ -f "$dstpath/`date +%y%m%d`/lock" ]
then
echo -e "\33[33mIt's has completed by `cat $dstpath/\`date +%y%m%d\`/lock`!!!\33[37m"
echo "[`date +%y/%m/%d\ %H:%M:%S`] It's has completed by `cat $dstpath/\`date +%y%m%d\`/lock`!!!" >&3
else
echo -e "Database\tExport\tCompress\tSql\tTgz\tRate"
echo -e "\33[32m------------------------------------------------------------\33[37m"
cd $dstpath/`date +%y%m%d`
for db in $mysqln
do
echo -n "$db"
$binpath/mysqldump --opt -u$mysqlu -p$mysqlp $db > $dstpath/`date +%y%m%d`/$db.sql 2> /dev/null
RETVAL1=$?
if [ $RETVAL1 -eq 0 ]
then
sqlsize=`du -k $db.sql | cut -f1`
echo -n "[`date +%y/%m/%d\ %H:%M:%S`]" >&3
echo -n "$db" | awk '{printf " %-17s", $1}' >&3
echo -n "OK " >&3
echo -en "\r\t\t \33[32mOK\33[37m\t"
tar -czvf $db.tgz $db.sql > /dev/null 2>&1
RETVAL2=$?
if [ $RETVAL2 -eq 0 ]
then
tgzsize=`du -k $db.tgz | cut -f1`
echo "OK" >&3
rm -f $dstpath/`date +%y%m%d`/$db.sql
echo -en " \33[32mOK\33[37m"
echo -en "\33[33m"
echo -en "$sqlsize" | awk '{printf "%12sKB", $1}'
echo -en "$tgzsize" | awk '{printf "%6sKB", $1}'
echo "$sqlsize $tgzsize" | awk '{printf "%8.0f%",$2/$1*100}'
echo -e "\33[37m"
else
echo "Compress fail return $RETVAL2" >&3
echo -e " \33[31mfail\33[37m"
fi
else
echo -n "[`date +%y/%m/%d\ %H:%M:%S`]" >&3
echo -n "$db" | awk '{printf " %-17s ", $1}' >&3
echo "Export fail return $RETVAL1" >&3
echo -en "\r\t\t \33[31mfail\33[37m"
fi
done
echo "`date +%H:%M:%S`">$dstpath/`date +%y%m%d`/lock
fi
[attachimg]239897[/attachimg] |
[ 本帖最后由 a8e8c1ca5b 于 2008-3-21 22:29 编辑 ] |
|