Skip to content

Commit fd3685b

Browse files
committed
docs: update README with detailed backup scheduling instructions and script setup
1 parent bf71ac7 commit fd3685b

File tree

1 file changed

+53
-11
lines changed

1 file changed

+53
-11
lines changed

README.md

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,32 +69,74 @@ docker service scale maintenance_database-backup=1
6969

7070
### Scheduling Regular Backups
7171

72-
To run backups automatically every 3 hours, you can use cron. Here's how to set it up:
72+
To run backups automatically, you'll need to set up a script on your Docker Swarm manager node. This script ensures proper execution by:
7373

74-
1. Create a cron job on your Docker Swarm manager node:
74+
1. Scaling the service to 1 replica
75+
2. Waiting for the backup task to complete
76+
3. Scaling back down to 0 replicas
77+
78+
Here's how to set it up:
79+
80+
1. Create a script file (e.g., `/usr/local/bin/run-backup.sh`):
81+
82+
```bash
83+
#!/usr/bin/env bash
84+
set -euo pipefail
85+
86+
# Ensure SERVICE_NAME is set
87+
if [ -z "${SERVICE_NAME:-}" ]; then
88+
echo "❌ Error: SERVICE_NAME environment variable is not set."
89+
echo " Example: export SERVICE_NAME=database-backup"
90+
exit 1
91+
fi
92+
93+
# 1) Scale up to 1
94+
echo "🔼 Scaling ${SERVICE_NAME} up to 1 replica…"
95+
docker service scale "${SERVICE_NAME}=1"
96+
97+
# 2) Wait for the task to exit (i.e. no tasks in RUNNING state)
98+
echo "⏳ Waiting for ${SERVICE_NAME} to finish…"
99+
while docker service ps "${SERVICE_NAME}" \
100+
--filter "desired-state=running" \
101+
--format '{{.ID}}' | grep -q .; do
102+
sleep 2
103+
done
104+
105+
# 3) Scale back to 0
106+
echo "🔽 Scaling ${SERVICE_NAME} back to 0 replicas…"
107+
docker service scale "${SERVICE_NAME}=0"
108+
109+
echo "✅ ${SERVICE_NAME} has completed and been scaled down."
110+
```
111+
112+
2. Make the script executable:
113+
114+
```bash
115+
chmod +x /usr/local/bin/run-backup.sh
116+
```
117+
118+
3. Create a cron job on your Docker Swarm manager node:
75119

76120
```bash
77121
# Edit crontab
78122
crontab -e
79123
```
80124

81-
2. Add the following line to run backups every 3 hours:
125+
4. Add the following line to run backups every 3 hours:
82126

83127
```bash
84-
0 */3 * * * docker service scale maintenance_database-backup=1
128+
# Set the service name (adjust according to your stack name)
129+
0 */3 * * * export SERVICE_NAME=maintenance_database-backup && /usr/local/bin/run-backup.sh >> /var/log/backup.log 2>&1
85130
```
86131

87132
This will:
88133

89134
- Run at minute 0 of every 3rd hour (00:00, 03:00, 06:00, etc.)
90-
- Scale the backup service to 1 replica, triggering a backup
91-
- The service will automatically scale back to 0 after completion
135+
- Execute the backup script with the proper service name
136+
- Log output to `/var/log/backup.log`
137+
- The script ensures the service completes before scaling down
92138

93-
Note: Make sure the cron daemon has access to the Docker socket. You might need to run the cron job as a user with Docker permissions or use `sudo`:
94-
95-
```bash
96-
0 */3 * * * sudo docker service scale maintenance_database-backup=1
97-
```
139+
Note: Make sure the cron daemon has access to the Docker socket. You might need to run the cron job as a user with Docker permissions or use `sudo`.
98140

99141
## Security Notes
100142

0 commit comments

Comments
 (0)