Created
January 16, 2024 13:56
-
-
Save fsultan/4778a239922fb88724fd176a4f3a19c0 to your computer and use it in GitHub Desktop.
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
import os | |
import subprocess | |
import boto3 | |
def perform_backup(): | |
compress = os.getenv("COMPRESS", "true") | |
keep_failed = os.getenv("KEEP_FAILED", "true") | |
to_path = os.getenv("TO_PATH", "/path/to/backup") | |
backup_type = os.getenv("BACKUP_TYPE", "full") | |
neo4j_host = os.getenv("NEO4J_HOST", "localhost") | |
neo4j_port = os.getenv("NEO4J_PORT", "7687") | |
database = os.getenv("DATABASE", "") | |
min_size_bytes = int(os.getenv("MIN_SIZE_BYTES", "1073741824")) | |
command = f"neo4j-admin database backup --compress={compress} --keep-failed={keep_failed} --to-path={to_path} --type={backup_type} --from={neo4j_host}:{neo4j_port} {database}" | |
try: | |
subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True) | |
backup_size = os.path.getsize(to_path) | |
if backup_size < min_size_bytes: | |
raise ValueError(f"Backup size is less than {min_size_bytes} bytes") | |
except subprocess.CalledProcessError as e: | |
error_message = e.output.decode("utf-8") | |
sns_topic_arn = os.getenv("SNS_TOPIC_ARN") | |
if sns_topic_arn: | |
sns_client = boto3.client("sns") | |
sns_client.publish(TopicArn=sns_topic_arn, Message=error_message) | |
else: | |
print("Error occurred during backup:", error_message) | |
except ValueError as e: | |
error_message = str(e) | |
sns_topic_arn = os.getenv("SNS_TOPIC_ARN") | |
if sns_topic_arn: | |
sns_client = boto3.client("sns") | |
sns_client.publish(TopicArn=sns_topic_arn, Message=error_message) | |
else: | |
print("Error occurred during backup:", error_message) | |
def main(): | |
# Call the function to perform the backup | |
perform_backup() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment