github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/scripts/s3/put-bucket-inventory.sh (about)

     1  #!/bin/bash
     2  
     3  FREQ="Weekly"
     4  INV_JSON="inv-config.json"
     5  PREFIX=".inventory"
     6  
     7  while getopts n:p:b:f: flag
     8  do
     9  	case "$flag" in
    10  		n) ID=${OPTARG};;
    11  		p) PREFIX=${OPTARG};;
    12  		b) BUCKET=${OPTARG};;
    13  		f) FREQ=${OPTARG};;
    14  	esac
    15  done
    16  
    17  print_usage() {
    18  	echo "Usage:"
    19  	echo "./put-bucket-inventory -b BUCKET_NAME [-n INVENTORY_NAME] [-p PREFIX] [-f FREQUENCY]"
    20  	echo
    21  	echo "The script enables periodical inventory generation for a bucket. The inventory is saved to a bucket into PREFIX directory daily or weekly depending on the command-line options. Note: it may take up to 48 hours to generate the first inventory"
    22  	echo
    23  	echo "  BUCKET_NAME - AWS bucket name for which inventory will be enabled"
    24  	echo "  INVENTORY_NAME - is AWS inventory ID - arbitrary string that must be unique for a new inventory". Default value is ".inventory"
    25  	echo "  PREFIX - all inventory objects will be placed under s3://BUCKET_NAME/PREFIX/ directory". Default value is the bucket name
    26  	echo "  FREQUENCY - how often inventory is updated. Can be either 'Daily' or 'Weekly'. Default value is 'Weekly'"
    27  }
    28  
    29  RED='\033[0;31m'
    30  NC='\033[0m'
    31  
    32  if [[ -z "${BUCKET}" ]]; then
    33  	printf "${RED}Error${NC}: bucket name is not defined.\n"
    34  	print_usage
    35  	exit 1
    36  fi
    37  
    38  if [[ -z "${ID}" ]]; then
    39  	ID="${BUCKET}"
    40  fi
    41  
    42  if [[ -z "${PREFIX}" ]]; then
    43  	printf "${RED}Error${NC}: prefix is not defined.\n"
    44  	print_usage
    45  	exit 1
    46  fi
    47  
    48  if [[ "${FREQ}" != "Daily" && "${FREQ}" != "Weekly" ]]; then
    49  	printf "${RED}Error${NC}: invalid value for frequency. Must be one of: ['Daily', 'Weekly']\n"
    50  	print_usage
    51  	exit 1
    52  fi
    53  
    54  cat > "${INV_JSON}" <<EOL
    55  {
    56  	"Destination": {
    57  		"S3BucketDestination": {
    58  			"Bucket": "arn:aws:s3:::${BUCKET}",
    59  			"Format": "CSV",
    60  			"Prefix": "${PREFIX}"
    61  		}
    62  	},
    63  	"IsEnabled": true,
    64  	"Id": "${ID}",
    65  	"IncludedObjectVersions": "Current",
    66  	"OptionalFields": ["Size","ETag"],
    67  	"Schedule":{
    68  		"Frequency": "${FREQ}"
    69  	}
    70  }
    71  EOL
    72  
    73  aws s3api put-bucket-inventory-configuration --bucket ${BUCKET} --id ${ID} --inventory-configuration "file://${INV_JSON}"