-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb-restore.sh
79 lines (67 loc) · 2.84 KB
/
mongodb-restore.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
# Set the source and destination directories
src="[email protected]:/path/to/backup/directory"
dst="/path/to/mongodb/data/directory"
# Set the email address to send alerts to
email_address="[email protected]"
# Set the log file
log_file="/path/to/log/file.log"
# Decrypt the password using the private key
password=$(gpg --decrypt password.gpg)
# Check if password decryption was successful
if [ $? -ne 0 ]; then
echo "$(date): Decryption of password failed." >> "$log_file"
echo "The decryption of the password for the MongoDB backup failed." | mail -s "Restore Failure" "$email_address"
exit 1
fi
# Create a temporary directory to store the decrypted backups
tmp_dir=$(mktemp -d)
# Download the encrypted backup from the remote server
rsync -av "$src" "$tmp_dir"
if [ $? -ne 0 ]; then
echo "$(date): Download of the MongoDB backup from $src failed." >> "$log_file"
echo "The download of the MongoDB backup from $src failed." | mail -s "Restore Failure" "$email_address"
rm -rf "$tmp_dir"
exit 1
fi
# Decrypt the backup using openssl
openssl enc -d -aes-256-cbc -in "$tmp_dir/encrypted.tar.gz" -out "$tmp_dir/decrypted.tar.gz" -pass "pass:$password"
if [ $? -ne 0 ]; then
echo "$(date): Decryption of the MongoDB backup failed." >> "$log_file"
echo "The decryption of the MongoDB backup failed." | mail -s "Restore Failure" "$email_address"
rm -rf "$tmp_dir"
exit 1
fi
# Extract the tar archive to the temporary directory
tar -xzvf "$tmp_dir/decrypted.tar.gz" -C "$tmp_dir"
if [ $? -ne 0 ]; then
echo "$(date): Extraction of the MongoDB backup failed." >> "$log_file"
echo "The extraction of the MongoDB backup failed." | mail -s "Restore Failure" "$email_address"
rm -rf "$tmp_dir"
exit 1
fi
# Stop the MongoDB service or process
systemctl stop mongod
if [ $? -ne 0 ]; then
mongod --shutdown
if [ $? -ne 0 ]; then
echo "$(date): Stopping the MongoDB service or process failed." >> "$log_file"
echo "Stopping the MongoDB service or process failed. Please stop the service or process manually before proceeding with the restore operation." | mail -s "Restore Failure" "$email_address"
rm -rf "$tmp_dir"
exit 1
fi
fi
# Restore the MongoDB database from the dump
mongorestore --host localhost --db mydatabase "$tmp_dir/mydatabase"
if [ $? -ne 0 ]; then
echo "$(date): Restore of the MongoDB database failed." >> "$log_file"
echo "The restore of the MongoDB database from the backup at $src failed." | mail -s "Restore Failure" "$email_address"
rm -rf "$tmp_dir"
exit 1
else
echo "The restore of the MongoDB database from the backup at $src completed successfully." >> "$log_file"
echo "The restore of the MongoDB database from the backup at $src completed successfully." | mail -s "Restore Success" "$email_address"
fi
# Start the MongoDB service or process
systemctl start mongod
# Clean up the temporary directory
rm -rf "$tmp_dir"