#!/bin/bash

# The directory backed up is specified.
BASEDIR[0]=/var/www
BASEDIR[1]=/etc

# DATABASENAME
DATABASENAME[0]=test_data

# Specification of directory backup ahead
BACKUPDIR=/root/backup

#Preservation period
period=7

# mysql root password
mysql_root_pass=root_pass

#The tape is taken out on Friday(5).(0==Sunday)
yobi=5



# backup start
mkdir $BACKUPDIR/latest

echo "*** Start backup ***"
tar zcf $BACKUPDIR/latest/www_data.tar.gz ${BASEDIR[0]}
tar zcf $BACKUPDIR/latest/etc_data.tar.gz ${BASEDIR[1]}

for i in "${DATABASENAME[@]}"
do
mysqlhotcopy -u root -p $mysql_root_pass $i $BACKUPDIR/latest/
done


# Start TAPE BACKUP
echo "*** Start tape backup ***"
cd $BACKUPDIR
tar cvpf /dev/nst0 ./


cd $BACKUPDIR/latest
tar zcf $BACKUPDIR/`date "+%Y-%m-%d"`.tar.gz ./*


# Delete old backup file
echo "*** Deletion of file from old backup file ***"
cd $BACKUPDIR
rm -rf $BACKUPDIR/latest
find $BACKUPDIR/ -type f -mtime +"$period" -name "*.tar.gz" -exec \rm '{}' \;



# The tape is taken out on '$yobi'.
if [ `date '+%w'` -eq $yobi ]; then
mt -f /dev/nst0 eject;
fi

echo "... ALL Done"

