Skip to content

Commit 951cb50

Browse files
authored
Merge pull request #192389 from stevenmatthew/blobCLI
Updated blob-CLI soft-delete examples to support versioning
2 parents edaa531 + 754a2b4 commit 951cb50

File tree

1 file changed

+105
-38
lines changed

1 file changed

+105
-38
lines changed

articles/storage/blobs/blob-cli.md

Lines changed: 105 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,10 @@ done < /mnt/c/temp/bloblist.xml
416416

417417
You can delete either a single blob or series of blobs with the `az storage blob delete` and `az storage blob delete-batch` commands. When deleting multiple blobs, you can use conditional operations, loops, or other automation as shown in the examples below.
418418

419-
[!WARNING] Running the following examples may permanently delete blobs. Microsoft recommends enabling container soft delete to protect containers and blobs from accidental deletion. For more info, see Soft delete for containers.
419+
> [!WARNING]
420+
> Running the following examples may permanently delete blobs. Microsoft recommends enabling container soft delete to protect containers and blobs from accidental deletion. For more info, see [Soft delete for containers](soft-delete-blob-overview.md).
420421
421-
The following sample code provides an example of both single and multiple download approaches. The first example deletes a single, named blob. The second example illustrates the use of logical operations in Bash to delete multiple blobs. The third example uses the `delete-batch` command to delete all blobs with the format *bennett-x*, except *bennett-2*.
422+
The following sample code provides an example of both individual and batch delete operations. The first example deletes a single, named blob. The second example illustrates the use of logical operations in Bash to delete multiple blobs. The third example uses the `delete-batch` command to delete all blobs with the format *bennett-x*, except *bennett-2*.
422423

423424
For more information, see the [az storage blob delete](/cli/azure/storage/blob#az-storage-blob-delete) and [az storage blob delete-batch](/cli/azure/storage/blob#az-storage-blob-delete-batch) reference.
424425

@@ -476,9 +477,9 @@ az storage blob delete-batch \
476477
--auth-mode login
477478
```
478479

479-
If your storage account's soft delete data protection option is enabled, you can use a listing operation to return blobs deleted within the associated retention period. To learn more about soft delete, refer to the [Soft delete for blobs](soft-delete-blob-overview.md) article.
480+
In some cases, it's possible to retrieve blobs that have been deleted. If your storage account's soft delete data protection option is enabled, the `--include d` parameter and value will return blobs deleted within the account's retention period. To learn more about soft delete, refer to thee [Soft delete for blobs](soft-delete-blob-overview.md) article.
480481

481-
Use the following example to retrieve a list of blobs deleted within container's associated retention period. The result displays a list of recently deleted blobs.
482+
Use the following examples to retrieve a list of blobs deleted within container's associated retention period. The first example displays a list of all recently deleted blobs and the dates on which they were deleted. The second example lists all deleted blobs matching a specific prefix.
482483

483484
```azurecli-interactive
484485
#!/bin/bash
@@ -496,7 +497,7 @@ az storage blob list \
496497
--auth-mode login \
497498
--query "[?deleted].{name:name,deleted:properties.deletedTime}"
498499
499-
#Retrieve a list of all blobs matching specific prefix
500+
#Retrieve a list of all deleted blobs matching a specific prefix
500501
az storage blob list \
501502
--container-name $containerName \
502503
--prefix $blobPrefix \
@@ -507,49 +508,115 @@ az storage blob list \
507508
--query "[].{name:name,deleted:deleted}"
508509
```
509510

510-
## Restore a soft-deleted blob
511-
As mentioned in the [List blobs](#list-blobs) section, you can configure the soft delete data protection option on your storage account. When enabled, it's possible to restore containers deleted within the associated retention period.
511+
## Restore a deleted blob
512+
As mentioned in the [List blobs](#list-blobs) section, you can configure the soft delete data protection option on your storage account. When enabled, it's possible to restore containers deleted within the associated retention period. You may also use versioning to maintain previous versions of your blobs for each recovery and restoration.
512513

513-
The following examples restore soft-deleted blobs with the `az storage blob undelete` method. The first example uses the `--name` parameter to restore a single named blob. The second example uses a loop to restore the remainder of the deleted blobs. Before you can follow this example, you'll need to enable soft delete on at least one of your storage accounts.
514+
If blob versioning and blob soft delete are both enabled, then modifying, overwriting, deleting, or restoring a blob automatically creates a new version. The method you'll use to restore a deleted blob will depend upon whether versioning is enabled on your storage account.
514515

515-
To learn more about the soft delete data protection option, refer to the [Soft delete for blobs](soft-delete-blob-overview.md) article or the [az storage blob undelete](/cli/azure/storage/blob#az-storage-blob-undelete) reference.
516+
The following code sample restores all soft-deleted blobs or, if versioning is enabled, restores the latest version of a blob. It first determines whether versioning is enabled with the `az storage account blob-service-properties show` command.
517+
518+
If versioning is enabled, the `az storage blob list` command retrieves a list of all uniquely-named blob versions. Next, the blob versions on the list are retrieved and ordered by date. If no versions are found with the `isCurrentVersion` attribute value, the `az storage blob copy start` command is used to make an active copy of the blob's latest version.
519+
520+
If versioning is disabled, the `az storage blob undelete` command is used to restore each soft-deleted blob in the container.
521+
522+
Before you can follow this example, you'll need to enable soft delete on at least one of your storage accounts. To learn more about the soft delete data protection option, refer to the [Soft delete for blobs](soft-delete-blob-overview.md) article or the [az storage blob undelete](/cli/azure/storage/blob#az-storage-blob-undelete) reference.
516523

517524
```azurecli-interactive
518525
#!/bin/bash
519526
storageAccount="<storage-account>"
527+
groupName="myResourceGroup"
520528
containerName="demo-container"
521529
522-
blobName="demo-file.txt"
523-
524-
#Restore a single, named blob
525-
az storage blob undelete \
526-
--container-name $containerName \
527-
--name $blobName \
528-
--account-name $storageAccount \
529-
--auth-mode login
530-
531-
#Retrieve all deleted blobs
532-
blobList=$( \
533-
az storage blob list \
534-
--container-name $containerName \
535-
--include d \
536-
--output tsv \
530+
blobSvcProps=$(
531+
az storage account blob-service-properties show \
537532
--account-name $storageAccount \
538-
--auth-mode login \
539-
--query "[?deleted].[name]" \
540-
)
533+
--resource-group $groupName)
541534
542-
#Iterate list of deleted blobs and restore
543-
for row in $blobList
544-
do
545-
tmpName=$(echo $row | sed -e 's/\r//g')
546-
echo "Restoring $tmpName"
547-
az storage blob undelete \
548-
--container-name $containerName \
549-
--name $tmpName \
550-
--account-name $storageAccount \
551-
--auth-mode login
552-
done
535+
softDelete=$(echo "${blobSvcProps}" | jq -r '.deleteRetentionPolicy.enabled')
536+
versioning=$(echo "${blobSvcProps}" | jq -r '.isVersioningEnabled')
537+
538+
# If soft delete is enabled
539+
if $softDelete
540+
then
541+
542+
# If versioning is enabled
543+
if $versioning
544+
then
545+
546+
# Get all blobs and versions using -Unique to avoid processing duplicates/versions
547+
blobList=$(
548+
az storage blob list \
549+
--account-name $storageAccount \
550+
--container-name $containerName \
551+
--include dv \--query "[?versionId != null].{name:name}" \
552+
--auth-mode login -o tsv | uniq)
553+
554+
# Iterate the collection
555+
for blob in $blobList
556+
do
557+
# Get all versions of the blob, newest to oldest
558+
blobVers=$(
559+
az storage blob list \
560+
--account-name $storageAccount \
561+
--container-name $containerName \
562+
--include dv \
563+
--prefix $blob \
564+
--auth-mode login -o json | jq 'sort_by(.versionId) | reverse | .[]')
565+
# Select the first (newest) object
566+
delBlob=$(echo "$blobVers" | jq -sr '.[0]')
567+
568+
# Verify that the newest version is NOT the latest (that the version is "deleted")
569+
if [[ $(echo "$delBlob" | jq '.isCurrentVersion') != true ]];
570+
then
571+
# Get the blob's versionId property, build the URI to the blob
572+
versionID=$(echo "$delBlob" | jq -r '.versionId')
573+
uri="https://$storageAccount.blob.core.windows.net/$containerName/$blob?versionId=$versionID"
574+
575+
# Copy the latest version
576+
az storage blob copy start \
577+
--account-name $storageAccount \
578+
--destination-blob $blob \
579+
--destination-container $containerName \
580+
--source-uri $uri \
581+
--auth-mode login
582+
583+
delBlob=""
584+
fi
585+
done
586+
587+
else
588+
589+
#Retrieve all deleted blobs
590+
blobList=$( \
591+
az storage blob list \
592+
--container-name $containerName \
593+
--include d \
594+
--output tsv \
595+
--account-name $storageAccount \
596+
--auth-mode login \
597+
--query "[?deleted].[name]" \
598+
)
599+
600+
#Iterate list of deleted blobs and restore
601+
for row in $blobList
602+
do
603+
tmpName=$(echo $row | sed -e 's/\r//g')
604+
echo "Restoring $tmpName"
605+
az storage blob undelete \
606+
--container-name $containerName \
607+
--name $tmpName \
608+
--account-name $storageAccount \
609+
--auth-mode login
610+
done
611+
612+
fi
613+
614+
else
615+
616+
#Soft delete is not enabled
617+
echo "Sorry, the delete retention policy is not enabled."
618+
619+
fi
553620
```
554621

555622
## Next steps

0 commit comments

Comments
 (0)