Skip to content

Instantly share code, notes, and snippets.

@halfnibble
Created January 18, 2021 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halfnibble/591597de43e0a118ebcab0f8b69dd480 to your computer and use it in GitHub Desktop.
Save halfnibble/591597de43e0a118ebcab0f8b69dd480 to your computer and use it in GitHub Desktop.
Minecraft Server Backups - Azure Storage Uploader Script.
# Inspired by https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python
import os
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
# Make sure you update this per server
CONTAINER_NAME = 'mcbackups1'
try:
print("Azure Blob storage v" + __version__ + " - Python quickstart sample")
# Quick start code goes here
except Exception as err:
print('Failed on initializaiton:')
print(err)
try:
connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
if not connect_str:
raise Exception('No Azure Storage Connection String in env.')
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
except Exception as err:
print('Failed to connect to Azure Storage:')
print(err)
# Do this once, then comment it out
# container_client = blob_service_client.create_container(CONTAINER_NAME)
local_path = '../backups'
backup_files = os.listdir(local_path)
for backup_file in backup_files:
upload_file_path = os.path.join(local_path, backup_file)
blob_client = blob_service_client.get_blob_client(container=CONTAINER_NAME, blob=backup_file)
print('Uploading to Azure Storage: ', upload_file_path)
with open(upload_file_path, 'rb') as data:
blob_client.upload_blob(data)
print('Now we delete: ', upload_file_path)
os.remove(upload_file_path)
print('Done with file: ', backup_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment