:>/dev/null

ラガードエンジニアの不撓不屈の精神/unlearning/go beyond

shで日時バックアップscript

shellで日時バックアップ処理を行う必要があった為、設定時メモ。

  • 以下構築条件
    • デイリーバックアップ
    • 圧縮必須
    • suffixが日時のみ処理
    • 開始・終了時刻が記載

本条件でshell scriptを書いた

#!/bin/sh

set -e

readonly SCRIPT_NAME=${0##*/}
readonly HISTORY=/root/logrotate/history.log

LOG_DATE_FORMAT(){
 echo `date '+%Y/%m/%d %H:%M:%S %a'`
}

echo  `LOG_DATE_FORMAT` `hostname -s` ${SCRIPT_NAME}[$$]: START >> ${HISTORY}


#: "テスト" && {
#    echo 今日の日付 … $(date +'%Y-%m-%d %H:%M')
#    echo 更新日が1日以上過去のファイルを探す: &&
#        find /data/td-agent/ -type f -mtime +0  -regextype posix-basic -regex ".*[0-9]\{4\}[0-9]\{1,2\}[0-9]\{1,2\}"
#        find /data/td-agent/ -not -name "*.gz" -type f -mtime +0  -regextype posix-basic -regex ".*[0-9]\{4\}[0-9]\{1,2\}[0-9]\{1,2\}.*"
#    echo 更新日が2日以上過去のファイルを探す: &&
#        find /data/td-agent/ -not -name "*.gz" -type f -mtime +1
#    echo 更新日が3日以上過去のファイルを探す: &&
#        find /data/td-agent/ -not -name "*.gz" -type f -mtime +2
#}


/bin/find /var/td-agent/store/ -not -name "*.gz" -type f -mtime +0 -regextype posix-basic -regex '.*[0-9]\{4\}[0-9]\{1,2\}[0-9]\{1,2\}' -exec gzip {} \;
/bin/find /var/td-agent/store/ -name "*.gz" -type f -mtime +1 -exec rm {} \;

echo  `LOG_DATE_FORMAT` `hostname -s` ${SCRIPT_NAME}[$$]: STOP >> ${HISTORY}

exit 0

後は、cronなりで起動設定すればバックアップされる。