Skip to content

Instantly share code, notes, and snippets.

@ferdhie
Created August 29, 2016 17:11
Show Gist options
  • Save ferdhie/e8f8beb3cec0013abd309575267ea21d to your computer and use it in GitHub Desktop.
Save ferdhie/e8f8beb3cec0013abd309575267ea21d to your computer and use it in GitHub Desktop.
Backup file to s3
#!/bin/bash
file="file-to-backup.gz"
contentType="application/x-gzip"
bucket="bucketname"
region="ap-southeast-1"
resource="/${bucket}/${file}"
dateValue=`date -R`
stringToSign="PUT\n\n${contentType}\n${dateValue}\n${resource}"
s3Key="XXXXXXXXXXXXXXXXX"
s3Secret="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64`
echo "$stringToSign | $signature"
curl -i -X PUT -T "${file}" \
-H "Host: ${bucket}.s3.amazonaws.com" \
-H "Date: ${dateValue}" \
-H "Content-Type: ${contentType}" \
-H "Authorization: AWS ${s3Key}:${signature}" \
https://${bucket}.s3-${region}.amazonaws.com/${file}
rm -rf $file
cd $pwd
echo "Backup done\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment