docs: schedule production database dumps and copy them to the Mac
Three dumps a day stay on the server for 3 days. A Mac script copies every dump file after one password prompt.
This commit is contained in:
@@ -31,6 +31,59 @@ Keep dump files private. They contain patient and account data. Do not commit th
|
||||
|
||||
---
|
||||
|
||||
## Automatic backups (three times a day)
|
||||
|
||||
The server keeps a new dump at three Iran times (`Asia/Tehran`, UTC+3:30). Each run adds a file. It does not replace the previous one. Files named `nudentic_YYYYMMDD_HHMMSS.dump` that are older than 3 days are deleted. That is about 9 files. `nudentic_before_restore_*.dump` is never deleted by this job.
|
||||
|
||||
| Iran time | What it captures |
|
||||
|-----------|------------------|
|
||||
| 02:00 | The previous day, after the clinic is closed |
|
||||
| 11:00 | Morning work |
|
||||
| 16:00 | Most of the afternoon |
|
||||
|
||||
The longest gap is overnight, from 16:00 to 02:00. Rows saved in that gap are only in the live database until the next file. These files sit on the same disk as the database. Copy them to the Mac with section 2 when you want copies that survive a dead server. Filenames use UTC, the same clock as the first manual dump.
|
||||
|
||||
The script is [`scripts/backup-prod-db.sh`](scripts/backup-prod-db.sh). A git tag deploy does not copy it, so put it on the server once from the Mac:
|
||||
|
||||
```bash
|
||||
scp infrastructure/scripts/backup-prod-db.sh dyolink@185.243.48.140:/opt/dyolink/infrastructure/scripts/
|
||||
ssh dyolink@185.243.48.140 'chmod +x /opt/dyolink/infrastructure/scripts/backup-prod-db.sh && /opt/dyolink/infrastructure/scripts/backup-prod-db.sh'
|
||||
```
|
||||
|
||||
The last command writes one dump immediately so you can see it in `database/backups` before cron is installed.
|
||||
|
||||
Cron is also one time, on the server, as the `dyolink` user. Check the server clock first:
|
||||
|
||||
```bash
|
||||
date
|
||||
```
|
||||
|
||||
If that time is already Iran time (offset `+0330`), install these three lines:
|
||||
|
||||
```bash
|
||||
(crontab -l 2>/dev/null | grep -v backup-prod-db.sh; cat <<'EOF'
|
||||
0 2 * * * /opt/dyolink/infrastructure/scripts/backup-prod-db.sh >> /opt/dyolink/infrastructure/database/backups/backup.log 2>&1
|
||||
0 11 * * * /opt/dyolink/infrastructure/scripts/backup-prod-db.sh >> /opt/dyolink/infrastructure/database/backups/backup.log 2>&1
|
||||
0 16 * * * /opt/dyolink/infrastructure/scripts/backup-prod-db.sh >> /opt/dyolink/infrastructure/database/backups/backup.log 2>&1
|
||||
EOF
|
||||
) | crontab -
|
||||
```
|
||||
|
||||
If `date` shows UTC, the same Iran times are 22:30, 07:30, and 12:30 UTC:
|
||||
|
||||
```bash
|
||||
(crontab -l 2>/dev/null | grep -v backup-prod-db.sh; cat <<'EOF'
|
||||
30 22 * * * /opt/dyolink/infrastructure/scripts/backup-prod-db.sh >> /opt/dyolink/infrastructure/database/backups/backup.log 2>&1
|
||||
30 7 * * * /opt/dyolink/infrastructure/scripts/backup-prod-db.sh >> /opt/dyolink/infrastructure/database/backups/backup.log 2>&1
|
||||
30 12 * * * /opt/dyolink/infrastructure/scripts/backup-prod-db.sh >> /opt/dyolink/infrastructure/database/backups/backup.log 2>&1
|
||||
EOF
|
||||
) | crontab -
|
||||
```
|
||||
|
||||
Confirm with `crontab -l`. Putting this in git does not turn cron on. On nudentic the UTC lines above are already installed, so `date` on that server prints UTC and the jobs run at 22:30, 07:30, and 12:30 UTC.
|
||||
|
||||
---
|
||||
|
||||
## 1. Take a backup
|
||||
|
||||
The site stays up. SSH in:
|
||||
@@ -60,22 +113,14 @@ Leave this SSH session open if you still need it. The next step is a **new** ter
|
||||
|
||||
## 2. Copy the dump to the Mac
|
||||
|
||||
Open a terminal on the Mac. The prompt must be `rameen@…`, not `dyolink@srv…`.
|
||||
|
||||
Replace `YYYYMMDD_HHMMSS` with the timestamp from `ls` (example: `nudentic_20261006_133214.dump`).
|
||||
Open a terminal on the Mac. The prompt must be `rameen@…`, not `dyolink@srv…`. From the repo:
|
||||
|
||||
```bash
|
||||
mkdir -p ~/Documents/dental/nudentic-backups
|
||||
scp dyolink@185.243.48.140:/opt/dyolink/infrastructure/database/backups/nudentic_YYYYMMDD_HHMMSS.dump ~/Documents/dental/nudentic-backups/
|
||||
cd /Users/rameen/Documents/dental/dyolink
|
||||
./infrastructure/scripts/copy-prod-backup.sh
|
||||
```
|
||||
|
||||
It asks for the server password. When it finishes:
|
||||
|
||||
```bash
|
||||
ls -lh ~/Documents/dental/nudentic-backups
|
||||
```
|
||||
|
||||
The size should match the file on the server (the first successful dump was 574K).
|
||||
It asks for the server password once and does not save it. It copies every `.dump` file from the server backup folder into `~/Documents/dental/nudentic-backups/` and prints the list. A good file is larger than a few kilobytes (the first successful dump was 574K).
|
||||
|
||||
---
|
||||
|
||||
|
||||
Reference in New Issue
Block a user