Skip to content

Commit 684903d

Browse files
authored
Add MongoDB backup script to JFrog Artifactory (#1240)
- Add mongodb-backup.sh script for automated MongoDB backups - Backs up directly to JFrog Artifactory via WebDAV mount - No local storage required - streams directly to Artifactory - Features: * Date-based naming (mongodb-backup-YYYY-MM-DD_HH-MM-SS.archive) * Automatic gzip compression * Auto-cleanup of backups older than 20 days * Mount verification and error handling * Cron-ready with logging to stdout - Add comprehensive README with setup and usage instructions Signed-off-by: mahdi@ibm.com Signed-off-by: mahdi@ibm.com
1 parent ca7ac7d commit 684903d

2 files changed

Lines changed: 240 additions & 0 deletions

File tree

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# MongoDB Backup Scripts
2+
3+
This directory contains scripts for backing up MongoDB to JFrog Artifactory.
4+
5+
## mongodb-backup.sh
6+
7+
Automated MongoDB backup script that backs up directly to JFrog Artifactory via WebDAV mount.
8+
9+
### Features
10+
11+
- ✅ Direct backup to Artifactory (no local storage required)
12+
- ✅ Automatic compression with gzip
13+
- ✅ Date-based naming: `mongodb-backup-YYYY-MM-DD_HH-MM-SS.archive`
14+
- ✅ Automatic cleanup of backups older than 30 days
15+
- ✅ Comprehensive logging to `/var/log/mongodb-backup.log`
16+
- ✅ Error handling and mount verification
17+
18+
### Prerequisites
19+
20+
1. **Install required packages:**
21+
```bash
22+
sudo apt update
23+
sudo apt install -y davfs2 mongodb-database-tools
24+
```
25+
26+
2. **Configure WebDAV mount:**
27+
```bash
28+
# Edit davfs2 secrets file
29+
sudo nano /etc/davfs2/secrets
30+
31+
# Add your Artifactory credentials:
32+
https://yourartfactory.com/artifactory/TRSS_Backups <your-username> <your-token>
33+
34+
# Secure the file
35+
sudo chmod 600 /etc/davfs2/secrets
36+
```
37+
38+
3. **Create mount point:**
39+
```bash
40+
sudo mkdir -p /mnt/artifactory
41+
```
42+
43+
4. **Configure fstab for automatic mounting:**
44+
```bash
45+
sudo nano /etc/fstab
46+
47+
# Add this line:
48+
https://yourartfactory.com/artifactory/TRSS_Backups /mnt/artifactory davfs user,noauto,uid=0,gid=0 0 0
49+
```
50+
51+
### Installation
52+
53+
1. **Copy the script to system location:**
54+
```bash
55+
sudo cp mongodb-backup.sh /usr/local/bin/
56+
sudo chmod +x /usr/local/bin/mongodb-backup.sh
57+
```
58+
59+
2. **Create log file:**
60+
```bash
61+
sudo touch /var/log/mongodb-backup.log
62+
sudo chmod 644 /var/log/mongodb-backup.log
63+
```
64+
65+
3. **Test the script:**
66+
```bash
67+
# Mount Artifactory first
68+
sudo mount /mnt/artifactory
69+
70+
# Run the backup script
71+
sudo /usr/local/bin/mongodb-backup.sh
72+
73+
# Check the log
74+
tail -f /var/log/mongodb-backup.log
75+
```
76+
77+
### Automated Backups with Cron
78+
79+
To schedule daily backups at 2:00 AM:
80+
81+
```bash
82+
# Edit root's crontab
83+
sudo crontab -e
84+
85+
# Add this line:
86+
0 2 * * * /usr/local/bin/mongodb-backup.sh >> /var/log/mongodb-backup.log 2>&1
87+
```
88+
89+
### Configuration
90+
91+
Edit the script to customize:
92+
93+
- `MOUNT_POINT`: Artifactory mount point (default: `/mnt/artifactory`)
94+
- `RETENTION_DAYS`: How long to keep backups (default: 30 days)
95+
- `MONGO_HOST`: MongoDB host (default: `localhost`)
96+
- `MONGO_PORT`: MongoDB port (default: `27017`)
97+
- MongoDB authentication (uncomment and set if needed)
98+
99+
### Monitoring
100+
101+
```bash
102+
# View recent logs
103+
sudo tail -50 /var/log/mongodb-backup.log
104+
105+
# List backups in Artifactory
106+
ls -lh /mnt/artifactory/mongodb-backup-*.archive
107+
108+
# Check disk space
109+
df -h /mnt/artifactory
110+
```
111+
112+
### Restoration
113+
114+
To restore from a backup:
115+
116+
```bash
117+
# Restore all databases
118+
mongorestore --archive=/mnt/artifactory/mongodb-backup-2026-05-29_02-00-00.archive --gzip
119+
120+
# Restore specific database
121+
mongorestore --archive=/mnt/artifactory/mongodb-backup-2026-05-29_02-00-00.archive \
122+
--gzip \
123+
--nsInclude="mydb.*"
124+
```
125+
126+
### Troubleshooting
127+
128+
**Mount fails:**
129+
```bash
130+
# Check if davfs2 is installed
131+
dpkg -l | grep davfs2
132+
133+
# Try manual mount
134+
sudo mount -v /mnt/artifactory
135+
136+
# Check logs
137+
sudo tail -f /var/log/syslog | grep davfs
138+
```
139+
140+
**Backup fails:**
141+
```bash
142+
# Check MongoDB is running
143+
sudo systemctl status mongod
144+
145+
# Test mongodump manually
146+
mongodump --host=localhost --port=27017 --archive=/tmp/test.archive --gzip
147+
```
148+
149+
## Support
150+
151+
For issues or questions, please refer to the main repository documentation or contact the infrastructure team.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/bin/bash
2+
3+
#############################################
4+
# MongoDB Backup to JFrog Artifactory
5+
# Backs up directly to mounted Artifactory
6+
# No local storage required
7+
#############################################
8+
9+
# Configuration
10+
MOUNT_POINT="/mnt/artifactory"
11+
DATE=$(date +%Y-%m-%d_%H-%M-%S)
12+
BACKUP_NAME="mongodb-backup-${DATE}"
13+
RETENTION_DAYS=30
14+
15+
# MongoDB connection details
16+
MONGO_HOST="localhost"
17+
MONGO_PORT="27017"
18+
# Uncomment if authentication is required:
19+
# MONGO_USER="backup_user"
20+
# MONGO_PASS="backup_password"
21+
# MONGO_AUTH_DB="admin"
22+
23+
# Function to log messages (output goes to stdout, cron handles logging)
24+
log_message() {
25+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
26+
}
27+
28+
# Check if Artifactory is mounted
29+
check_mount() {
30+
if ! mountpoint -q "$MOUNT_POINT"; then
31+
log_message "ERROR: Artifactory not mounted at $MOUNT_POINT"
32+
log_message "Attempting to mount..."
33+
sudo mount "$MOUNT_POINT"
34+
sleep 2
35+
if ! mountpoint -q "$MOUNT_POINT"; then
36+
log_message "ERROR: Failed to mount Artifactory. Exiting."
37+
exit 1
38+
fi
39+
log_message "Successfully mounted Artifactory"
40+
fi
41+
}
42+
43+
# Start backup process
44+
log_message "=========================================="
45+
log_message "=== Starting MongoDB Backup ==="
46+
log_message "Backup name: $BACKUP_NAME"
47+
log_message "=========================================="
48+
49+
# Check mount
50+
check_mount
51+
52+
# Build mongodump command
53+
MONGODUMP_CMD="mongodump --host=$MONGO_HOST --port=$MONGO_PORT"
54+
55+
# Add authentication if configured
56+
if [ ! -z "$MONGO_USER" ]; then
57+
MONGODUMP_CMD="$MONGODUMP_CMD --username=$MONGO_USER --password=$MONGO_PASS --authenticationDatabase=$MONGO_AUTH_DB"
58+
fi
59+
60+
# Backup directly to Artifactory with compression
61+
MONGODUMP_CMD="$MONGODUMP_CMD --archive=${MOUNT_POINT}/${BACKUP_NAME}.archive --gzip"
62+
63+
# Execute backup
64+
log_message "Backing up directly to Artifactory..."
65+
if $MONGODUMP_CMD 2>&1; then
66+
log_message "✓ MongoDB backup completed successfully"
67+
68+
# Verify file exists
69+
if [ -f "${MOUNT_POINT}/${BACKUP_NAME}.archive" ]; then
70+
BACKUP_SIZE=$(du -h "${MOUNT_POINT}/${BACKUP_NAME}.archive" | cut -f1)
71+
log_message "✓ Backup verified: ${BACKUP_NAME}.archive ($BACKUP_SIZE)"
72+
else
73+
log_message "⚠ WARNING: Could not verify backup file"
74+
fi
75+
else
76+
log_message "✗ ERROR: MongoDB backup failed"
77+
exit 1
78+
fi
79+
80+
# Clean up old backups (older than RETENTION_DAYS)
81+
log_message "Cleaning up old backups (older than $RETENTION_DAYS days)..."
82+
find "$MOUNT_POINT" -name "mongodb-backup-*.archive" -type f -mtime +$RETENTION_DAYS -delete 2>&1
83+
84+
log_message "=========================================="
85+
log_message "=== Backup Process Completed Successfully ==="
86+
log_message "=========================================="
87+
log_message ""
88+
89+
exit 0

0 commit comments

Comments
 (0)