我准备了azure cli脚本azurebk.azcli来备份azure虚拟机。脚本执行环境为Linux。在 az-backup 命令中,我提到了日期格式的保留期。但根据我的客户请求,我需要为此脚本设置 crontab。在没有 crontab 的情况下正常执行脚本时,需要每次更改日期以保留期限。需要帮助获取 azCLI 命令,该命令将自动提取日期(今天日期 + 添加接下来的 3 天)以进行备份。
以下是代码:
# Read the subscription ID or name
subscriptionId=subscriptionID
az account set --subscription $subscriptionId
# Check if required parameters are provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <ResourceGroupName> <VMName>"
exit 1
fi
# Assign RGName and VMName as argument parameters
resourceGroupName=$1
vmName=$2
#Define the parameters
date=(date -d "+3days")
recoveryServicesVaultName=testvault1
location=Central US
#Run the backup
az backup protection backup-now --resource-group $resourceGroupName --vault-name $recoveryServicesVaultName --container-name $vmName --item-name $vmName --retain-until $date --backup-management-type AzureIaasVM
# Check the exit status of the previous command
if [ $? -eq 0 ]; then
echo "backup is successful"
exit 0 # Success
else
echo "backup is failed"
exit 99 # Failure
fi
需要帮助获取 azCLI 命令,该命令将自动提取日期(今天日期 + 添加接下来的 3 天)以进行备份。
为了根据当前日期计算
Azure CLI
脚本中的保留日期并添加three days
。您可以通过在脚本中使用 date
命令来实现此目的。
date=$(date -u +%d-%m-%Y -d "+3 days")
这里是更新的代码,用于启动 azure 的备份
Virtual Machine
。
#!/bin/bash
subscriptionId="Subscription_ID"
az account set --subscription $subscriptionId
if [ "$#" -ne 2 ]; then
echo "Usage: $0 venkat-RG venkat-linux"
exit 1
fi
# Assign RGName and VMName as argument parameters
resourceGroupName=$1
vmName=$2
# Fetch item_name for the VM
itemname=$(az backup item list --resource-group $resourceGroupName --vault-name "vm-vault" --output json | jq -r --arg vmName "$vmName" '.[] | select(.properties.friendlyName == $vmName) | .name')
# Check if itemname is empty
if [ -z "$itemname" ]; then
echo "VM not found in backup items."
exit 1
fi
# Define the parameters
date=$(date -u +%d-%m-%Y -d "+3 days")
echo "Retention Period: $date"
recoveryServicesVaultName="vm-vault"
location="East US"
# Run the backup
az backup protection backup-now --resource-group $resourceGroupName --item-name $itemname --vault-name $recoveryServicesVaultName --container-name $vmName --retain-until $date --backup-management-type AzureIaasVM
# Check the exit status of the previous command
if [ $? -eq 0 ]; then
echo "Backup is successful"
exit 0 # Success
else
echo "Backup failed"
exit 99 # Failure
fi
回复:
运行代码后,备份作业已按照门户中的日期要求启动。