#!/bin/bash
#

BACKUP_BASE=/data/s3_backup/eDoc2
BUCKET_NAME=eDoc2
 
if [ ! -d "$BACKUP_BASE" ]; then
    echo "$BACKUP_BASE: No such file or directory"
    exit 101
else
    ls -1 $BACKUP_BASE > /tmp/s3file.txt
fi

if [ ! -f "/tmp/s3file.txt" ]; then
   echo "s3file.txt not found !"
   exit 101
fi

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


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

echo "[$(date +"%F %T")] restore begin."
echo > /tmp/recovery.log

while read filename; do
     read -u6
     {
        s3cmd put $BACKUP_BASE/$filename  s3://$BUCKET_NAME --skip-existing > /dev/null 2>&1
        if [ $? -ne 0 ];then
          echo "[$(date +"%F %T")] $BACKUP_BASE/$filename recovery failed." 
        else
          echo "[$(date +"%F %T")] $BACKUP_BASE/$filename recovery success."
        fi
        echo >&6
      } &  
done < /tmp/s3file.txt   >> /tmp/recovery.log
wait

exec 6>&-
echo "[$(date +"%F %T")] restore end."
