From af4aa7289433f0485d71b16eb44e88ab6d07031e Mon Sep 17 00:00:00 2001 From: Arno Kaimbacher Date: Fri, 21 Apr 2023 15:38:57 +0200 Subject: [PATCH] - describe db backup and restore --- DisasterManagement.md | 54 +++++++++++++++++++++++++++++++++++++++++++ db_backup.drawio.svg | 24 ++++++++----------- 2 files changed, 64 insertions(+), 14 deletions(-) diff --git a/DisasterManagement.md b/DisasterManagement.md index d11dadc..5b301ec 100644 --- a/DisasterManagement.md +++ b/DisasterManagement.md @@ -157,3 +157,57 @@ worstcase: Restore whole data disk, which is mounted into /storage, and all of i One of the common backup and restore configurations is an environment with two SQL Servers (SQLServer-1 and SQLServer-2), two SQL Server instances (SQLInstance-1 and SQLInstance-2), and one database named SQLDB-1 which will be backed up, transferred, and restored to SQLServer-2. ![Image](./db_backup.drawio.svg) +
+
+## Create DB backup + +To create a backup of a research database using pg_dump, you can follow these steps: + +1. Open a terminal or command prompt and navigate to the directory where you want to store the backup file. +2. Type the following command to create a backup of the database: + + ```bash + pg_dump -U -E UTF8 -F c -b -v -f .backup + ``` + + Here, replace **username** with the username of the database, **backup_file_name** with the name you want to give to the backup file, and **database_name** with the name of the research database you want to backup. + + -E is for the encoding, e.g. UTF8 + -F c specifies the format of the backup as a custom format, which is more flexible than plain SQL text format. \ + -b includes the backup of the objects such as functions and procedures. \ + -v enables verbose mode which will display the progress of the backup process. \ + + Usually the timestamp is integrated into the backup_file_name: + + ```bash + pg_dump -U -F c -b -v -f _$(date +"%Y%m%d_%H%M%S").backup + e.g.: + pg_dump -U tethys_user -E UTF8 -F c -b -v -f /home/user/backups/tethys_$(date +"%Y%m%d").backup tethys + ``` + +3. When you run the command, you will be prompted to enter the password for the database user. Enter the password and press enter. +4. The pg_dump command will start creating the backup file. Wait until it completes, which may take several minutes depending on the size of the database. + +5. Once the backup is complete, you will see a message indicating that the backup has been created successfully. + +It's important to note that the backup file may contain sensitive information, so it should be stored securely and only accessible to authorized personnel. +
+
+## Restore DB on second DB-Server + +To restore a backup of the research database using pg_restore, you can follow these steps: + +1. Open a terminal or command prompt and navigate to the directory where the backup file is located. +2. Type the following command to restore the backup to the database: + ```bash + pg_restore -U -d + ``` + Here, replace with the username of the database, with the name of the research database you want to restore the backup to, and with the name of the backup file. +3. When you run the command, you will be prompted to enter the password for the database user. Enter the password and press enter. +4. The pg_restore command will start restoring the backup file to the database. Wait until it completes, which may take several minutes depending on the size of the database. + +e.g. +``` +pg_restore -h localhost -p 5432 -U tethys_user -d tethys_db -v /tmp/tethys_20230404.backup +``` +Once the restore process is complete, the research database should be fully recovered with all its data and objects. diff --git a/db_backup.drawio.svg b/db_backup.drawio.svg index a1e9110..7c20c60 100644 --- a/db_backup.drawio.svg +++ b/db_backup.drawio.svg @@ -1,24 +1,20 @@ - - - - + + -
+
- SQL Server-1 + PostgreSQL Server-1
- - SQL Server-1 + + PostgreSQL Server-1 @@ -60,16 +56,16 @@ -
+
- SQL Server-2 + PostgreSQL Server-2
- - SQL Server-2 + + PostgreSQL Server-2