MySQL backup and restore

因為翰x要移機,所以要把db 上的資料轉出來,不過因為用mysql db < db.sql
常常會出他媽的鬼問題,很杜爛!!!
所以單純把資料dump 成 text file 會是一個比較好的方式,然後再透過mysqlimport
或是LOAD DATA INFILE
我寫了兩個小小的scripts
就是dump和 restore
有須要的人請自行服用 ::-p:
1.import_db.sh

db_dir=$1
db=$2
for file in `find $1 -type f -depth 1 | egrep txt`
do
table=`echo $file |cut -d “/” -f 5|cut -d “.” -f 1`
echo ‘import data to ‘$table’ finished ‘
/usr/local/bin/mysql -u root -p -e “load data infile ‘$file’ into table $table” $db
done

2.dump_db.sh

date=`date +%Y%m%d`
db=$1
dump_dir=$2
charset=$3
#Dump DB structure
/usr/local/bin/mysqldump -uroot -p -d $db > $dump_dir/$db.sql
# Dump Data
chown mysql:mysql $dump_dir
/usr/local/bin/mysqldump -u root -p -t –default-character-set=$charset –tab=$dump_dir $db
#tar -zcf /tmp/$db.$date.tar.gz $dump_dir

我的shell script 寫的很爛~~
別見笑~哈 :8-):

PS:剛剛在刻這篇的時後,邊和lyz講電話,一邊在shell 上工作
沒想到,他媽的不小心
rm -rf * .txt(是想刪掉所有的txt的)
結果因為多打了一個space變成我把資料全刪了
我當時在 ~ 一堆東西沒了~~~幹!!! ::S:

About johnpupu

..........
This entry was posted in MySQL and tagged . Bookmark the permalink.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word