-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create github action to check backup.
- Loading branch information
1 parent
e4fc584
commit 43d2987
Showing
1 changed file
with
41 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,36 +26,45 @@ jobs: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
AWS_REGION: ${{ secrets.AWS_REGION }} | ||
BACKUP_BUCKET_NAME: ${{ secrets.BACKUP_BUCKET_NAME }} | ||
S3_FOLDER: 'db_backup/' | ||
BACKUP_BUCKET_NAME: "${{ secrets.BACKUP_BUCKET_NAME }}" | ||
S3_FOLDER: "db_backup/" | ||
SLACK_CHANNEL: "#observability-test" | ||
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} | ||
run: | | ||
import boto3 | ||
from datetime import datetime | ||
# Create the expected file name | ||
today_date = datetime.utcnow().strftime('%Y%m%d') | ||
expected_file = f"{os.getenv('S3_FOLDER')}smartapi_{today_date}.zip" | ||
# Create the S3 client | ||
s3_client = boto3.client('s3', region_name=os.getenv('AWS_REGION')) | ||
# Try to fetch the file metadata | ||
try: | ||
s3_client.head_object(Bucket=os.getenv('BACKUP_BUCKET_NAME'), Key=expected_file) | ||
print(f"Backup file {expected_file} exists.") | ||
except s3_client.exceptions.ClientError: | ||
print(f"Backup file {expected_file} does NOT exist.") | ||
exit(1) # Return an error code if the file does not exist | ||
- name: Send notification to Slack if backup file does not exist | ||
if: failure() # Only runs if the previous step failed (file not found) | ||
uses: slackapi/[email protected] | ||
with: | ||
payload: | | ||
{ | ||
"channel": "#observability-test", # Replace with your Slack channel | ||
"username": "Backup Check Bot", | ||
"text": "🚨 The expected backup file for `smartapi_{ { steps.check_s3_backup.outputs.expected_file } }` was not found in the S3 bucket!" | ||
} | ||
env: | ||
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} | ||
import boto3 | ||
import os | ||
import requests | ||
from datetime import datetime | ||
# Create the expected file name | ||
today_date = datetime.utcnow().strftime("%Y%m%d") | ||
expected_file = f"{os.getenv('S3_FOLDER')}smartapi_{today_date}.zip" | ||
# Create the S3 client | ||
s3_client = boto3.client("s3", region_name=os.getenv("AWS_REGION")) | ||
# Try to fetch the file metadata | ||
try: | ||
s3_client.head_object(Bucket=os.getenv("BACKUP_BUCKET_NAME"), Key=expected_file) | ||
print(f"Backup file {expected_file} exists, no action needed") | ||
except s3_client.exceptions.ClientError: | ||
print(f"Backup file {expected_file} does NOT exist.") | ||
# Create the payload for Slack | ||
slack_data = { | ||
"channel": os.getenv("SLACK_CHANNEL"), | ||
"username": os.getenv("APPLICATION_NAME"), | ||
"icon_emoji": ":thumbsdown:", | ||
"text": ":alert: Backup file {expected_file} does NOT exist.", | ||
} | ||
try: | ||
response = requests.post(os.getenv("SLACK_WEBHOOK_URL"), json=slack_data, timeout=10) | ||
if response.status_code == 200: | ||
print(" └─ Slack notification sent successfully.") | ||
else: | ||
print(f" └─ Failed to send message to Slack: {response.status_code}, {response.text}") | ||
except requests.exceptions.Timeout: | ||
print(" └─ Request timed out to Slack WebHook URL.") | ||
except requests.exceptions.RequestException as e: | ||
print(f" └─ Failed to send Slack notification. Error: {str(e)}") |