-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAzUpload.sh
executable file
·97 lines (83 loc) · 3.01 KB
/
AzUpload.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
function validate_env(){
if [ -z "$account_name" ]; then
echo "Missing account_name environment variable"
exit
fi
if [ -z "$container_name" ]; then
echo "Missing container_name environment variable"
exit
fi
if [ -z "$sp_app_id" ]; then
echo "Missing sp_app_id environment variable"
exit
fi
if [ -z "$sp_password" ]; then
echo "Missing sp_password environment variable"
exit
fi
if [ -z "$sp_tenant_id" ]; then
echo "Missing sp_tenant_id environment variable"
exit
fi
if [ -z "$email" ]; then
echo "Missing email environment variable"
exit
fi
if [ -z "$search_dir" ]; then
echo "Missing search_dir environment variable"
exit
fi
if [ -z "$tier" ]; then
echo "Missing tier environment variable"
exit
fi
}
validate_env
echo "Uploading backups..."
#file_pattern=vzdump-*.tar.zst
#*.tar.zst for CT and pve host backups
#*.vma.zst for VM backups
pve_config_file_pattern=pve-host*.zst
backups_file_pattern=vzdump*.zst
#after=$(date -u --date="90 days ago" +"%Y-%m-%dT%H:%M:%SZ")
#Measure upload time
start=$(date +%s)
#Token expires after 90 of inactivity. Use a different scheduled (monthly) job to keep login alive
az login --service-principal -u ${sp_app_id} -p ${sp_password} --tenant ${sp_tenant_id}
#az storage blob upload-batch -d $container_name --account-name $account_name --auth-mode login -s $search_dir --pattern $file_pattern --tier $tier --overwrite false >> $logfile 2>&1
echo "Starting upload of PVE host config..."
{ # try
az storage blob upload-batch -d $container_name --account-name $account_name --auth-mode login -s $search_dir --pattern $pve_config_file_pattern --tier $tier --overwrite false
} || { # catch
echo "Exception while uploading PVE host config."
}
echo "Starting upload of backups..."
# { # try
# az storage blob upload-batch -d $container_name --account-name $account_name --auth-mode login -s $search_dir --pattern $backups_file_pattern --tier $tier --overwrite false
# } || { # catch
# echo "Exception while uploading backups."
# }
# Implement a retry mechanism. Upload may fail if a backup file disappear between the moment the command is launched and the moment the specific file is accessed
# Example : Total uploads of all backups takes 2 days and one of the machine is backed up every day (and only latest backup is kept).
n=0
until [ "$n" -ge 3 ]
do
# command && break # substitute your command here
az storage blob upload-batch -d $container_name --account-name $account_name --auth-mode login -s $search_dir --pattern $backups_file_pattern --tier $tier --overwrite false && break
echo "Exception while uploading backups."
n=$((n+1))
sleep 5
done
end=$(date +%s)
seconds=$(($end-$start))
elapsed=$(date -d@$seconds -u +"%j days %H:%M:%S")
body=$(cat <<-END
Upload of homelab backups to cloud completed.
Job time: $elapsed
END
)
logfile="/var/log/backup2azure.log"
#Confirm upload
mail -A $logfile -s "[Homelab] Offsite backup completed" "$email" <<< "$body"
echo "Done uploading backups"