Data Backup and Recovery with Azure Blob Storage (original) (raw)

Last Updated : 28 Nov, 2023

Microsoft has various storage types, one of which is Azure Blob Storage. The data stored in Blob Storage can be of various formats including but not restricted to pictures and videos. Blob storage is suited to store unstructured data i.e. the data that doesn't have a specific structure such as text or binary, on Microsoft's data storage platform.

**Azure Backup

**Azure Backup offers a cloud-based backup solution that is easy to use, secure, affordable, and suitable for safeguarding application-critical data stored in **Azure Blobs for your organization.

**Azure Recovery

Azure recovery is the restoration of data and services within Microsoft Azure to a previous state. It includes data, applications, and virtual machine recovery to a specific point or from backups. Users can restore data and services from operational or vaulted backups, with operational backups offering point-in-time restoration and vaulted backups following a set schedule.

Types of Backup for Blobs

**Continuous backups
To prevent unintentional deletion or corruption of your block blobs, you can set up operational backup, a managed local data protection solution. Instead of being moved to the backup vault, the data is kept locally within the source storage account. You don't need to create a backup schedule.

**Periodic backups
Vaulted backup is a managed offsite data protection service that you may set up to prevent your storage account and blobs from being maliciously or unintentionally deleted. The backup information is duplicated, preserved within the backup vault as dictated by the retention rules in the policy, and can be accessed as per the timetable and recurrence you set in the backup policy when utilizing vaulted backups.

Benefits of Azure Blob Backup

Benefits of Azure Blob Recovery

Steps for Performing Data Backup and Recovery Using Azure Blob Storage

**Step 1: Creating a Storage Account in Azure.

Click on Create a Resource button

 Click on Create then select Storage Account.

We have selected **LRS(Locally-redundant).

Create basic storage account

Encryption

Review

**Step 2: Creating a **Container.

Storage created

New container

**Step 3: Installing the required libraries. Here we only need to install **azure-storage-blob library.

**Note: Install/Upgrade **pip for further using it to install **azure-storage-library.

  1. Checking the version,
    For Python 3, run the command: **pip3 --version
    For Python 2, run the command: **pip --version
  2. If **pip is not installed, install using given commands:
    **python3 -m ensurepip --upgrade
    **OR
    **python -m ensurepip --upgrade

**pip install azure-storage-blob

**OR

**pip3 install azure-storage-blob

Accessing through CLI

Succesfully installed azure core

**Note: For information about the library we have installed here and/or for related commands, please refer Microsoft Azure Blob Storage docs.

**Step 4: Creating the **backup.py file. We are using **Python for writing the backup and recovery scripts.

**nano backup.py

from azure.storage.blob import BlobServiceClient import os

storage_account_key = '[your_storage_account_key]' storage_account_name = '[your_storage_account_name]' connection_string = '[your_connection_string]' container_name = '[your_container_name]'

def uploadToBlobStorage(file_path, file_name): blob_service_client = BlobServiceClient.from_connection_string(connection_string) blob_client = blob_service_client.get_blob_client(container=container_name, blob=file_name)

with open(file_path, "rb") as data:
    blob_client.upload_blob(data)
    print("Upload "+file_name+" file")

uploadToBlobStorage('[your_backup_file_path]', 'Task')

`

Exit editor

**Step 5: Creating file named **recovery.py.

**nano recovery.py

from azure.storage.blob import BlobServiceClient import os

storage_account_key = '[your_storage_key]' storage_account_name = '[your_storage_account_name]' connection_string = '[your_connection_string]' container_name = '[your_container_name]'

download_directory = '[your_recovery_directory_path]'

def download_files(): blob_service_client = BlobServiceClient.from_connection_string(connection_string) container_client = blob_service_client.get_container_client(container_name)

#Ensure the download directory exists
os.makedirs(download_directory, exist_ok=True)

# Download and save each blob to the local directory
for blob in container_client.list_blobs():
    blob_client = container_client.get_blob_client(blob)
    blob_data = blob_client.download_blob()
    blob_file_path = os.path.join(download_directory, blob.name)

    # Write the blob data to a local file
    with open(blob_file_path, "wb") as file:
                    file.write(blob_data.readall())

Call the download_files function to initiate the download

download_files()

`

import blob service client

**Step 6: Accessing the details required in **Step 4 and **Step 5.

Access keys

Data Backup

**Step 7: Run the **backup.py file.

**python backup.py // for python2

**python3 backup.py // for python3

Python3 backup.py

**Step 8: Check if the data has been **backed up to the desired container.

Blob backup container

Data Recovery

**Step 9: Run the **recovery.py file.

**python recovery.py

**OR

**python3 recovery.py

Python3 recovery.py

**Step 10: Verifying if recovery process was successful.

task

Basic Troubleshooting

  1. **How to resolve the given error?
    backupYou must have provided the path address to a folder instead of a file. You can't run a folder in Python.
  2. **I can't find the recovered file on my Local System even after running the recovery.py file.
    • Make sure that you have given the right path to the folder/directory where you want to download your data at time of recovery. The script in **recovery.py file should have the correct path.
    • Check other details such as Storage Account and Container Name are also correct in the file.
  3. **Why do I get **command not found **message when trying to check the version of Python present on my computer?
    Try using **Python3 in command instead of **Python cause you might have Python3 installed and not Python2 and vice versa.
    • Check that python(any of the versions) is available on your pc. You can use **python --version command inside your CLI to check the version of python installed.
    • Also, make sure that Python is added to your systems PATH.