0
0

AWS S3 syncコマンドの説明

Posted at

はじめに

S3バケットとローカルの間、ファイルをdownload・uploadする場合、aws s3 cpコマンドがよく利用されると思います。実は、aws s3 syncコマンドを利用してもよいのです。機能面を見ると、この2つのコマンドは、すごく似ている感じですね。そもそも、どんな違いがあるのでしょうか。

違い所

  • cpコマンド

対象ファイルの存在と変更に関わらず、単純に条件に合致した対象ファイルをコピーすること。(無駄なコピー操作が発生するかもしれない)

  • syncコマンド

同期化だから、対象ファイルをコピーする前に、S3バケットとローカルの間、ファイルの差分を行います。コピー先に存在する、かつ、変更がないファイルに対して、skipして、コピーしないです。コピー操作を最小限に抑えるため、コスト削減ができます。

使い方のお勧め

一回目のファイルコピー(コピー先に何のファイルも存在していない)は、cpコマンドを利用する(syncコマンドは、余計な同期化チェックにコストがかかるため、避けたいと思います。)

二回目以降は、syncコマンドを利用する。(同期化だから、スピードとコストの両方ともお得ですね。)

Example

syncコマンドは、デフォルトで、全てのファイル(.bashrcファイルも含む)をs3バケットにuploadします。回避したい場合、--exclude ".*"を追加してよい。

[cloudshell-user@ip-10-132-89-169 ~]$ pwd
/home/cloudshell-user
[cloudshell-user@ip-10-132-89-169 ~]$ echo "test" > test.txt
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ ls -l
total 4
-rw-r--r--. 1 cloudshell-user cloudshell-user 5 Jul 18 02:49 test.txt
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ aws s3 ls yunfan-front-backup
2021-03-07 11:42:13    2167775 yunfan.zip
2021-03-13 07:12:19   10998345 yunfan20210313.zip
2021-03-16 14:41:00   11012289 yunfan20210316.zip
[cloudshell-user@ip-10-132-89-169 ~]$ aws s3 sync . s3://yunfan-front-backup
upload: ./.bash_logout to s3://yunfan-front-backup/.bash_logout                     
upload: ./.zshrc to s3://yunfan-front-backup/.zshrc                                 
upload: ./.bash_profile to s3://yunfan-front-backup/.bash_profile
upload: ./.bashrc to s3://yunfan-front-backup/.bashrc           
upload: .config/powershell/Microsoft.PowerShell_profile.ps1 to s3://yunfan-front-backup/.config/powershell/Microsoft.PowerShell_profile.ps1
upload: ./test.txt to s3://yunfan-front-backup/test.txt        
upload: ./.zprofile to s3://yunfan-front-backup/.zprofile      
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ aws s3 sync . s3://yunfan-front-backup
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ aws s3 sync . s3://yunfan-front-backup
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ echo "test001" > test.txt 
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ aws s3 sync . s3://yunfan-front-backup
upload: ./test.txt to s3://yunfan-front-backup/test.txt         
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ aws s3 ls yunfan-front-backup
                           PRE .config/
2024-07-18 02:52:32         18 .bash_logout
2024-07-18 02:52:32        141 .bash_profile
2024-07-18 02:52:32        539 .bashrc
2024-07-18 02:52:32        299 .zprofile
2024-07-18 02:52:32        777 .zshrc
2024-07-18 02:53:22          8 test.txt
2021-03-07 11:42:13    2167775 yunfan.zip
2021-03-13 07:12:19   10998345 yunfan20210313.zip
2021-03-16 14:41:00   11012289 yunfan20210316.zip
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ 
##コンソール画面から、.bashrcファイルなどを手動で削除しておく##
[cloudshell-user@ip-10-132-89-169 ~]$ aws s3 ls yunfan-front-backup
2024-07-18 02:53:22          8 test.txt
2021-03-07 11:42:13    2167775 yunfan.zip
2021-03-13 07:12:19   10998345 yunfan20210313.zip
2021-03-16 14:41:00   11012289 yunfan20210316.zip
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ aws s3 sync . s3://yunfan-front-backup --exclude ".*"                                                                                        
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ aws s3 ls yunfan-front-backup                                                                                                                
2024-07-18 02:53:22          8 test.txt
2021-03-07 11:42:13    2167775 yunfan.zip
2021-03-13 07:12:19   10998345 yunfan20210313.zip
2021-03-16 14:41:00   11012289 yunfan20210316.zip
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ echo "xxxx" > test.txt 
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ aws s3 sync . s3://yunfan-front-backup --exclude ".*"
upload: ./test.txt to s3://yunfan-front-backup/test.txt         
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ aws s3 sync . s3://yunfan-front-backup --exclude ".*"
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ 
[cloudshell-user@ip-10-132-89-169 ~]$ aws s3 ls yunfan-front-backup                                                                                                                
2024-07-18 03:01:08          5 test.txt
2021-03-07 11:42:13    2167775 yunfan.zip
2021-03-13 07:12:19   10998345 yunfan20210313.zip
2021-03-16 14:41:00   11012289 yunfan20210316.zip
[cloudshell-user@ip-10-132-89-169 ~]$ 

終わりです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0