nav-img
Advanced

Restoring from Full Backup Files to a Self-Built PostgreSQL Database

You can download backup files by referring to Downloading a Full Backup and restore data from them.

Prerequisites

Decompression with tools:

  1. A tool for decompressing TAR files must be installed in a Unix-like system.
  2. Either python2.7 or python3 can be used.
  3. The following third-party packages need to be installed: lz4, gzip, and shutil.
  4. The version of the local database must be the same as that of the cloud database.
  5. Extensions of the same version as the cloud database are installed in the local database.

Procedure (Decompression Using Commands) (Recommended)

  1. Prepare a local directory for storing tablespaces.
  2. Stop the local database server.
  3. Store the following configuration files in the data directory of the local database to another directory: postgresql.conf, pg_hba.conf, and recovery.done.
  4. Clear the data directory of the local database.
  5. Run the following command to decompress backup files to the directory prepared in 1:

    tar -zxf src_file -C target_dir

    Variables in the command are described as follows:

    src_file: indicates the directory where PostgreSQL full backup files are stored.

    target_dir: indicates the directory to which data is restored and it has been prepared in step 1. The directory must be empty. If the directory does not exist, the system automatically creates it.

    The following directories are generated after the decompression:

    • base: stores full files.
    • pg_wal (if the PostgreSQL version is 9.x, the directory is pg_xlog): stores incremental files.
    • A number of tablespace directories named using digits (if the original backup contains tablespace files).

  6. Copy the files in 5 to a specified directory of the local database.

    1. Copy all the decompressed files in the base directory to the data directory of the local database, and then replace the three files in the data directory of the local database with the configuration files in step 3.
    2. Copy the files in the decompressed pg_wal directory (if the PostgreSQL version is 9.x, the directory is pg_xlog) to the pg_wal (or pg_xlog directory) under the data directory of the local database.
    3. If the original backup contains tablespace files, modify the corresponding tablespace soft links in data\tablespace_map.

  7. Reboot the database and wait until the database restoration is complete.

Procedure (Decompression with Tools)

  1. Prepare a local directory for storing tablespaces.
  2. Stop the local database server.
  3. Store the following configuration files in the data directory of the local database to another directory: postgresql.conf, pg_hba.conf, and recovery.done.
  4. Clear the data directory of the local database.
  5. Download the decompression tool.
  6. Run the following command to decompress backup files to the directory prepared in Step 1:

    python restore_obs_file.py src_file target_dir

    Variables in the command are described as follows:

    src_file: indicates the directory where PostgreSQL full backup files are stored.

    target_dir: indicates the directory to which data is restored. The directory has been prepared in 1. The directory must be empty. If the directory does not exist, the system automatically creates it.

    The following directories are generated after the decompression:

    • data: stores full backup data. The recovery.done file has been deleted.
    • xlog: stores incremental backup data.
    • tblspc: stores tablespace directory files (if the original backup contains tablespace files).

  7. Copy the files in 6 to the specified directory of the local database.

    1. Copy all the decompressed files in the data directory to the data directory of the local database, and then replace the three files in the data directory of the local database with the configuration files saved in step 3.
    2. Copy the decompressed files in the xlog directory to the pg_xlog or pg_wal folder in the data directory of the local database. (The folder names vary according to database versions.)
    3. Move the decompressed tablespace folder (if any) in the tblspc directory to the tablespace directory created in step 1 and modify the soft link of the corresponding tablespace in data\tablespace_map.

  8. Reboot the database and wait until the database restoration is complete.