From 6981a18ecfcda09bf9e0036c5832677a3440b695 Mon Sep 17 00:00:00 2001 From: Potenz Date: Sun, 5 Oct 2025 04:44:08 +0530 Subject: [PATCH] feat(parallel-backupfile-added)-v4 --- packages/db/docs/migration.md | 39 ++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/packages/db/docs/migration.md b/packages/db/docs/migration.md index a75a05d..4137023 100644 --- a/packages/db/docs/migration.md +++ b/packages/db/docs/migration.md @@ -7,6 +7,7 @@ This document explains how to **restore the DentalApp PostgreSQL database** on a ## ๐Ÿงฉ Overview You will: + 1. Create a PostgreSQL backup on the **main PC** 2. Copy it to the **target PC** 3. Restore it cleanly using `pg_restore` @@ -16,12 +17,14 @@ You will: ## โš™๏ธ Prerequisites Before starting: + - PostgreSQL is installed on both machines (same major version recommended) - The app is **not running** during restore - You know the database credentials - *(from `.env` or environment variables)* + _(from `.env` or environment variables)_ Example: + ```bash DATABASE_URL=postgresql://postgres:mypassword@localhost:5432/dentalapp ``` @@ -29,19 +32,23 @@ DATABASE_URL=postgresql://postgres:mypassword@localhost:5432/dentalapp ## ๐Ÿงญ Steps to Restore Database on Another PC ### ๐Ÿ–ฅ๏ธ Step 1 โ€” Create Backup on Main PC + - Generate the backup.dump file from the backup page from the app. ### Step 2 โ€” Copy Backup File to Target PC + - Transfer the backup file to the second PC using USB, network share, cloud, or SCP. = Example destination path: C:\db_backups\backup.dump ### ๐Ÿงน Step 3 โ€” Prepare the Target PC + - Stop the DentalApp application to avoid database locks. - Ensure PostgreSQL is installed and running. - (Optional but recommended) Drop the existing database: + ``` PGPASSWORD='mypassword' dropdb -U postgres -h localhost dentalapp ``` @@ -49,15 +56,17 @@ PGPASSWORD='mypassword' dropdb -U postgres -h localhost dentalapp ### โ™ป๏ธ Step 4 โ€” Restore the Database # Case1: when we got a zip folder. + -linux bash # 4.1) unzip to a directory + ``` unzip backup.zip -d /tmp/dental_dump_dir ``` - # 4.2) restore into an already-created DB named 'dentalapp' + ``` PGPASSWORD='mypassword' createdb -U postgres -h localhost -O postgres dentalapp # optional PGPASSWORD='mypassword' pg_restore -U postgres -h localhost -d dentalapp -j 4 /tmp/dental_dump_dir @@ -67,15 +76,19 @@ PGPASSWORD='mypassword' /usr/lib/postgresql/17/bin/pg_restore -v -U postgres -h ``` # Case2: when we got a tar folder. + -linux bash # 4.1) unzip to a directory -``` -unzip backup.zip -d /tmp/dental_dump_dir -``` +``` +# create target dir and extract +mkdir -p /tmp/dental_dump_dir +tar -xzf backup.tar.gz -C /tmp/dental_dump_dir +``` # 4.2) restore into an already-created DB named 'dentalapp' + ``` PGPASSWORD='mypassword' createdb -U postgres -h localhost -O postgres dentalapp # optional PGPASSWORD='mypassword' pg_restore -U postgres -h localhost -d dentalapp -j 4 /tmp/dental_dump_dir @@ -84,10 +97,10 @@ or PGPASSWORD='mypassword' /usr/lib/postgresql/17/bin/pg_restore -v -U postgres -h localhost -C -d postgres /tmp/dental_dump_dir ``` - ### โœ… Step 5 โ€” Verify the Restore - Check that the tables are restored successfully: + ``` PGPASSWORD='mypassword' psql -U postgres -h localhost -d dentalapp -c "\dt" ``` @@ -97,6 +110,7 @@ PGPASSWORD='mypassword' psql -U postgres -h localhost -d dentalapp -c "\dt" ### ๐Ÿงฉ Step 6 โ€” Update App Configuration - Ensure the .env file on the target PC points to the correct database: + ``` DATABASE_URL=postgresql://postgres:mypassword@localhost:5432/dentalapp ``` @@ -107,22 +121,22 @@ DATABASE_URL=postgresql://postgres:mypassword@localhost:5432/dentalapp - Use the same PostgreSQL version as the main PC. - - For large databases, use parallel restore for speed: + ``` pg_restore -U postgres -j 4 -d dentalapp backup.dump ``` - Always keep at least one recent backup archived safely. - -# If such error came: +# If such error came: - pg_restore: error: unsupported version (1.16) in file header - use cmd: -- 1) Add PGDG (official PostgreSQL) APT repo and its key, then update and install client-17 +- 1. Add PGDG (official PostgreSQL) APT repo and its key, then update and install client-17 + ``` sudo apt update && sudo apt install -y wget ca-certificates gnupg lsb-release curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /usr/share/keyrings/pgdg.gpg @@ -131,7 +145,8 @@ sudo apt update sudo apt install -y postgresql-client-17 ``` -- 2) Run pg_restore from the installed v17 binary (replace password as needed) +- 2. Run pg_restore from the installed v17 binary (replace password as needed) + ``` PGPASSWORD='mypassword' /usr/lib/postgresql/17/bin/pg_restore -v -U postgres -h localhost -C -d postgres ./backup.dump -``` \ No newline at end of file +```