#!/bin/bash

BACKUP_BASE=/opt
BUCKET_NAME=eDoc2

BUCKETS=$(echo $BUCKET_NAME | tr ',' ' ')

if [ $(ps aux | grep -v grep | grep -c s3cmd) -gt 1 ];then
   echo "backup process is running" >> /tmp/edoc2_backup.log
   exit 1
fi


for bucket in $BUCKETS; do

   BACKUP_PATH=$BACKUP_BASE/$bucket

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

   /usr/bin/s3cmd sync s3://$bucket/ ${BACKUP_PATH}/ 
   if [ "$?" == 0 ];then
       echo "$(date '+%Y-%m-%d %H:%M:%S') Bucket $bucket Document backup success." >> /tmp/edoc2_backup.log
   else
       echo "$(date '+%Y-%m-%d %H:%M:%S') Bucket $bucket Document backup faile." >> /tmp/edoc2_backup.log
   fi
done
