-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb-backup.sh
55 lines (46 loc) · 1.89 KB
/
mongodb-backup.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
# Set the source and destination directories
src="/path/to/mongodb/data/directory"
dst="user@remote.server:/path/to/backup/directory"
# Set the email address to send alerts to
email_address="your@email.com"
# Set the log file
log_file="/path/to/log/file.log"
# Decrypt the password using the private key
password=$(gpg --decrypt password.gpg)
# Create a temporary directory to store the encrypted backups
tmp_dir=$(mktemp -d)
# Dump the MongoDB database to the temporary directory
mongodump --host localhost --db mydatabase --out "$tmp_dir"
if [ $? -ne 0 ]; then
echo "$(date): Dump of the MongoDB database failed." >> "$log_file"
echo "The dump of the MongoDB database failed." | mail -s "Backup Failure" "$email_address"
rm -rf "$tmp_dir"
exit 1
fi
# Compress the dump using tar
tar -czvf "$tmp_dir/dump.tar.gz" "$tmp_dir/mydatabase"
if [ $? -ne 0 ]; then
echo "$(date): Compression of the MongoDB dump failed." >> "$log_file"
echo "The compression of the MongoDB dump failed." | mail -s "Backup Failure" "$email_address"
rm -rf "$tmp_dir"
exit 1
fi
# Encrypt the backups using openssl
openssl enc -aes-256-cbc -salt -in "$tmp_dir/dump.tar.gz" -out "$tmp_dir/encrypted.tar.gz" -pass "pass:$password"
if [ $? -ne 0 ]; then
echo "$(date): Encryption of the MongoDB dump failed." >> "$log_file"
echo "The encryption of the MongoDB dump failed." | mail -s "Backup Failure" "$email_address"
rm -rf "$tmp_dir"
exit 1
fi
# Perform the backup using rsync
rsync -av "$tmp_dir/encrypted.tar.gz" "$dst"
if [ $? -eq 0 ]; then
echo "The backup of the MongoDB database to $dst completed successfully." | mail -s "Backup Success" "$email_address"
else
echo "The backup of the MongoDB database to $dst failed." | mail -s "Backup Failure" "$email_address"
echo "$(date): Backup of the MongoDB database to $dst failed." >> "$log_file"
exit 1
fi
# Remove the temporary directory and its contents
rm -rf "$tmp_dir"