#!/usr/bin/env bash
CMD_DATE='/bin/date'
CMD_COMPRESS='/bin/tar -pczf'
DATABASES='/etc/apt /etc/ssh'
export YEAR="$(${CMD_DATE} +%Y)"
export MONTH="$(${CMD_DATE} +%m)"
export DAY="$(${CMD_DATE} +%d)"
export TIME="$(${CMD_DATE} +%H.%M.%S)"
export TIMESTAMP="${YEAR}.${MONTH}.${DAY}.${TIME}"
# Define, check, create directories.
export BACKUP_DIR="${BACKUP_ROOTDIR}/files/${YEAR}/${MONTH}/${DAY}"
# Log file
export LOGFILE="${BACKUP_DIR}/${TIMESTAMP}.log"
[ ! -d ${BACKUP_DIR} ] && mkdir -p ${BACKUP_DIR} 2>/dev/null
# Initialize log file.
echo "* Starting backup: ${TIMESTAMP}." >${LOGFILE}
echo "* Backup directory: ${BACKUP_DIR}." >>${LOGFILE}
backup_files()
{
db="${1}"
FNAME="${db##*/}"
echo $FNAME >> ${LOGFILE}
# Compress
${CMD_COMPRESS} ${BACKUP_DIR}/${FNAME}-${TIMESTAMP}".tar.gz ${db} >>${LOGFILE}
}
# Backup.
echo "* Backing up databases ..." >> ${LOGFILE}
for db in ${DATABASES}; do
backup_files ${db} >>${LOGFILE}
done