14
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Linuxでのバックアップスクリプトのテンプレート

14
Last updated at Posted at 2017-05-30

はじめに

Linuxで何かのバックアップを取るシェルスクリプトをcronで回すというのはよくやると思います。

その際のシェルスクリプトってほとんど共通なので、自分のためにメモっておきます。

仕様

  • バックアップファイルはアーカイブされていること
  • ファイル名にはホスト名と日付を入れる
  • 世代管理する
  • ログはsyslogに書き出す

スクリプト

backup.sh
# !/bin/sh

hostname=`/bin/hostname`
date=`/bin/date +"%Y%m%d_%H%M"`
path=PATH/TO/BACKUP
filename="${path}/${hostname}_${date}.gz"
filekeepdays=7

echo "XXX backup started." | logger -t xxx_backup

# BACKUP_COMMAND 2>&1 | logger -t xxx_backup

for file in `find ${path}/ -mtime +${filekeepdays} -type f -name \*.gz`
do
    echo "deleted: " ${file} | logger -t xxx_backup
    /bin/rm ${file}
done

echo "XXX backup finished." | logger -t xxx_backup

exit 0

まとめ

ログをsyslogに吐き出すと、いちいち個別にログローテーションを考慮しなくて楽です。

14
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?