#!/bin/bash
# Global variable
FILES=()
LOGDIR=$HOME/backup/docker-logs
NOW=$(date +%Y-%m-%d)

if [ $EUID -ne 0 ]; then
	echo -e "\e[1m\e[93mPlease run as must be a root!\033[0m"
	exit
fi


# check Log Directory
if [ ! -d ${LOGDIR} ]; then
	mkdir -p ${LOGDIR}
fi

NAMES=( `docker ps --format '{{.Names}}'` )
for name in ${NAMES[@]}
do
	folder=${LOGDIR}/${name}
	if [ ! -d ${folder} ]; then
		mkdir -p ${folder}
		echo "Created Directory : "${folder}
	else
		echo ${name}
		find ${LOGDIR} -type f -name "*.log" -mtime +7 -delete
	fi
	tmp=`docker inspect --format='{{.LogPath}}' ${name}`
	FILES+=("$tmp")	
done

for index in ${!FILES[@]}
do
	nice -n 19 bzip2 -ck ${FILES[$index]} > ${LOGDIR}/${NAMES[$index]}/${NOW}.bz2
	truncate -s 0 ${FILES[$index]}
done
