#!/bin/bash
##: Globals
BACKUPREPO="/home/dave/s3backup"
INITONLY=0
##: Args
if [ "$1" == "--initialize" ]; then
INITONLY=1
fi
##: Go to the backup folder
cd "$BACKUPREPO"
##: Get a list of buckets
buckets=(`aws s3 ls | awk '{ print $3}'`)
##: Create local folder for each if necessary
for i in "${buckets[@]}"
do
NEWFOLDER=0
[ ! -d "./$i" ] && NEWFOLDER=1 && mkdir "$i"
[ "$INITONLY" == "1" ] && [ "$NEWFOLDER" == "0" ] && echo "Bucket folder: [$i] exists."
[ "$INITONLY" == "1" ] && [ "$NEWFOLDER" == "1" ] && echo "Bucket folder: [$i] created."
done
##: Bail out here if just a folder build is wanted
if [ "$INITONLY" == "1" ]; then
exit 0
fi
##: Now sync them one at a time
for i in "${buckets[@]}"
do
if [ ! -f "./$i/s3b.ignore_backup" ]; then
echo "## ------------------------- Syncing [$i]..."
aws s3 sync "s3://$i" "./$i"
else
echo "## ------------------------- Skipping [$i]..."
fi
done
##: Leave
cd -
exit 0