What is Azure File Sync?

Azure File Sync is a cloud-based solution that allows you to synchronize files between Windows servers and Azure Files. With Azure File Sync, you can centralize your organization’s file shares in Azure Files while maintaining local access to the files.

Prerequisites

Before you start using Azure File Sync, you need to have the following:

  1. An Azure subscription
  2. A storage account in Azure
  3. Windows servers running Windows Server 2012 R2 or later
  4. Azure PowerShell or Azure CLI installed on your local machine

Step 1: Create an Azure File Sync Service

The first step in using Azure File Sync is to create an Azure File Sync service. You can create a new Azure File Sync service using Azure CLI with the following commands:

  1. Login to your Azure account:
az login

2 .Set the subscription to be used:

az account set --subscription <subscription-id>

3.Create a new resource group:

az group create --name <resource-group-name> --location <location>

4.Create a new Azure File Sync service:

az storage sync service create --name <sync-service-name> --resource-group <resource-group-name> --storage-account <storage-account-name> --azure-file-share <file-share-name>
  1. Replace <sync-service-name> with a unique name for your Azure File Sync service, <resource-group-name> with the name of the resource group you created, <storage-account-name> with the name of your storage account, and <file-share-name> with the name of the Azure file share you want to sync.

Step 2: Register Windows Servers

After creating an Azure File Sync service, you need to register the Windows servers that will participate in the sync. You can register a Windows server using Azure CLI with the following commands:

  1. Install the Azure File Sync agent on the Windows server. You can download the agent from the following link: https://aka.ms/afsagent
  2. Install the Azure PowerShell or Azure CLI on the Windows server.
  3. Open PowerShell or Command Prompt and run the following command to register the server with the Azure File Sync service:
Register-AzStorageSyncServer -SyncServiceName <sync-service-name> -ResourceGroupName <resource-group-name> -Name <server-name>
  1. Replace <sync-service-name> with the name of the Azure File Sync service you created, <resource-group-name> with the name of the resource group you created, and <server-name> with the name of the Windows server you want to register.

Step 3: Create a Sync Group

After registering the Windows servers, you need to create a sync group to define which files will be synced between the servers and Azure Files. You can create a sync group using Azure CLI with the following commands:

  1. Create a new sync group:
az storage sync group create --name <sync-group-name> --service-name <sync-service-name> --resource-group <resource-group-name> --subscription <subscription-id> --sync-protocol SMB

Replace <sync-group-name> with a unique name for your sync group, <sync-service-name> with the name of the Azure File Sync service you created, <resource-group-name> with the name of the resource group you created, and <subscription-id> with the ID of your Azure subscription.

2.Add a server endpoint:

az storage sync group endpoint create --name <server-endpoint-name> --service-name <sync-service-name> --resource-group <resource-group-name> --subscription <subscription-id> --sync-group <sync-group-name> --path <local-path> --server-name <server-name>

Replace `<server-endpoint-name>` with a unique name for your server endpoint, `<sync-service-name>` with the name of the Azure File Sync service you created, `<resource-group-name>` with the name of the resource group you created, `<subscription-id>` with the ID of your Azure subscription, `<sync-group-name>` with the name of the sync group you created, `<local-path>` with the path to the local folder you want to sync, and `<server-name>` with the name of the Windows server you registered in Step 2.

3. Add a cloud endpoint:

az storage sync group cloud-endpoint create --name <cloud-endpoint-name> --service-name <sync-service-name> --resource-group <resource-group-name> --subscription <subscription-id> --sync-group <sync-group-name> --azure-file-share <file-share-name>

Replace `<cloud-endpoint-name>` with a unique name for your cloud endpoint, `<sync-service-name>` with the name of the Azure File Sync service you created, `<resource-group-name>` with the name of the resource group you created, `<subscription-id>` with the ID of your Azure subscription, `<sync-group-name>` with the name of the sync group you created, and `<file-share-name>` with the name of the Azure file share you want to sync.

Step 4: Sync Files

After creating a sync group, you can start syncing files between the Windows servers and Azure Files. You can initiate a sync using Azure CLI with the following command:

az storage sync group sync --service-name <sync-service-name> --resource-group <resource-group-name> --subscription <subscription-id> --sync-group <sync-group-name>

Replace `<sync-service-name>` with the name of the Azure File Sync service you created, `<resource-group-name>` with the name of the resource group you created, `<subscription-id>` with the ID of your Azure subscription, and `<sync-group-name>` with the name of the sync group you created.

Conclusion

In this tutorial, you learned how to use Azure CLI to create an Azure File Sync service, register Windows servers, create a sync group, and sync files between the Windows servers and Azure Files. Azure File Sync is a powerful tool for centralizing file shares in Azure while maintaining local access to the files.