コンテンツのバックアップ
コンテンツをバックアップするスクリプトを書き上げてから、こんな単純な要求に対して複雑すぎる……感が半端ない。無駄かー
- (テストで)「/etc/」以下と、「/var/log/」以下をバックアップ
- バックアップは25日間保存
- 「/srv/contents-backup-datastore」以下に、「tar czf」でバックアップ
#!/bin/bash set -e set -u BACKUPDIR="/srv/contents-backup-datastore" # 25 days (24 * 25 = 600 hours) SAVED=600 TARGETS=("/etc" "/var/log") LOCKFILE="/var/run/contents_backup.lock" RETRIES=3 SLEEPTIME=15 # 1 hours (60 * 60 * 1 = 3600sec) LOCKTIMEOUT=3600 SELFID=`id | sed -e 's/uid=//' -e 's/(.*//'` if [ $SELFID -ne 0 ]; then # failed ERRMSG="You are not root, You cannot execute this script." echo $ERRMSG 1>&2 exit 100 fi # Require lockfile command in procmail package. lockfile -${SLEEPTIME} -r $RETRIES -l $LOCKTIMEOUT $LOCKFILE >/dev/null 2>&1 if [ $? -ne 0 ] ; then # lock failed. ERRMSG="Contents backup - Still running." echo $ERRMSG exit 1 fi TMPDIR=/tmp/contents-backup.$$ mkdir -p $TMPDIR trap "exit 1" HUP INT PIPE QUIT TERM trap "rm -f ${TMPDIR}/*.tar.gz; rmdir ${TMPDIR}; rm -f ${LOCKFILE}" EXIT TODAY=`date +%Y%m%d` for URI in ${TARGETS[@]} do FNAME=`echo $URI | sed 's!/!_!g'` BACKUP=${TMPDIR}/${FNAME}.${TODAY}.tar.gz tar czf $BACKUP $URI >/dev/null 2>&1 if [ $? -eq 0 ]; then echo "Successful in the preparation of the contents backup: ${FNAME}" else # warning $ERRMSG "Failed in the preparation of the contents backup: ${FNAME}" echo $ERRMSG 1>&2 fi done if [ ! -d $BACKUPDIR ]; then # failed ERRMSG="Contents of backup does not exist: ${BACKUPDIR}" echo $ERRMSG 1>&2 exit 200 fi /usr/sbin/tmpwatch $SAVED $BACKUPDIR if [ "$(ls -A ${TMPDIR}/*.tar.gz)" ]; then mv ${TMPDIR}/*.tar.gz ${BACKUPDIR}/ fi echo "Backup of the contents on server was completed."