Shell Script

Backup webhome

1. 개요

Backup web home directory

 

2. Source Code

#!/bin/bash
 
############################################
# Backup - webhome directory               #
# Date  : 2017.12.12.                      #
# Maker : L.T                              #
############################################
 
# Set variable
TODAY=`date +'%Y%m%d'`
REMOVEDAY=`date +'%Y%m%d' -d '3 days ago'`
 
# Check log
LOGFILE="/var/log/backup_webhome.log"
if [ ! -f $LOGFILE ]; then
        sudo touch $LOGFILE
        sudo chown ubuntu.ubuntu $LOGFILE
fi
 
function wrlog(){
        NOWTIME=`date +'[%F %T]'`
        MSG="${NOWTIME} $1"
        echo $MSG
        echo $MSG >> $LOGFILE
}
 
wrlog "Backup webhome directory"
 
### Get webhome directory
for BAKDIR in $(ls -l /webhome | grep drwx | awk '{print $9}')
do
 
### Remove Past file
if [ -f "/backup/${BAKDIR}_${REMOVEDAY}.tar.gz" ] ; then
        wrlog "Delete past file : /backup/${BAKDIR}_${REMOVEDAY}.tar.gz"
        wrlog "`ls -al /backup/${BAKDIR}_${REMOVEDAY}.tar.gz`"
 
        sudo /bin/rm -f /backup/${BAKDIR}_${REMOVEDAY}.tar.gz
        wrlog "Remove Complete!"
fi
 
### Backup Today file
if [ ! -f "/backup/${BAKDIR}_${TODAY}.tar.gz" ] ; then
        wrlog "Backup Start : ${BAKDIR}"
        cd /webhome
        sudo /bin/tar zcf /backup/${BAKDIR}_${TODAY}.tar.gz ${BAKDIR}
        wrlog "Backup End : ${BAKDIR}"
fi
 
done
 
wrlog "=============================="
 
/bin/ls -al /backup

 

Back To Top