目次

概要

MySQLのDBダンプを定期作成するスクリプト。

ソース~

#!/bin/sh
# ------------------------
#  mysql dump make script
# ------------------------
# Version : 1.0
# date    : 2010/01/13

export LANG=C
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/mysql/bin:/usr/local/mysql/sbin:$PATH

## Set
ROTATE="30"
DBUSER="[DB USER]"
DBPASS="[DB PASSWD]"
DATEM=`date +%Y%m`
BACKUPLOG="/var/logs/backup_"$DATEM".log"  # Backup Working Log
BACKUPDIR="/home/system/backup"            # Backup Local Space
BACKUPSRV="/mnt/database/backup" # Backup Network Space

## Log
LOGDATE=`date +%Y/%m/%d\ %H:%M:%S`
echo $LOGDATE" DB backup start : "$1" : "$2 >> $BACKUPLOG

## Check

# TARGET
if [ -z $1 ]; then
 echo "ERR : target Database unknown" >> $BACKUPLOG
 echo "ERR : target Database unknown"
 echo "USE : ./mysql-dumpbackup.sh  [TargetDatabase] [OUTPUT FILE NAME]"
 exit 0
else
 DBNAME=$1
fi

# OUTPUT
if [ -z $2 ]; then
 echo "ERR : Output File unknown" >> $BACKUPLOG
 echo "ERR : Output File unknown"
 echo "USE : ./mysql-dumpbackup.sh  [TargetDatabase] [OUTPUT FILE NAME]"
 exit 0
else
 OUTFILE=$2
fi

# Dir Check
if [ -w !$BACKUPDIR ]; then
 echo "ERR : "$BACKUPDIR" not write" >> $BACKUPLOG
 echo "ERR : "$BACKUPDIR" not write"
 exit 1
fi

## Exec
cd $BACKUPDIR

# Rotate

test -f $OUTFILE.gz.$ROTATE && rm -rf $OUTFILE.gz.$ROTATE 
while [ $ROTATE -ge 1 ];
do
 i=$((ROTATE-1))
 if [ -f $OUTFILE.gz.$i ]; then 
  mv $OUTFILE.gz.$i $OUTFILE.gz.$ROTATE
 fi
  ROTATE=$i
done

test -f $OUTFILE.gz && mv $OUTFILE.gz $OUTFILE.gz.1

# MySQL dump

mysqldump -u $DBUSER --password=$DBPASS $DBNAME > $OUTFILE
gzip $OUTFILE

# Copy Server

if [ -w $BACKUPSRV ]; then
 FILEDATE=`date +%Y%m%d`
 cp $OUTFILE.gz $BACKUPSRV/$OUTFILE.$FILEDATE.gz 
fi

exit 0
# EOF

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2013-08-11 (日) 17:22:32 (3854d)