-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.sh
More file actions
executable file
·71 lines (57 loc) · 1.96 KB
/
main.sh
File metadata and controls
executable file
·71 lines (57 loc) · 1.96 KB
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
#!/bin/bash
# Copyright MalwarePad Productions 2016 - 2023
# Least complicated bash code regarding colors (i absolutely love this language (lie))
nc='\033[0m'
red='\033[0;31m'
blue='\033[0;34m'
green='\033[0;32m'
use_s3() {
S3_KEY="${MONGODUMP_S3_KEY_PREFIX}/${1}"
aws configure set aws_access_key_id "$MONGODUMP_S3_ACCESS_KEY"
aws configure set aws_secret_access_key "$MONGODUMP_S3_SECRET_KEY"
aws s3 --endpoint-url "$MONGODUMP_S3_ENDPOINT" cp "$1" "s3://$MONGODUMP_S3_BUCKET/$S3_KEY"
}
check_command() {
if ! command -v "$1" &>/dev/null; then
echo -e "${red}The $1 binary cannot be found/executed!${nc}"
exit 1
fi
}
check_command "mongodump"
check_command "zip"
check_command "gpg"
check_command "aws"
[[ -z "${MONGODUMP_URI}" || -z "${MONGODUMP_SLEEP}" || -z "${MONGODUMP_S3_ACCESS_KEY}" || -z "${MONGODUMP_S3_SECRET_KEY}" || -z "${MONGODUMP_S3_ENDPOINT}" || -z "${MONGODUMP_S3_BUCKET}" || -z "${MONGODUMP_S3_KEY_PREFIX}" ]] &&
{
echo -e "${red}Invalid environment detected!${nc}"
exit 1
}
[[ ! -f "${MONGODUMP_GPG}" ]] &&
{
echo -e "${red}No public key to do the encryption against!${nc}"
exit 1
}
while :; do
EXPORT_NAME=$(date "+%F-%T")
echo -e "${blue}Export ${EXPORT_NAME} was initiated...${nc}"
if mongodump --uri "${MONGODUMP_URI}" --out "./out"; then # dumping
# Zipping
echo -e "${blue}Export ${EXPORT_NAME} is being zipped...${nc}"
zip -rm "${EXPORT_NAME}.zip" out/
# Encryption
echo -e "${blue}Export ${EXPORT_NAME} is being encrypted...${nc}"
gpg --recipient-file "${MONGODUMP_GPG}" --encrypt "${EXPORT_NAME}.zip"
rm "${EXPORT_NAME}.zip"
# Uploading
echo -e "${blue}Export ${EXPORT_NAME} is being uploaded to s3...${nc}"
use_s3 "${EXPORT_NAME}.zip.gpg"
# Cleanup
rm "${EXPORT_NAME}.zip.gpg"
echo -e "${green}Export ${EXPORT_NAME} succeeded!${nc}"
else
echo -e "${red}Export ${EXPORT_NAME} failed!${nc}"
exit 1
fi
echo -e "${blue}Waiting ${MONGODUMP_SLEEP} before running again...${nc}"
sleep "${MONGODUMP_SLEEP}"
done