#!/bin/bash
# filename: website_backup.sh

DATE=`date +%Y-%m-%d`
OLDDATE=`date +%Y-%m-%d -d '-5 days'`
WEBSITEOLDDATE=`date +%Y-%m-%d -d '-7 days'`
SITE_DIR=/data/site/

BACKDIR=/data/backup/site

if [ ! -d ${BACKDIR} ];then
	mkdir -p ${BACKDIR}
fi

if [ ! -d ${BACKDIR}/${DATE} ];then
       mkdir -p ${BACKDIR}/${DATE}
fi

if [ -d ${BACKDIR}/${OLDDATE} ];then
	rm -rf ${BACKDIR}/${OLDDATE}
fi

tar -cjf ${BACKDIR}/${DATE}/WEBSITE_${DATE}.bar.bz2 ${SITE_DIR};
echo "website has been backup successful";

HOST=113.98.190.24
FTP_USERNAME=www_backup
FTP_PASSWORD=www_backup

cd ${BACKDIR}/${DATE}

ftp -i -n -v << !
open ${HOST}
user ${FTP_USERNAME} ${FTP_PASSWORD}
bin
cd ${WEBSITEOLDDATE}
mdelete *
cd ..
rmdir ${WEBSITEOLDDATE}
mkdir ${DATE} 
cd ${DATE}
mput *
bye
!
