Created
March 18, 2013 03:12
-
-
Save shizm/5184762 to your computer and use it in GitHub Desktop.
MySQLのデータベースをバックアップする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# MySQL バックアップスクリプト | |
# | |
# 指定された場所にバックアップを作成する | |
# 対象データベース | |
BACKUP_DB_NAME=testdb | |
DB_USER=user | |
DB_PASSWORD=pass | |
DB_HOST=localhost | |
# バックアップ先ディレクトリ設定 | |
BACKUP_DIR=$HOME/backup/$BACKUP_DB_NAME/`date +%Y%m%d` | |
### 設定はここまで ### | |
### バック処理開始 ### | |
# バックアップディレクトリが存在しない場合は事前に作成 | |
if [ ! -d $BACKUP_DIR ] | |
then | |
echo "バックアップディレクトリ(${BACKUP_DIR})が存在しないため作成" | |
mkdir -p $BACKUP_DIR | |
fi | |
echo "バックアップ処理を開始します" | |
sequence=1 | |
TARGET_FILE=$BACKUP_DIR/${BACKUP_DB_NAME}_`date +%Y%m%d`_${sequence}.sql.gz | |
while [ -e ${TARGET_FILE} ] | |
do | |
sequence=`expr $sequence + 1` | |
TARGET_FILE=$BACKUP_DIR/${BACKUP_DB_NAME}_`date +%Y%m%d`_${sequence}.sql.gz | |
done | |
echo $TARGET_FILE | |
mysqldump -u$DB_USER -p$DB_PASSWORD -h $DB_HOST --opt $BACKUP_DB_NAME | gzip > $TARGET_FILE | |
echo "バックアップ処理が完了しました" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment