Skip to content

Instantly share code, notes, and snippets.

@tskrynnyk
Last active September 24, 2015 12:07
Show Gist options
  • Save tskrynnyk/746031 to your computer and use it in GitHub Desktop.
Save tskrynnyk/746031 to your computer and use it in GitHub Desktop.
asterisk-monitor-backup
#! /bin/sh
# asterisk-monitor-backup:
# dev at skrynnyk.pl
#
# echo '10 3 1 * * root test -x /usr/local/sbin/asterisk-monitor-backup && /usr/local/sbin/asterisk-monitor-backup' >/etc/cron.d/asterisk-monitor-backup
#
DELETE_FILE_AFTER_BACKUP=1
COMPRESS=tar # tar, zip
CONVERT=mp3 # mp3
VERBOSE=3 # 0 = errors, 1 = info, 2 = more info, ...
LOG2SYSLOG=0
LOG2FILE=1
CP=`which cp` || exit 125
TAR=`which tar` || exit 125
ZIP=`which zip` || exit 125
LAME=`which lame` || exit 125
LOGGER=`which logger` || exit 125
CP_PARAM='-apr'
TAR_PARAM='' # --total
ZIP_PARAM='-q'
LAME_PARAM='--quiet -m m --resample 16'
PROGNAME=`basename $0`
SRCDIR=/var/spool/asterisk/monitor
BACKUPDIR=/var/backup/monitor
LOCKFILE=/var/lock/$PROGNAME.lock
LOGFILE=/var/log/$PROGNAME.log
# ------------------------------------------------------------------------------
_echo() {
if [ $2 -le $VERBOSE ]; then
[ $LOG2SYSLOG -eq 1 ] && $LOGGER -t $PROGNAME -i $1
[ $LOG2FILE -eq 1 ] && echo -e "`date '+%F %X'` $PROGNAME[$$]:" $1
fi
}
_exit() {
_echo "$1" 0
_echo 'Bye' 0
exit $2
}
ami_root() {
if [ `id -u` -ne 0 ]; then
_echo 'Sorry, must be root.' 0
exit 1
fi
}
clean_up() {
_echo "Cleaning up..." 1
_echo "rm $TMPDIR" 2
rm -rf $TMPDIR
_echo "rm $LOCKFILE" 2
rm -f $LOCKFILE
}
convert2any() {
DIR=$1
_echo "Converting..." 1
case $CONVERT in
mp3)
ls $DIR | while read i; do
_echo "Converting $i to $CONVERT" 2
FNAME=`basename $i .wav`
$LAME $LAME_PARAM \
--tt $FNAME \
--tg speech \
$DIR/$FNAME.wav $DIR/$FNAME.mp3
[ "$?" -eq 0 ] && rm $DIR/$FNAME.wav
done
;;
*)
_echo "CONVERT: unknown value" 0
;;
esac
}
compress_dir() {
RET=255
cd $1/$2
_echo "Compressing $1/$2" 1
case $COMPRESS in
tar)
$TAR $TAR_PARAM -zcf ../$2.tar.gz ./ 2>&1
RET=$?
;;
zip)
$ZIP $ZIP_PARAM ../$2.zip .
RET=$?
;;
*)
_echo "COMPRESS: unknown value" 0
;;
esac
cd - 1>/dev/null
[ "$RET" -eq 0 ] && rm -r $1/$2
}
mvall2backupdir() {
_echo "Copying archives to $BACKUPDIR" 1
$CP $CP_PARAM $TMPDIR/* $BACKUPDIR
}
# ------------------------------------------------------------------------------
# MAIN
ami_root
{
_echo 'Welcome' 0
[ -f $LOCKFILE ] && _exit "$LOCKFILE exists, exiting..." 1
echo $$ >$LOCKFILE
TMPDIR=`mktemp -q -d $TESTDIR/tmp/$PROGNAME.XXXXXXXX`
[ -z $TMPDIR ] && _exit "Can't create temp directory, exiting..." 1
[ -d $BACKUPDIR ] || (mkdir -p $BACKUPDIR || _exit "Can't create backup directory, exiting..." 1)
cd $SRCDIR
YEAR="`date -d '-2 month' +%Y`"
MONTH="`date -d '-2 month' +%m`"
#for m in `seq -f '%02g' 11`; do
for m in $MONTH; do
MCDIR=$TMPDIR/$YEAR/$m
for d in `seq -f '%02g' 31`; do
_echo "$YEAR/$m/$d" 3
find . -type f -name "*$YEAR$m$d*.wav" \
| while read fn; do
[ -d $MCDIR/$d ] || mkdir -p $MCDIR/$d
_echo "Copying $fn to $MCDIR/$d" 2
$CP $CP_PARAM $fn $MCDIR/$d
if [ $DELETE_FILE_AFTER_BACKUP -eq 1 ]; then
_echo "Removing $fn" 2
rm $fn
fi
done
if [ -d $MCDIR/$d ]; then
[ -n "$CONVERT" ] && convert2any $MCDIR/$d
[ -n "$COMPRESS" ] && compress_dir $MCDIR $d
fi
done
done
[ -d "$MCDIR" ] && mvall2backupdir
clean_up
_echo 'Bye' 0
} >> $LOGFILE
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment