Copy s3 bucket contents to another1 min read

Hi Techrunnr, this post is regarding copy the contents of s3 bucket to another using aws cli, let’s say we have some contents in bucket1, we want to take daily basis backup to another bucket and we want to maintain a directory structure as month and day basis, so here we go

  • prerequisites:
    • AWS CLI installed
    • AWS Access key and secret key and your account should be able to access both the bucket
  • Login to aws account using below commands:
    aws configure
    AWS Access Key ID:<Enter your AWS access key>
    AWS Secret Access Key:<Enter secret key>
    Default region name:<Enter region name>
    Default output format:<Enter format(defualt JSON will take)>
  • We will create folder with today’s date in the script save the contents to day directory on our system, we copy this contents to another bucket
  • Now copy the source bucket contents to another bucket on monthly and daily basis (/month/day format), use below script
    #! /bin/bash
    set -e
    DATE=`date '+%Y-%m-%d'`
    DAY=`date '+%d'`
    MONTH=`date '+%m'`
    mkdir $DATE
    aws s3 sync s3://<source_bucket> $DATE/$DAY
    aws s3 sync $DATE s3://destination_bucket/$MONTH
  • Check copied or not
    aws s3 ls s3://<destination_bucket>/<month>/day/

We need your support!!
Other Amount:
Questions Answered
Articles Written
Overall Points

You may also like...

Leave a Reply