Last active
April 1, 2017 06:58
-
-
Save sasrai/7e81656c791478dd09efba067ddadd2d to your computer and use it in GitHub Desktop.
Sponge鯖用自動バックアップ(WebAPIプラグイン利用)
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
#!/usr/bin/env bash | |
BACKUP_DIR=backups | |
LOG_FILE=${BACKUP_DIR}/backup.log | |
WEBAPI_PORT=8080 | |
API_KEY="foobar" | |
CURL_OPTS="-H Content-Type:application/json -H x-webapi-key:${API_KEY}" | |
BODY=$( | |
cat << _EOT_ | |
{ | |
"name": "Web-API", | |
"command": "save-all", | |
"waitLines": 0, | |
"waitTime": 0 | |
} | |
_EOT_ | |
) | |
# マイクラが起動しているかチェック | |
if [ -z $(netstat -tan | grep LISTEN | grep ${WEBAPI_PORT}) ]; then exit; fi | |
# カレントディレクトリをスクリプトファイルの場所に移動 | |
cd $(dirname $0) | |
# ログに実行日時を追記 | |
cat << _EOT_ >> ${LOG_FILE} | |
$(echo --- run script ---) | |
$(date) | |
_EOT_ | |
# マイクラにバックアップコマンドを実行させる | |
echo -n "Server response : " | |
curl ${CURL_OPTS} -d "${BODY}" http://localhost:${WEBAPI_PORT}/api/cmd >> ${LOG_FILE} | |
echo >> ${LOG_FILE} | |
# なんとなく待機 | |
sleep 10 | |
BACKUP_DAY=$(date "+%Y%m%d") | |
BACKUP_TIME=$(date "+%H%M%S") | |
function dir_backup() { | |
# バックアップディレクトリ | |
DIR=${BACKUP_DIR}/$1 | |
if [ ! -d ${DIR} ]; then mkdir -p ${DIR}; fi | |
# アーカイブ名 | |
FILENAME=${BACKUP_DAY} | |
ARCHIVE_PATTERN="[0-9]{8}" | |
if [ ! -z $3 ]; then FILENAME=${FILENAME}-${BACKUP_TIME}; ARCHIVE_PATTERN="${ARCHIVE_PATTERN}-[0-9]{6}"; fi | |
BACKUP_PATH=${DIR}/${FILENAME}.zip | |
if [ ! -f ${BACKUP_PATH} ]; then | |
echo "Create backup ($1) : ${BACKUP_PATH}" >> ${LOG_FILE} | |
zip ${BACKUP_PATH} $1 | |
fi | |
# 指定した個数以上のバックアップを削除 | |
find ${DIR} -type f -regextype posix-egrep -regex ".*/${ARCHIVE_PATTERN}\.zip" | sort -n | head -n -$2 | while read fn; do | |
echo "Remove backup ($1) : ${fn}" >> ${LOG_FILE} | |
rm ${fn} | |
done | |
} | |
dir_backup config 4 | |
dir_backup mods 4 | |
dir_backup world 7 | |
dir_backup world 12 true | |
echo "--- backup done ---" >> ${LOG_FILE} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment