Quick tip - S3 sync
Tiago Rosa da costa
Posted on July 18, 2021
What’s s3 sync? The s3 sync is one command of the aws-cli which allows synchronize files with a bucket. The complete command is: aws s3 sync ./ s3://bucket_name --delete
Explain in details this command:
- The part aws s3 sync indicate to aws-cli that you need synchronize something with s3
- The part ./ get all files in current directory to send to s3
- The part s3://bucket_name change bucket_name per name the bucket that exists in your aws account. This bucket receives files synchronized.
- The part --delete is optional. When mentioned this parameter indicates that after synchronization occurs delete files in the local machine.
I believe that following situations s3 sync can help you:
- Your application running in machine and write log files in disk, but disk is full and you can’t delete log files of your application.
- You configured your database and made backups of the database because you didn't use management database service You deploy the react application in s3.
- You execute build and upload files in s3 where s3 hosted frontend application.
Let’s talk about the situations above and explain how s3 sync can help you.
First situation: your application running in machine and write log files in disk, but disk is full and you can’t delete log files of your application
In this scenery your application writes log files in disk, ok. But it is necessary to remove these files because the machine disk is full and can’t delete log files. One solution to resolve this problem:
aws s3 sync ./ s3://bucket_name --delete
Explain command above:
- Firstly install aws-cli and configure credentials.
- After execute command above where get files in ./ , send to bucket_name and delete local files.
- To automate is necessary only create cron task in machine to execute command above based your needs.
Second situation: you configured your database and made backups of the database because you didn't use management database service
In this scenery you need to generate backup daily from the database and store one place to another moment access, case needs. The solution to resolve this problem:
mysqldump -u username -p database_name > ./backup_$(date +"%m-%d-%Y").sql
aws s3 sync ./ s3://bucket_name --delete
Explain commands above:
- Firstly install aws-cli and configure credentials.
- After use first command above in this example to execute backup mysql database.
- After use second command to get files in ./ , send to bucket_name and delete local files to prevent machine disk full.
- To automate is necessary only to create a cron task in the machine to execute the commands above.
Third situation: you deploy the react application in s3. You execute build and upload files in s3 where s3 hosted frontend application
You deploy a manually react application in s3 where s3 hosts a frontend application. The solution to resolve this problem deploy manually:
npm run build
cd ./build
aws s3 sync ./ s3://bucket_name
Explain commands above:
- Firstly install aws-cli and configure credentials
- After use the first command to execute build react application that generates build directory when contains files to deploy.
- After use second command to access build directory
- After use command to get files in ./ and send to bucket_name
- To automate it for you, you can use github actions to execute pipeline. When sending code to the branch master in the github repository execute the github action pipeline with command above. To github actions pipeline use this https://github.com/aws-actions/configure-aws-credentials where setting aws-cli and configured credentials in your aws-cli and after execute commands above
Posted on July 18, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.