Creating an archive database

If the performance of the iBatyr Monitor server deteriorates due to the large amount of accumulated data, you can divide the current database into two: current and archived. You can leave data for the current calendar year in the current one, and put data for the entire previous operating time in the archived one.

Note

Starting from version 5.6, the script supports creating an archive database for the main database located on another host. See parameter [DATABASE HOST] in paragraph 6 of the instructions.

The result of performing all operations in this instruction will be:

  • two databases:

    • archival, which will contain data from year xxxx to last year inclusive;

    • current, in which data for the current year will be stored;

  • ability to select a database to view.

Warning

Before creating an archive database, make a backup of the database. Backup ensures data recovery if lost.

Warning

Before archiving the database, close old incidents or disable incident protection. If deletion protection is enabled and there are open incidents for the current year, shards associated with these incidents will not be deleted from the archive database.

There are two ways to create a data archive:

  1. Script.

  2. Teams.

Note

The script speeds up the work, but is at the testing and development stage. For any questions, please contact support for the latest information.

Preparing for archiving

  1. Make sure your disk has about 150% free space of the current database size.

Note

If there is not enough available space, use the compact script from step 4 of the instructions in the next tab.

  1. If you use ClickHouse, create a ClickHouse database in it according to the instructions in the Creating an archive database section in the next tab.

  2. Create an archive folder with PostgreSQL permissions. It is advisable to do this on a separate medium:

sudo mkdir /mnt/separate_disk/<archive_name>
sudo chmod 700 /mnt/separate_disk/<archive_name>
sudo chown postgres:postgres /mnt/separate_disk/<archive_name>

where <archive_name> is the name of the archive database.

Note

Use simple names for folders and databases based on archive dates. For example, archive_2022.

  1. Create the archives folder if it doesn’t exist:

sudo mkdir /var/lib/ibatyr/archives
  1. Create a symbolic link to the new folder:

cd /var/lib/ibatyr/archives
sudo ln -s /mnt/separate_disk/<archive_name> <archive_name>
  1. Create a new tablespace and copy the database into it:

bash /usr/share/ibatyr/bin/db/experimental/copy_db_to_tablespace <ARCHIVE NAME> [DATABASE HOST] [--remote]

Where:

<ARCHIVE NAME> — name of the new archive database;

[DATABASE HOST] - Specify this parameter only if the database is on another host.

[--remote] is a flag that indicates the need for a remote connection to the database.

Note

The script is experimental and requires operator attention. Upon completion of the work, you will be prompted to create an archive Clickhouse database. This process takes a long time, but no longer requires attention. Copying a 13 GB database takes 39 minutes, copying a 3.3 TB database takes 4 hours.

Deleting obsolete data

  1. Run a script that removes shards (months) from the main database that precede the current year:

bash /usr/share/ibatyr/bin/db/experimental/drop_shards -d <MAIN DB NAME> [-n 100 (default 3)] [-o ASC (default)] -md true

Where:

-d points to the database from which you want to delete data;

-n defines the number of shards, the default value is 3;

-o specifies the order in which shards are deleted. ASC applies ascending order from oldest data to the beginning of the current year (default);

-md when specifying true marks the database specified in the -d parameter as the main one, which affects the receipt of the list of shards to delete (false by default);

Example: To erase the previous two years, enter -n 24 -md true. The order ASC is applied automatically.

  1. Delete the current year’s shards from the archive database:

bash /usr/share/ibatyr/bin/db/experimental/drop_shards -d <ARCHIVE NAME> [-n 100 (default 3)] -o DESC

where -o DESC means you delete data in descending order from the last month to the first month of the current year. For example, to delete all shards of the current year, specify -n 12 -o DESC.

  1. Delete data from PostgreSQL and ClickHouse archive and main database:

./bin/db/experimental/cleanup_archive_data

Done! When you log into iBatyr Monitor, you will see a new archive in the list of databases:

Illustration 1 — Creating an archive database

Results of the work

The result of correct execution of all operations will be:

  • main and archive databases available for selection when logging into the web interface;

  • the ability for ClickHouse to work with all databases;

  • Policies will be executed in the main database, but not in the archive database.

Modification date: 10.07.2024