#!/bin/bash
#

BACKUP_BASE=/data/s3_backup
BUCKET_NAME=eDoc2
CEPH_HOST=192.168.105.121
BUCKETS=$(echo $BUCKET_NAME | tr ',' ' ')

trap "exit_process" 2 3 15
exit_process(){
    rm -f /var/lock/s3backup.lock
}

if [ -f /var/lock/s3backup.lock ];then
   echo "[$(date +"%F %T")] backup process is running" >> /tmp/edoc2_backup.log
   exit 1
else
   echo "[$(date +"%F %T")] backup start"  >> /tmp/edoc2_backup.log
   echo > /tmp/edoc2_backup_detail.log
   touch /var/lock/s3backup.lock
fi

mkfifo /tmp/fifo
exec 6<> /tmp/fifo
rm -f /tmp/fifo

for i in `seq 1 10`; do
    echo
done >&6

for bucket in $BUCKETS; do
    BACKUP_PATH=$BACKUP_BASE/$bucket
    if [ ! -d $BACKUP_PATH ]; then
       mkdir -p ${BACKUP_PATH}
    fi
    ssh root@$CEPH_HOST radosgw-admin bi list --bucket=$bucket | grep '\"name\":' | grep -v '_multipart_' | awk -F'[ "]+' '{print $4}' > /tmp/file.txt
    if [ $? -eq 0 ]; then
        while read file; do
            read -u6
            {
             s3cmd get s3://$bucket/$file $BACKUP_PATH/ --skip-existing > /dev/null 2>&1
             if [ $? -ne 0 ]; then
                echo "[$(date +"%F %T")] bucket:$bucket file: $file backup failed."
             else
                echo "[$(date +"%F %T")] bucket:$bucket file: $file backup success."
             fi
             echo >&6
            } &
        done < /tmp/file.txt >> /tmp/edoc2_backup_detail.log
        wait
    fi
    rm -f /tmp/file.txt
done

exec 6>&-
echo "[$(date +"%F %T")] backup end"  >> /tmp/edoc2_backup.log
rm -f /var/lock/s3backup.lock
