The steps cover taking a nudentic.ir dump, copying it off the server, loading it into local Postgres, and restoring production in an emergency.
7.3 KiB
Manual database backup (nudentic.ir)
Copy-paste runbook for the production Postgres database. Use it when you need a dump in a hurry, a copy of production data on your Mac, or a restore after something has gone wrong.
A dump is one file that contains the dyolink_db tables: users, patients, appointments, treatments, lab cases, billing, and so on. Taking a dump does not stop the site. Postgres reads a consistent snapshot while the app keeps running.
The dump does not contain photo or case-file bytes. Those files live on disk under uploads/. The database only stores the file name and the path. After a restore, the case row is back; opening the file still needs the file on disk.
Names
| What | Value |
|---|---|
| SSH | dyolink@185.243.48.140 |
| Server directory | /opt/dyolink/infrastructure |
| Production Postgres container | dyolink_db_prod |
| Database | dyolink_db |
| Dump folder on the server | /opt/dyolink/infrastructure/database/backups |
| Same folder inside the container | /backups |
| Copy on the Mac | ~/Documents/dental/nudentic-backups/ |
| Local Postgres container | dyolink-postgres-dev |
The container already has POSTGRES_USER and POSTGRES_DB. Do not type the password, and do not print database.env.
Keep dump files private. They contain patient and account data. Do not commit them, and do not put them inside the git repo. ~/Documents/dental/nudentic-backups/ is outside the repo.
Do not
- Do not run
infrastructure/scripts/manage.sh backup. It uses-U postgres. Production has nopostgresrole; the user isdyolink_user. - Do not run
docker volume rm dyolink_postgres_data_prodordocker volume prune. That deletes the live database. - Do not run the
scpcopy while your prompt isdyolink@srv…. That copies the file onto the server again.scpruns on the Mac, where the prompt isrameen@….
1. Take a backup
The site stays up. SSH in:
ssh dyolink@185.243.48.140
On the server (dyolink@srv…):
cd /opt/dyolink/infrastructure
mkdir -p database/backups
docker exec dyolink_db_prod sh -c \
'pg_dump -U "$POSTGRES_USER" -d "$POSTGRES_DB" -Fc -f /backups/nudentic_$(date +%Y%m%d_%H%M%S).dump'
ls -lh database/backups
docker exec dyolink_db_prod sh -c 'pg_restore -l /backups/nudentic_*.dump | head -40'
ls -lh must be run from /opt/dyolink/infrastructure, or use ls -lh with no extra path if you are already inside database/backups.
A good dump is larger than a few kilobytes. The pg_restore -l list should name dbname: dyolink_db and tables such as patients and lab_cases. A file of a few bytes means the dump failed.
Leave this SSH session open if you still need it. The next step is a new terminal on the Mac.
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).
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/
It asks for the server password. When it finishes:
ls -lh ~/Documents/dental/nudentic-backups
The size should match the file on the server (the first successful dump was 574K).
3. Load the dump into the local database
This deletes the current local dyolink_db and replaces it with the dump. Production is not touched.
Stop the local API first (npm run start:dev in backend/). Postgres itself stays up.
From backend/, start local Postgres if it is not already running:
cd /Users/rameen/Documents/dental/dyolink/backend
docker compose -f docker-compose.postgres.yml up -d
The container name is dyolink-postgres-dev. DATABASE_URL in backend/.env should already point at localhost and database dyolink_db.
On the Mac, copy the dump into that container and replace the local database. Change the filename if yours is different.
docker cp ~/Documents/dental/nudentic-backups/nudentic_YYYYMMDD_HHMMSS.dump dyolink-postgres-dev:/tmp/nudentic.dump
docker exec dyolink-postgres-dev sh -c 'psql -U "$POSTGRES_USER" -d postgres -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '\''dyolink_db'\'' AND pid <> pg_backend_pid();"'
docker exec dyolink-postgres-dev sh -c 'dropdb -U "$POSTGRES_USER" --if-exists dyolink_db && createdb -U "$POSTGRES_USER" dyolink_db'
docker exec dyolink-postgres-dev sh -c 'pg_restore -U "$POSTGRES_USER" -d dyolink_db --no-owner --no-acl /tmp/nudentic.dump'
docker exec dyolink-postgres-dev sh -c 'psql -U "$POSTGRES_USER" -d dyolink_db -c "\dt"'
pg_restore may print warnings. If \dt lists tables such as patients, lab_cases, and users, the restore worked.
Start the API and the frontend the way you usually do. Sign in with a production account: the password hashes came with the dump. Local .env secrets stay as they are.
Attachment links will not open files on the Mac, because those files were not in the dump.
4. Emergency: put a dump back on production
This replaces the live nudentic.ir database. Everything saved after that dump’s timestamp is gone. Run this only when you have decided the current production data should be discarded.
SSH in and go to the infrastructure directory:
ssh dyolink@185.243.48.140
cd /opt/dyolink/infrastructure
4a. Save the database you are about to replace. If the restore is wrong, this file is the way back.
docker exec dyolink_db_prod sh -c \
'pg_dump -U "$POSTGRES_USER" -d "$POSTGRES_DB" -Fc -f /backups/nudentic_before_restore_$(date +%Y%m%d_%H%M%S).dump'
ls -lh database/backups
4b. If the dump you want is only on the Mac, send it to the server first. Run this on the Mac, not inside SSH:
scp ~/Documents/dental/nudentic-backups/nudentic_YYYYMMDD_HHMMSS.dump \
dyolink@185.243.48.140:/opt/dyolink/infrastructure/database/backups/
4c. Stop the API so it cannot write during the restore. Postgres stays running.
docker compose -f docker-compose.prod.yml --env-file .env stop backend
4d. Load the chosen dump. Replace the filename with the dump you intend to keep (not the before_restore file, unless that is the one you want).
docker exec dyolink_db_prod sh -c 'psql -U "$POSTGRES_USER" -d postgres -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '\''dyolink_db'\'' AND pid <> pg_backend_pid();"'
docker exec dyolink_db_prod sh -c 'dropdb -U "$POSTGRES_USER" --if-exists dyolink_db && createdb -U "$POSTGRES_USER" dyolink_db'
docker exec dyolink_db_prod sh -c 'pg_restore -U "$POSTGRES_USER" -d dyolink_db --no-owner --no-acl /backups/nudentic_YYYYMMDD_HHMMSS.dump'
4e. Start the API again.
docker compose -f docker-compose.prod.yml --env-file .env start backend
On startup the API runs prisma migrate deploy and the reference-data seed. That brings the schema in line with the running app if the app is newer than the dump. It does not delete the restored patients and cases.
4f. Check the site. Open https://nudentic.ir/api/health, then sign in and open a case you know was in that dump.
Case rows come back with the dump. Photo and case files on the server disk are left as they are. A database restore does not roll those files backward or forward.