-
Notifications
You must be signed in to change notification settings - Fork 0
73 lines (63 loc) · 2.44 KB
/
dev-db-backup.yml
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
name: Backup MongoDB Atlas db-dev
on:
schedule:
- cron: '0 3 * * *' # Everyday at 3:00 am (UTC 0) | 5:00 am (UTC 2)
workflow_dispatch: # Possibility to launch manually
env:
DB_SOURCE: db-dev
DB_SOURCE_URL: ${{ secrets.MONGODB_DEV_URL }}
DB_TEST: db-test
DB_TEST_URL: ${{ secrets.MONGODB_TEST_URL }}
jobs:
backup-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Set Date env
id: set-date
run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
- name: Install Dependencies
run: |
sudo apt update
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-ubuntu2404-x86_64-100.10.0.deb -O mongodb-mongotools.deb
sudo dpkg -i mongodb-mongotools.deb || sudo apt install -f -y openssl
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
- name: Create MongoDB Dump
env:
TODAY: ${{ steps.set-date.outputs.date }}
run: |
mongodump \
--out=backup-$DB_SOURCE-$TODAY \
--uri="$DB_SOURCE_URL" \
-d $DB_SOURCE
- name: Restore Dump to MongoDB Atlas Test Cluster
env:
TODAY: ${{ steps.set-date.outputs.date }}
run: |
mongorestore \
backup-$DB_SOURCE-$TODAY \
--uri="$DB_TEST_URL" \
--nsInclude="$DB_SOURCE.*" \
--nsFrom="$DB_SOURCE.*" \
--nsTo="$DB_TEST.*" \
--drop
- name: Encrypt Dump
env:
ENCRYPTION_KEY: ${{ secrets.ENCRYPTION_KEY }}
TODAY: ${{ steps.set-date.outputs.date }}
run: |
tar -czf backup-$DB_SOURCE-$TODAY.tar.gz backup-$DB_SOURCE-$TODAY
openssl enc -aes-256-cbc -salt -in backup-$DB_SOURCE-$TODAY.tar.gz -out backup-$DB_SOURCE-$TODAY.tar.gz.enc -k "$ENCRYPTION_KEY"
rm -rf backup-$DB_SOURCE-$TODAY backup-$DB_SOURCE-$TODAY.tar.gz
- name: Upload Dump to Azure Storage
env:
AZURE_STORAGE_CONNECTION_STRING: ${{ secrets.AZURE_STORAGE_CONNECTION_STRING }}
TODAY: ${{ steps.set-date.outputs.date }}
run: |
az storage blob upload \
--container-name db-backups \
--file backup-$DB_SOURCE-$TODAY.tar.gz.enc \
--name backup-$DB_SOURCE-$TODAY.tar.gz.enc \
--overwrite \
--connection-string "$AZURE_STORAGE_CONNECTION_STRING"