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:
Script.
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
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.
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.
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.
Create the archives folder if it doesn’t exist:
sudo mkdir /var/lib/ibatyr/archives
Create a symbolic link to the new folder:
cd /var/lib/ibatyr/archives sudo ln -s /mnt/separate_disk/<archive_name> <archive_name>
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
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 trueWhere:
-dpoints to the database from which you want to delete data;
-ndefines the number of shards, the default value is 3;
-ospecifies the order in which shards are deleted.ASCapplies ascending order from oldest data to the beginning of the current year (default);
-mdwhen specifyingtruemarks the database specified in the-dparameter 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 orderASCis applied automatically.
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 DESCwhere
-o DESCmeans 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.
Delete data from PostgreSQL and ClickHouse archive and main database:
./bin/db/experimental/cleanup_archive_dataDone! When you log into iBatyr Monitor, you will see a new archive in the list of databases:
![]()
Preparing for archiving
Make sure that the disk (separate disk) has enough free space - about 150% of the current database.
Note
We recommend storing the archive database on a separate disk. This will simplify working with it and speed up working with the main database.
Stop iBatyr Monitor services:
sudo service ibatyr stop sudo service nginx stop
Check that services have stopped. To do this, run:
ibatyr top ibatyr ps Ни одна из этих команд не должна выводить список процессов.
If you are in doubt about the amount of free space available:
4.1 Open the compact script file, which is part of iBatyr Monitor:
sudo nano /usr/share/ibatyr/bin/compact
4.2 In the opened file, uncomment the lines
echo "Vacuum database"andsudo -u postgres psql ibatyr -c "vacuum full". The final file will look like this:#!/bin/bash # USE compact # HELP Cleanup unreffered dimensions and vacuum database ibatyr stop echo "Cleanup dimensions" cat sql/cleanup_dimensions.sql | bin/sql echo "Vacuum database" sudo -u postgres psql ibatyr -c "vacuum full" ibatyr start echo "Cleanup done" bin/info
4.3 Save changes.
4.4 After cleaning the database is complete, run:
ibatyr compact
Note
This command will significantly reduce the amount of memory the database takes up.
Creating an archive database
Note
If you do not use ClickHouse, then go to Creating a new database.
To avoid losing data in ClickHouse, follow these steps:
Login to ClickHouse:
ibatyr clickhouse
Rename the current database:
RENAME DATABASE ibatyr TO archive_2022; Здесь *archive_2022* — имя архивной базы данных.
Create a new main database in ClickHouse:
ibatyr clickhouse reinit
After this, proceed to creating a new database.
Creating a new database
Note
For ease of use, use simple names based on archive dates as the name of the new database and TABLESPACE. For example: archive_2022.
Create a folder with permissions for PostgreSQL. It is advisable to do this on a separate medium:
sudo mkdir /mnt/separate_disk/archive_2022 sudo chmod 700 /mnt/separate_disk/archive_2022 sudo chown postgres:postgres /mnt/separate_disk/archive_2022
Create the archives folder if it doesn’t exist:
sudo mkdir /var/lib/ibatyr/archives
Create a symbolic link to the new folder:
cd /var/lib/ibatyr/archives sudo ln -s /mnt/separate_disk/archive_2022 archive_2022
Create TABLESPACE. To do this, log into the PostgreSQL management console:
sudo -u postgres psqland run the command:
CREATE TABLESPACE archive_2022 LOCATION '/var/lib/ibatyr/archives/archive_2022';
Copy the current database to the archive one:
CREATE DATABASE archive_2022 TEMPLATE ibatyr TABLESPACE archive_2022;
Remove shards from the table before the current year. To open the list of shards, exit PostgreSQL:
\qOpen the list:
ibatyr shard listReturn to PostgreSQL:
sudo -u postgres psql -d ibatyror
ibatyr sqlDelete shards by substituting the name in <table name>:
drop table <название таблицы>;
Now the current database has been completely copied to the archive one. Remove everything from the current iBatyr Monitor database that was transferred to the archive:
delete from agent_event where local_time < '2023-01-01 00:00:00'; # в нашем случае оставляем только 2023-й год # Очистите измерения delete from agent_web where id not in (select distinct web_data_id from agent_event where web_data_id IS NOT NULL); delete from agent_time where id not in (select distinct time_id from agent_event where time_id IS NOT NULL); delete from agent_account where id not in (select distinct account_id from agent_event where account_id IS NOT NULL); delete from agent_appinstallation where id not in (select distinct app_installation_id from agent_event where app_installation_id IS NOT NULL); delete from agent_application where id not in (select distinct application_id from agent_event where application_id IS NOT NULL); delete from agent_networkconnection where id not in (select distinct net_data_id from agent_event where net_data_id IS NOT NULL); delete from agent_dialog where id not in (select distinct dialog_id from agent_event where dialog_id IS NOT NULL); delete from agent_device where id not in (select distinct device_id from agent_event where device_id IS NOT NULL); delete from agent_attachedfile where not exists (select 1 from agent_event where agent_event.attached_file_id=agent_attachedfile.id) and agent_attachedfile.category is null; delete from agent_filepath where not exists (select 1 from agent_event where agent_event.file_path_id = agent_filepath.id); delete from analytics_session where local_time < '2023-01-01 00:00:00'; delete from analytics_filter_events where not exists (select 1 from agent_event where agent_event.id=analytics_filter_events.event_id); # Выйдите из PostgreSQL \q # Завершите сессии в iBatyr Monitor ibatyr reset_extended_sessions
Return to PostgreSQL using one of the commands:
sudo -u postgres psql -d ibatyror
ibatyr sql
Optimize the operation of the database (the procedure may take a lot of time):
ANALYZE;
Add the archive database to the configuration file etc/ibatyr/config in the
DATABASESsection:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'ibatyr', 'USER': 'ibatyr', 'PASSWORD': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'HOST': '', 'PORT': '', }, 'archive_2022': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'archive_2022', 'USER': 'ibatyr', 'PASSWORD': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', # пароль такой же, как у базы данных default 'HOST': '', 'PORT': '', } }
Ready! Now you can check that the transfer was successful.
To do this, start the iBatyr Monitor server:
ibatyr start
Open the server web interface. If everything went well, you will be able to select a database in the login form.
Final design of the archival database
After checking the availability of both databases, complete the design of the archive database. To do this:
Stop iBatyr Monitor services:
sudo service ibatyr stop sudo service nginx stop
Check that services have stopped. To do this, run:
ibatyr top ibatyr psNone of these commands should list processes.
Clear the PostgreSQL archive database of unnecessary data:
ibatyr sql ibatyr=>\c archive_2022 Вы подключены к базе данных "archive_2022" как пользователь "ibatyr". archive_2022=>Warning
Before performing the next operation, double check that you are connected to the archive database!
Delete all shards of the current year using the command:
drop table <название таблицы>;
Delete data:
delete from agent_event where local_time >= '2023-01-01 00:00:00'; # Убираем 2023-й год # Очищаем измерения delete from agent_web where id not in (select distinct web_data_id from agent_event where web_data_id IS NOT NULL); delete from agent_time where id not in (select distinct time_id from agent_event where time_id IS NOT NULL); delete from agent_account where id not in (select distinct account_id from agent_event where account_id IS NOT NULL); delete from agent_appinstallation where id not in (select distinct app_installation_id from agent_event where app_installation_id IS NOT NULL); delete from agent_application where id not in (select distinct application_id from agent_event where application_id IS NOT NULL); delete from agent_networkconnection where id not in (select distinct net_data_id from agent_event where net_data_id IS NOT NULL); delete from agent_dialog where id not in (select distinct dialog_id from agent_event where dialog_id IS NOT NULL); delete from agent_device where id not in (select distinct device_id from agent_event where device_id IS NOT NULL); delete from agent_attachedfile where not exists (select 1 from agent_event where agent_event.attached_file_id=agent_attachedfile.id) and agent_attachedfile.category is null; delete from agent_filepath where not exists (select 1 from agent_event where agent_event.file_path_id = agent_filepath.id); delete from analytics_session where local_time >= '2023-01-01 00:00:00'; delete from analytics_session_extended where local_time >= '2023-01-01 00:00:00'; delete from analytics_filter_events where not exists (select 1 from agent_event where agent_event.id=analytics_filter_events.event_id);
Optimize the operation of the database (the procedure may take a lot of time):
ANALYZE;
Fill in the ClickHouse archive with data:
OVERRIDE_DBNAME=archive_2022 ibatyr clickhouse reinit OVERRIDE_DBNAME=archive_2022 ibatyr clickhouse pump
Ready! You can launch the iBatyr Monitor service and check the operation of the 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