Back up and restore DataClarity Postgresql database
You can back up and restore the DataClarity database either by copying the postgres pvc folder or by using pg_dump for the PostgreSQL database. The latter method is recommended if you are migrating the database to another server.
Procedure
-
Create a backup:
-
Connect to the VM where the DataClarity platform is installed, and then go to the microk8s default storage location:
Copycd /var/snap/microk8s/common/default-storage
The folder containing the postgres data starts with ‘default-postgres-pv-claim-pvc’.
Note: To access, copy, or delete the folder, you will likely need to switch to the superuser (root). To switch to the superuser, find the corresponding command for your OS. For example, the
sudo su
command is used in Ubuntu. -
Create a copy of the folder with the postgres pv, where
/home/user/pgbackup
is the location of the new copy that you can change as needed:Copycp -a default-postgres-pv-claim-pvc-910fec3c-89f8-4191-86ab-fd5d587ec7d5/ /home/user/pgbackup/default-postgres-pv-claim-pvc-910fec3c-89f8-4191-86ab-fd5d587ec7d5
Note: In the above example, the folder name is default-postgres-pv-claim-pvc-910fec3c-89f8-4191-86ab-fd5d587ec7d5. In your case, it will be different, but it always starts with ‘default-postgres-pv-claim-pvc’.
-
-
Restore the backup:
-
Go to the default storage location and record the name of the folder containing the postgres pv:
Copycd /var/snap/microk8s/common/default-storage
Then, delete the folder with the postgres pv:
Copyrm -rf default-postgres-pv-claim-pvc-910fec3c-89f8-4191-86ab-fd5d587ec7d5
-
Copy the backup folder back into the default storage location:
Copycp -a /home/user/pgbackup/default-postgres-pv-claim-pvc-910fec3c-89f8-4191-86ab-fd5d587ec7d5 /var/snap/microk8s/common/default-storage/default-postgres-pv-claim-pvc-910fec3c-89f8-4191-86ab-fd5d587ec7d5
-
-
Create a backup:
-
Connect to the VM where the DataClarity platform is installed, and then connect to the Postgres container:
Copykubectl exec -it postgres-67fc4f87f8-vwvsf sh
Note: The postgres container has a different name each time, but it always starts with ‘postgres’. While entering the command, after typing ‘post’, press the Tab key to autocomplete the container name.
-
Create a DB backup using pg_dump, where ‘dc_db’ is the main database and ‘dc_db_bk.sql’ is the filename of the backup, which you can change as needed:
Copypg_dump -d dc_db -U postgres -p 5432 -f dc_db_bk.sql
-
Exit the container:
Copyexit
-
Copy the backup file to a place outside of the docker container:
Copykubectl cp postgres-67fc4f87f8-vwvsf:/dc_db_bk.sql /home/user/pgbackup/dc_db_bk.sql
Important: Do not skip this step because the backup file will be lost if, for any reason, the docker container is recreated.
-
-
Restore the backup:
-
Copy the backup file inside the postgres container:
Copykubectl cp /home/user/pgbackup/dc_db_bk.sql postgres-67fc4f87f8-vwvsf:/dc_db_bk.sql
-
Connect to the postgres container:
Copykubectl exec -it postgres-67fc4f87f8-vwvsf sh
-
Connect to the PostgreSQL server:
Copypsql -U postgres
-
Terminate all the database connections, and then drop the dc_db database:
CopySELECT pg_terminate_backend (pid) FROM pg_stat_activity WHERE datname = 'dc_db';
CopyDROP DATABASE dc_db;
Note: If you get an error stating that the dc_db database is being accessed by other users, run the above command to terminate the connections again, immediately followed by the drop command.
-
Create the dc_db database:
CopyCREATE DATABASE dc_db;
-
Close the PostgreSQL connection:
Copyexit
-
Restore the database backup:
Copypsql -U postgres -d dc_db -1 -f dc_db_bk.sql
-
Comments
0 comments