Skip to content

Instantly share code, notes, and snippets.

@usual-tools
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usual-tools/3f1e020a5f3aa3a3fc82 to your computer and use it in GitHub Desktop.
Save usual-tools/3f1e020a5f3aa3a3fc82 to your computer and use it in GitHub Desktop.
#!/bin/sh
# 別ファイルの設定値を読み込む。${HOME} からの相対パス。
source ./sample-tools/config.conf
#### - - - - - - - - - - - - - -
#### DB バックアップの設定値
# 入力値の数判定、4 より少ない場合はエラー終了
if [ $# -lt 4 ]
then
echo "Please input 4 argument ! Now $#"
exit 1
fi
target_db_name=$1 # 第 1 引数は DB 名
target_db_user=$2 # 第 2 引数は DB ユーザー名
target_db_pass=$3 # 第 3 引数は DB パスワード
target_db_host=$4 # 第 4 引数は ホスト
# ローテーション日数
bkup_days=30
# 今日の日付
today=`date +%Y%m%d`
# ローテーション過ぎた日
past_date=`date "-d$bkup_days days ago" +%Y%m%d`
# バックアップのディレクトリー
bkup_dir=${bkup_base_dir}/db/${target_db_name}
sql_file=${target_db_name}_${today}.sql
bkup_file=${sql_file}.tar.gz
bkup_file_past=${target_db_name}_${past_date}.sql.tar.gz
#### - - - - - - - - - - - - - -
#### ディレクトリーを移動して、そこで作業
cd $bkup_dir
if [ $? != 0 ]; then
echo "Backup directory does not exist."
exit 1
fi
#### - - - - - - - - - - - - - -
#### DB をダンプ
mysqldump $target_db_name -u $target_db_user -p$target_db_pass -h $target_db_host --opt > $sql_file
if [ $? != 0 -o ! -e $sql_file ]; then
echo "Cannot dump database."
exit 1
fi
#### - - - - - - - - - - - - - -
#### アーカイブと圧縮
tar cfz $bkup_file $sql_file
#### - - - - - - - - - - - - - -
#### 一時的な sql ファイルを削除
echo "Del Temporary file"
${tools_dir}/rm.php ${bkup_dir}/${sql_file}
#### - - - - - - - - - - - - - -
#### ローテーション処理
echo "Rotation"
${tools_dir}/rm.php ${bkup_dir}/${bkup_file_past}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment