Skip to content

Instantly share code, notes, and snippets.

@boltronics
Created August 16, 2012 06:55
Show Gist options
  • Save boltronics/3367539 to your computer and use it in GitHub Desktop.
Save boltronics/3367539 to your computer and use it in GitHub Desktop.
Makes a copy of all buckets in an S3 account, with the prefix being the destination region name (eg. "ap-southeast-1.").
#!/bin/bash
# Adam Bolte <abolte@systemsaviour.com>
declare -r dstRegion="ap-southeast-1"
declare -r bucketList="$(s3cmd ls | sed -e 's/.*\ \ s3:\/\/\(.*\)$/\1/' | \
grep -E -v "^(${dstRegion}|s3hub|rightscale)" | xargs echo)"
declare -ri bucketTotal="$(echo ${bucketList} | wc -w)"
declare -r bucketBackupsList="$(s3cmd ls | \
sed -e 's/.*\ \ s3:\/\/\(.*\)$/\1/' | \
grep "^${dstRegion}\." | xargs echo)"
declare -i bucketCounter=0
declare bucket
function bucketExists()
{
declare bucket
for bucket in ${bucketBackupsList}
do
[ "${bucket}" = "${1}" ] && return 0
done
return 1
}
for bucket in ${bucketList}
do
bucketCounter=$((++bucketCounter))
echo "${bucketCounter}/${bucketTotal} - backing up s3://${bucket}/..."
if ! bucketExists "${dstRegion}.${bucket}"
then
echo s3cmd --bucket-location=${dstRegion} mb s3://${dstRegion}.${bucket}
fi
echo s3cmd --preserve --recursive --force --delete-removed \
--parallel --workers=50 \
--dry-run cp s3://${bucket}/ s3://${dstRegion}.${bucket}/
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment