Skip to content

Instantly share code, notes, and snippets.

@youhey
Created September 20, 2012 02:59
Show Gist options
  • Save youhey/3753706 to your computer and use it in GitHub Desktop.
Save youhey/3753706 to your computer and use it in GitHub Desktop.
コンテンツのバックアップ
#!/bin/bash
set -e
set -u
BACKUPDIR="/srv/contents-backup-datastore"
# 25 days (24 * 25 = 600 hours)
SAVED=600
TARGETS=("/path/to/1st" "/path/to/2nd")
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."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment