|
| 1 | +""" |
| 2 | +This script checks if a backup file for the current date exists in a specified S3 bucket. |
| 3 | +If the backup file does not exist, a notification is sent to a Slack channel. |
| 4 | +
|
| 5 | +Expected file format in the S3 bucket: |
| 6 | +- The file should be in the folder 'db_backup/' with the following naming pattern: |
| 7 | + 'smartapi_YYYYMMDD.zip', where YYYYMMDD corresponds to the current date. |
| 8 | +
|
| 9 | +Required Environment Variables: |
| 10 | +- AWS_ACCESS_KEY_ID: The AWS access key ID to read the AWS s3 bucket. |
| 11 | +- AWS_SECRET_ACCESS_KEY: The AWS secret access key to read the AWS s3 bucket. |
| 12 | +- BACKUP_BUCKET_NAME: The name of the AWS S3 bucket where backups are stored. |
| 13 | +- S3_FOLDER: The folder path within the S3 bucket where backups are stored (e.g., 'db_backup/'). |
| 14 | +- AWS_REGION: The AWS region where the S3 bucket is located. |
| 15 | +- SLACK_CHANNEL: The Slack channel where notifications should be sent (e.g., '#observability-test'). |
| 16 | +- SLACK_WEBHOOK_URL: The Slack Webhook URL used to send the notification. |
| 17 | +
|
| 18 | +Functionality: |
| 19 | +1. The script uses the AWS SDK (boto3) to check for the existence of the backup file in the specified S3 bucket. |
| 20 | +2. If the file is found, it logs that no action is needed. |
| 21 | +3. If the file is not found, it sends a notification to the configured Slack channel. |
| 22 | +
|
| 23 | +Dependencies: |
| 24 | +- boto3: For interacting with AWS S3. |
| 25 | +- requests: For sending HTTP POST requests to Slack. |
| 26 | +
|
| 27 | +""" |
| 28 | + |
| 29 | +import boto3 |
| 30 | +import botocore |
| 31 | +import os |
| 32 | +import requests |
| 33 | + |
| 34 | +from datetime import datetime |
| 35 | + |
| 36 | + |
| 37 | +def send_slack_notification(message): |
| 38 | + |
| 39 | + print(f" └─ {message}") |
| 40 | + |
| 41 | + # Create the payload for Slack |
| 42 | + slack_data = { |
| 43 | + "channel": os.getenv("SLACK_CHANNEL"), |
| 44 | + "username": "SmartAPI", |
| 45 | + "icon_emoji": ":thumbsdown:", |
| 46 | + "text": message, |
| 47 | + } |
| 48 | + |
| 49 | + try: |
| 50 | + print(" └─ Sending Slack notification.") |
| 51 | + response = requests.post(os.getenv("SLACK_WEBHOOK_URL"), json=slack_data, timeout=10) |
| 52 | + if response.status_code == 200: |
| 53 | + print(" └─ Slack notification sent successfully.") |
| 54 | + else: |
| 55 | + print(f" └─ Failed to send message to Slack: {response.status_code}, {response.text}") |
| 56 | + except requests.exceptions.Timeout as e: |
| 57 | + print(" └─ Request timed out to Slack WebHook URL.") |
| 58 | + raise e |
| 59 | + except requests.exceptions.RequestException as e: |
| 60 | + print(f" └─ Failed to send Slack notification. Error: {str(e)}") |
| 61 | + raise e |
| 62 | + |
| 63 | + |
| 64 | +def check_backup_file(): |
| 65 | + |
| 66 | + # Create the expected file name |
| 67 | + today_date = datetime.today().strftime("%Y%m%d") |
| 68 | + expected_file = f"{os.getenv('S3_FOLDER')}smartapi_{today_date}.zip" |
| 69 | + |
| 70 | + # Create the S3 client |
| 71 | + s3_client = boto3.client("s3", region_name=os.getenv("AWS_REGION")) |
| 72 | + |
| 73 | + # Try to fetch the file metadata |
| 74 | + try: |
| 75 | + response = s3_client.head_object(Bucket=os.getenv("BACKUP_BUCKET_NAME"), Key=expected_file) |
| 76 | + print(f" └─ Backup file {expected_file} exists!") |
| 77 | + |
| 78 | + # Get the file size in bytes |
| 79 | + file_size = response['ContentLength'] |
| 80 | + |
| 81 | + # Check if the file is larger than 1MB |
| 82 | + if file_size > 1048576: # 1MB in bytes |
| 83 | + print(f" └─ Backup file is larger than 1MB! Size: {file_size} bytes.") |
| 84 | + print(" └─ Nothing to do!") |
| 85 | + else: |
| 86 | + message = f":alert: The backup file {expected_file} is smaller than 1MB!" |
| 87 | + send_slack_notification(message) |
| 88 | + |
| 89 | + except botocore.exceptions.ClientError as e: |
| 90 | + print(e) |
| 91 | + message = f":alert: The backup file {expected_file} was NOT created today!" |
| 92 | + send_slack_notification(message) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + check_backup_file() |
0 commit comments