-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit-backup
64 lines (48 loc) · 2.08 KB
/
init-backup
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
#!/usr/bin/env bash
set -e
echo "### logging in to Azure"
az login --identity --verbose
echo "### Setting the subscription"
az account set --subscription $SUBSCRIPTION_ID
echo "### Getting the secret from Azure Key Vault"
# Get Secret from KV - Private Key
vault_name=$KEY_VAULT_NAME
secret_name=$SECRET_NAME
mkdir -p /root/.ssh
az keyvault secret show --name $secret_name --vault-name $vault_name --query value -o tsv > /root/.ssh/id_rsa
chmod 600 /root/.ssh/id_rsa
# Get Secret from KV - Host Key
echo "### Getting the host key from Azure Key Vault"
secret_name=$GH_HOST_KEY_SECRET_NAME
az keyvault secret show --name $secret_name --vault-name $vault_name --query value -o tsv > /root/.ssh/known_hosts
chmod 644 /root/.ssh/known_hosts
echo "### preparing the config file"
# Prepare backup.config file
cp /backup-utils/backup.config-example /backup-utils/backup.config
NEW_HOSTNAME=$GHE_HOSTNAME
sed -i "s/^GHE_HOSTNAME=\".*\"/GHE_HOSTNAME=\"$NEW_HOSTNAME\"/" /backup-utils/backup.config
# Running the backup script
echo "### Running the backup script"
cd /backup-utils/bin/
bash ./ghe-backup
GHE_SNAPSHOT_TIMESTAMP=$(ls -lt /backup-utils/data/ | grep "^d" | head -n 1 | awk '{print $NF}')
# archive the contents of the data directory with tar
echo "### Archiving the contents of the data directory with tar"
tar -czf /mnt/data/$GHE_SNAPSHOT_TIMESTAMP.tar.gz -C /backup-utils/data/$GHE_SNAPSHOT_TIMESTAMP .
# Delete any files under /mnt/data that are older than $DAYS_RETENTION
echo "### Deleting files older than $DAYS_RETENTION days from /mnt/data"
backup_count=$(find /mnt/data -type f | wc -l)
if [ $backup_count -gt $MIN_BACKUP_NUM ]; then
find /mnt/data -type f -mmin +$((DAYS_RETENTION * 1440)) | while read -r file; do
echo "### Deleting file: $file"
if [ $(find /mnt/data -type f | wc -l) -gt $MIN_BACKUP_NUM ]; then
rm -f "$file"
backup_count=$((backup_count - 1))
else
echo "### Minimum number of backups ($MIN_BACKUP_NUM) reached. Stopping deletion."
break
fi
done
else
echo "### Minimum number of backups ($MIN_BACKUP_NUM) not reached. Skipping deletion."
fi