Add log function for logging

(utilisez "git push" pour publier vos commits locaux)
This commit is contained in:
Victor LABORIE 2017-01-09 17:11:51 +01:00
parent f6e404bc1f
commit ea869a9c8e
3 changed files with 44 additions and 23 deletions

61
bkctld
View file

@ -98,9 +98,8 @@ sub_start() {
mkdir ${JAILDIR}/${jail}/dev/shm mkdir ${JAILDIR}/${jail}/dev/shm
mount -t devpts -o gid=4,mode=620 none ${JAILDIR}/${jail}/dev/pts mount -t devpts -o gid=4,mode=620 none ${JAILDIR}/${jail}/dev/pts
mount -t tmpfs none ${JAILDIR}/${jail}/dev/shm mount -t tmpfs none ${JAILDIR}/${jail}/dev/shm
chroot ${JAILDIR}/${jail} /usr/sbin/sshd -D & chroot ${JAILDIR}/${jail} /usr/sbin/sshd
#umount -R ${JAILDIR}/${jail}/dev echo "Jail $jail was started"
#umount ${JAILDIR}/${jail}/proc/
} }
sub_stop() { sub_stop() {
@ -116,6 +115,7 @@ sub_stop() {
kill $pid kill $pid
umount -R ${JAILDIR}/${jail}/dev umount -R ${JAILDIR}/${jail}/dev
umount ${JAILDIR}/${jail}/proc/ umount ${JAILDIR}/${jail}/proc/
echo "Jail $jail was stopped"
} }
sub_reload() { sub_reload() {
@ -123,6 +123,7 @@ sub_reload() {
if [ $(check_jail_on $jail) ]; then if [ $(check_jail_on $jail) ]; then
pkill -HUP -F ${JAILDIR}/${jail}/${SSHD_PID} pkill -HUP -F ${JAILDIR}/${jail}/${SSHD_PID}
fi fi
echo "Jail $jail was reloaded"
} }
sub_restart() { sub_restart() {
@ -170,21 +171,27 @@ sub_inc() {
inc="${INCDIR}/${jail}/${date}" inc="${INCDIR}/${jail}/${date}"
mkdir -p ${INCDIR}/${jail} mkdir -p ${INCDIR}/${jail}
if [ ! -d "${inc}" ]; then if [ ! -d "${inc}" ]; then
/bin/date +"%d-%m-%Y ; %H:%M" >> $LOGFILE echo "Make $date inc of $jail ..."
start=$(date +"%d-%m-%Y ; %H:%M")
if ( check_btrfs ); then if ( check_btrfs ); then
btrfs subvolume snapshot ${JAILDIR}/$jail $inc >> $LOGFILE btrfs subvolume snapshot ${JAILDIR}/$jail $inc
else else
echo -n "hard copy $jail begins at : " >> $LOGFILE echo -n "hard copy $jail begins at : "
cp -alx ${JAILDIR}/${jail}/ ${INCDIR}${jail}/$date cp -alx ${JAILDIR}/${jail}/ ${INCDIR}${jail}/$date
echo -n "hard copy $JAIL ends at : " >> $LOGFILE echo -n "hard copy $JAIL ends at : "
fi fi
/bin/date +"%d-%m-%Y ; %H:%M" >> $LOGFILE end=$(date +"%d-%m-%Y ; %H:%M")
echo "Inc $date of $jail created (Start at $start / End at $end"
else
echo "Inc $date of $jail already exist !" >&2
fi fi
done done
#| tee -a $LOGFILE | mail -s "[info] EvoBackup - create incs" $MYMAIL #| mail -s "[info] EvoBackup - create incs" $MAIL
} }
sub_rm() { sub_rm() {
empty="/tmp/bkctld-$$-$RANDOM/"
mkdir $empty
for jail in $( ls -1 $JAILDIR ); do for jail in $( ls -1 $JAILDIR ); do
incs=$(ls -1 ${INCDIR}/$jail) incs=$(ls -1 ${INCDIR}/$jail)
if [ -f ${CONFDIR}/$jail ]; then if [ -f ${CONFDIR}/$jail ]; then
@ -196,20 +203,22 @@ sub_rm() {
done < ${CONFDIR}/$jail done < ${CONFDIR}/$jail
) )
fi fi
#for j in $( ls ${INCDIR}/$jail ); do
for j in $( grep -v -f <(echo "${keep}") <(echo "${incs}") ); do for j in $( grep -v -f <(echo "${keep}") <(echo "${incs}") ); do
echo -n "Delete $jail/$j begins at : " >> $LOGFILE echo -n "Delete $jail/$j begins at : "
/bin/date +"%d-%m-%Y ; %H:%M" >> $LOGFILE /bin/date +"%d-%m-%Y ; %H:%M"
if ( check_btrfs ); then if ( check_btrfs ); then
[ -n "$j" ] && btrfs subvolume delete ${INCDIR}/$jail/$j && touch /tmp/evobackup-rm.txt btrfs subvolume delete ${INCDIR}/$jail/$j && touch /tmp/evobackup-rm.txt
else else
cd ${INCDIR}/$jail cd ${INCDIR}/$jail
[ -n "$j" ] && rsync -a --delete /dev/null $j* rsync -a --delete $empty $j*
[ -n "$j" ] && rmdir $j* && touch /tmp/evobackup-rm.txt rmdir $j* && touch /tmp/evobackup-rm.txt
fi fi
echo -n "Delete ${jail}/$j ends at : " >> $LOGFILE echo -n "Delete ${jail}/$j ends at : "
/bin/date +"%d-%m-%Y ; %H:%M" >> $LOGFILE /bin/date +"%d-%m-%Y ; %H:%M"
done done
done #| tee -a $LOGFILE | ( [ -e "/tmp/evobackup-rm.txt" ] && mail -s "[info] EvoBackup - purge incs" $MYMAIL && rm /tmp/evobackup-rm.txt ) done #| ( [ -e "/tmp/evobackup-rm.txt" ] && mail -s "[info] EvoBackup - purge incs" $MAIL && rm /tmp/evobackup-rm.txt )
rmdir $empty
} }
mk_jail() { mk_jail() {
@ -375,6 +384,18 @@ set_firewall() {
done done
} }
log() {
subcommand=$1
logfile="${LOG_DIR}/bkctld.log"
shift
tty -s
if [ $? -eq 0 ]; then
sub_${subcommand} $@ 2>&1 | tee -a $logfile
else
sub_${subcommand} $@ 2>&1 >> $logfile
fi
}
main() { main() {
if [ $(id -u) != 0 ]; then if [ $(id -u) != 0 ]; then
echo "Error, you need to be root to run $0 !" >&2 echo "Error, you need to be root to run $0 !" >&2
@ -395,12 +416,12 @@ main() {
usage usage
;; ;;
"inc" | "rm") "inc" | "rm")
sub_${subcommand} $jail log ${subcommand}
;; ;;
"init") "init")
if [[ -n "${jail}" ]]; then if [[ -n "${jail}" ]]; then
if ( ! $(check_jail $jail) ); then if ( ! $(check_jail $jail) ); then
sub_${subcommand} $jail log ${subcommand} $jail
fi fi
fi fi
;; ;;
@ -423,7 +444,7 @@ main() {
done done
else else
if ( $(check_jail $jail) ); then if ( $(check_jail $jail) ); then
sub_${subcommand} $jail log ${subcommand} $jail
fi fi
fi fi
fi fi

View file

@ -12,7 +12,7 @@ if [ $? == 0 ]; then
mount -o remount,rw /usr mount -o remount,rw /usr
fi fi
mkdir -m 0755 -p $TPLDIR mkdir -m 0755 -p $TPLDIR $LOG_DIR
cp -v $dir/tpl/* $TPLDIR cp -v $dir/tpl/* $TPLDIR
install -m 0755 -v $dir/bkctld /usr/local/sbin/ install -m 0755 -v $dir/bkctld /usr/local/sbin/

View file

@ -5,8 +5,8 @@ CONFDIR='/etc/evobackup/'
JAILDIR='/backup/jails/' JAILDIR='/backup/jails/'
INCDIR='/backup/incs/' INCDIR='/backup/incs/'
TPLDIR='/usr/local/share/evobackup/' TPLDIR='/usr/local/share/evobackup/'
LOGFILE='/var/log/evobackup.log' LOG_DIR='/var/log/bkctld/'
MYMAIL='jdoe@example.com' MAIL='jdoe@example.com'
SSHD_PID='/var/run/sshd.pid' SSHD_PID='/var/run/sshd.pid'
SSHD_CONFIG='/etc/ssh/sshd_config' SSHD_CONFIG='/etc/ssh/sshd_config'
AUTHORIZED_KEYS='/root/.ssh/authorized_keys' AUTHORIZED_KEYS='/root/.ssh/authorized_keys'