LoginSignup
4
5

More than 3 years have passed since last update.

【CentOS8】AWS CLIでS3にファイルをアップロード & バックアップ用のシェルスクリプトを作成する

Posted at

概要

本書ではCentOS8にAWS CLIをインストールし、S3にローカルのファイルをアップロードします。またAWS CLIとS3を利用して、バックアップ用シェルスクリプトを作成します。

0. 前提条件

  • AWS S3のバケットを作成していること。
  • IAMユーザーを作成し、AWS S3のIAMロールを適用していること。
  • 作成したIAMユーザーのアクセスキーID,シークレットアクセスキーを生成していること。
  • <注意>AWSを不正に利用されないために、アクセスキーIDとシークレットアクセスキーの取り扱いに十分気を付けてください。
  • 本書ではCentOS 8.0.1905 (Hyper-V上にインストール)を使用します。

1. AWS CLIのインストール

  1. Python3.6をインストールする。

     # dnf install python36 
    
  2. Python,pipがインストールされたことを確認する。

    # python3 -V
    Python 3.6.8
    # pip3 --version
    pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
    
  3. pipを使用してAWS CLIをインストールする。

    # pip3 install awscli
    

2. AWS CLIの初期設定

  1. AWS CLIのインストール確認のため、AWS CLIのバージョンを表示する。

    # aws --version
    aws-cli/1.16.298 Python/3.6.8 Linux/4.18.0-80.el8.x86_64 botocore/1.13.34
    
  2. AWS CLIに作成したIAMユーザーを紐づける。

    # aws configure
    AWS Access Key ID [None]: アクセスキーID
    AWS Secret Access Key [None]: シークレットアクセスキー
    Default region name [None]: リージョン(東京の場合はap-northeast-1)
    Default output format [None]: json
    
  3. AWS CLIにIAMユーザーが紐づいたことを確認する。

    # aws sts get-caller-identity
    {
        "UserId": "xxxxxxxxxxxxxxxxxxxxx",
        "Account": "xxxxxxxxxxxx",
        "Arn": "arn:aws:iam::xxxxxxxxxxxx:user/xxxxxxx"
    }
    

3. S3にローカルファイルをアップロード

  1. 作成したAWS S3のバケットを表示する。

    # aws s3 ls
    2019-12-01 19:44:02 バケット名
    
  2. ローカルのファイルを作成したバケットにコピーする。

    # aws s3 cp "/home/test/test_centos8.txt" s3://バケット名
    upload: ./test_centos8.txt to s3://バケット名/test_centos8.txt
    
  3. バケットにファイルがコピーされたことを確認する。

    # aws s3 ls s3://バケット名
    2019-12-08 21:19:37         21 test_centos8.txt
    

4. バックアップ用のシェルスクリプトを作成する。

作成例

〇シェルスクリプト

#!/bin/sh

# Specify the directory to back up.
backup_directory='/home/test'

# Specify the directory destination that compresses the temporarily specified backup source directory into tar.gz.
compresses_directory='/tmp'

# Specify the backup AWS S3 bucket.
backup_s3='s3://バケット名'

# Generate a date to perform backup.
backup_date=`date "+%Y%m%d"`

# Harden the local backup source directory into a tar.gz file
tar -zcvf "${compresses_directory}"/backup-"${backup_date}".tar.gz -P "${backup_directory}"

# Upload the backup source tar.gz to the AWS S3 bucket.
aws s3 mv "${compresses_directory}"/backup-"${backup_date}".tar.gz "${backup_s3}"

# Check the directory of the uploaded AWS S3 bucket
aws s3 ls "${backup_s3}"

exit 0

〇実行結果

/home/test/
/home/test/.mozilla/
/home/test/.mozilla/extensions/
/home/test/.mozilla/plugins/
/home/test/.bash_logout
/home/test/.bash_profile
/home/test/.bashrc
/home/test/testfile.txt
/home/test/test_centos8.txt
move: ./backup-20191208.tar.gz to s3://バケット名/backup-20191208.tar.gz
2019-12-08 22:09:12        629 backup-20191208.tar.gz

作成例の流れ

本書で作成しましたバックアップ用のシェルスクリプトの流れを下記に記載します。

  • ローカルのバックアップ対象ディレクトリを指定する。
backup_directory='/home/test'

一時的にtar.gzに固めるディレクトリを指定する。(S3にtar.gzファイルをアップロードする)

compresses_directory='/tmp'
  • バックアップ先のS3バケットを指定する。
backup_s3='s3://バケット名'
  • バックアップの日付(YYMMDD形式)を生成する。
backup_date=`date "+%Y%m%d"`
  • バックアップ対象のディレクトリをtar.gzファイルに固める。
tar -zcvf "${compresses_directory}"/backup-"${backup_date}".tar.gz -P "${backup_directory}"
  • バックアップ対象のtar.gzファイルをS3バケットに移動する。
aws s3 mv "${compresses_directory}"/backup-"${backup_date}".tar.gz "${backup_s3}"
  • バケットに保存されているファイルを表示する。
aws s3 ls "${backup_s3}"

最後に

AWS CLIを利用することでAWSマネジメントコンソールを開かずにS3にファイルをアップロードすることができます。またS3を利用したバックアップ用のシェルファイルを作成することで、AWSマネジメントコンソールを経由せずにバックアップ対象のファイルをS3にアップロードすることができます。

4
5
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
4
5