Created
June 27, 2017 03:11
-
-
Save tkota0726/1ad624f766d493124f84d055b42809ca to your computer and use it in GitHub Desktop.
sa-ba-
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/bash | |
#!/bin/bash | |
datestr=`date +%Y%m%d-%H%M%S` | |
STATUS=0 | |
# 関数: scpを行う | |
# $1 コピー元ファイル名(フルパス) | |
# $2 コピー先ファイル名 | |
function scpfile(){ | |
eval "scp -i /Users/kota/.ssh/kota-test-2.pem [username]@[ip]:$1 ./$2" | |
if [ $? -ne 0 ] # [ "$a" -ne "$b" ] $aと$bが違う場合TRUEを返します。$? 直前実行したコマンドの終了値(0は成功、1は失敗) | |
then | |
echo "[ERROR]scp error. file=$1" | |
STATUS=1 | |
fi | |
return | |
} | |
# 関数: ファイルの削除を行う。 | |
# $1: 削除対象のファイルパス(前方一致) | |
# 10世代分のファイルを残し、それより古いファイルは削除します。 | |
function deletefile(){ | |
CNT=0 | |
for file in `ls -1t ${1}*` # 更新日付が新しい順に 削除ファイルのリストを作成。for文で回す。 | |
do | |
CNT=$((CNT+1)) | |
if [ ${CNT} -le 10 ] # 10世代より過去のバックアップは削除する | |
then | |
continue | |
fi | |
eval "rm ${file}" | |
done | |
return | |
} | |
# MAIN処理 | |
# 指定ファイルを | |
# /backup/[filename]_YYYYMMDD-HHMMSS.tar.gzとして保存します。 | |
eval "tar czf /backup/wordpressfile_${datestr}.tar.gz [path]" | |
if [ $? -ne 0 ] | |
then | |
echo "[ERROR]tar error." | |
STATUS=1 | |
fi | |
# 10世代より前のファイルを削除します。 | |
deletefile "/backup/[jpgfile]" # jpgファイルの削除 | |
# 最新のバックアップファイルをローカルへコピー | |
scpfile /backup/[filename]_${datestr}.tar.gz [filename].tar.gz | |
# 実行結果をメールで通知 | |
if [ ${STATUS} -ne 0 ] | |
then | |
SUBJECT="[ERROR]cms backup report" | |
else | |
SUBJECT="[SUCCESS]cms backup report" | |
fi | |
echo "" | mailx -s "${SUBJECT}" "[mail@address]" | |
exit ${STATUS} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment