0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

mysqlでデータベース毎にダンプをとる

Posted at

何をするか?

データベース毎にダンプをとる。

shの作成

ホスト名やユーザ名は共通する場所に置いておく。

# conf.txt
host="ホスト名"
user="ユーザ名"
password="パスワード"
dump_path="ダンプの保存場所"
db_name_list_file="データベース名を保存するファイル名"

データベース名を取得して保存するshを作成する。
ホスト名などは上で作成したファイルから取得する。

# db_name.sh
cd `dirname $0`

. ./conf.txt

show_command="show databases;"

db_name_list=$dump_path"/"$db_name_list_file

$(mysql -h $host -u $user -p$password -N -e "$show_command" > $db_name_list)

保存したデータベース名を読み取って、ダンプを取る。
gizpに圧縮してファイルの容量を減らす。

# db_dmup.sh
cd `dirname $0`

. ./conf.txt

read_file=$dump_path"/"$db_name_list_file

while read line
do
  $(mysqldump -u $user -h $host -p"$password" $line --no-tablespaces | gzip > ${dump_path}/${line}_dump.sql.gz)
done < $read_file

あとは、順番に動くようにcronで自動実行の設定をする。

おわりに

さくらのレンタルサーバで複数のアプリが起動していて、役割が違うデータベースが存在するので、このようなスクリプトを作成してみました。
今後はデータベースが増えても自動でダンプが取れるはず。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?