您好,登錄后才能下訂單哦!
近期公司有一個數據庫需要遷移,但是里面創建的數據庫比較多,我們采取腳本將庫單獨備份,然后上傳到另一臺主機,新建數據庫,然后恢復數據庫。將自己寫的幾個小腳本粘貼到此,
分庫壓縮備份
#/bin/sh #version 0.1 MYUSER=mysqlback MYPASS=databack@162.com #SOCKET=/data/3306/mysql.sock MYLOGIN="mysql -u$MYUSER -p$MYPASS " MYDUMP="mysqldump -u$MYUSER -p$MYPASS -B" DATABASE="$($MYLOGIN -e "show databases;"|egrep -vi "Data|_schema|mysql")" #正則 for dbname in $DATABASE do MYDIR=/data/backup/$dbname [ ! -d $MYDIR ] && mkdir -p $MYDIR $MYDUMP $dbname|gzip >$MYDIR/${dbname}_$(date +%F).sql.gz done
批量解壓縮
#/bin/bash for i in `ls *.gz` do gunzip $i done 解壓結果: wl_smsgate_2017-04-12.sql wl_smsgate_hx_2017-04-12.sql wl_smsgate_ldkj_2017-04-12.sql wl_smsgate_tt_2017-04-12.sql wl_smsgate_dg_2017-04-12.sql wl_smsgate_jjbs_2017-04-12.sql wl_smsgate_pc_2017-04-12.sql wl_smsgate_tt_lt_2017-04-12.sql wl_smsgate_dxfj_2017-04-12.sql wl_smsgate_jl_2017-04-12.sql wl_smsgate_sb_2017-04-12.sql
批量創建數據庫
#/bin/bash #fun create database mysqluser=root mysqlpass=winnerlook123 mysqlcent="mysql -u $mysqluser -p$mysqlpass" dabasename=`cat databasename` date2="_2017-04-12.sql" for data in $dabasename do $mysqlcent -e "create database if not exists $data default character set utf8" done
恢復數據
#/bin/bash #fun create database mysqluser=root mysqlpass=winnerlook123 mysqlcent="mysql -u $mysqluser -p$mysqlpass" dabasename=`cat databasename` date2="_2017-04-12.sql" for data in $dabasename do $mysqlcent -e "use $data " && $mysqlcent -e " source /tmp/backup/$data$date2 "#恢復數據 done
然后檢查數據庫的數據字符集以及其數據條數。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。