A. Activate pgaudit
correlation https://teguhth.blogspot.com/2024/02/enable-pgaudit-pgauditlogtofile-in.html
B. convert to table
correlation https://teguhth.blogspot.com/2024/04/convert-pgaudit-pgauditlogtofile-log.html
C. Testing sample CRUD Activity (sample database 'dwh')
1. create table pembelian;
create table barang(
KODE_BARANG char(6) not null ,
NAMA_BARANG varchar(25),
SATUAN_BARANG varchar(20),
STOK_BARANG decimal(4),
primary key (KODE_BARANG));
2. insert ELK-01
insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-01','RICE COOKER','BUAH',20);
3. insert ELK-02
insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-02','LEMARI ES','UNIT',8);
4. insert ELK-03
insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-03','TELEVISI','UNIT',30);
5. delete ELK-02
delete from barang where kode_barang='ELK-02';
select * from barang;
6. insert ELK-04
insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-04','RADIO/TAPE','BUAH',35);
7. update ELK-03
UPDATE barang SET stok_barang = 99 WHERE kode_barang = 'ELK-03';
select * from barang;
8. insert ELK-05
insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-05','KOMPUTER','UNIT',28);
9. add column keterangan in table barang
ALTER TABLE barang ADD COLUMN keterangan VARCHAR(100);
select * from barang;
10. insert ELK-06
insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-06','KIPAS ANGIN','BUAH',38);
11. update keterangan
UPDATE barang SET keterangan = 'hologram' WHERE kode_barang = 'ELK-03';
select * from barang;
correlation https://teguhth.blogspot.com/2024/02/enable-pgaudit-pgauditlogtofile-in.html
B. convert to table
correlation https://teguhth.blogspot.com/2024/04/convert-pgaudit-pgauditlogtofile-log.html
C. Testing sample CRUD Activity (sample database 'dwh')
1. create table pembelian;
create table barang(
KODE_BARANG char(6) not null ,
NAMA_BARANG varchar(25),
SATUAN_BARANG varchar(20),
STOK_BARANG decimal(4),
primary key (KODE_BARANG));
2. insert ELK-01
insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-01','RICE COOKER','BUAH',20);
3. insert ELK-02
insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-02','LEMARI ES','UNIT',8);
4. insert ELK-03
insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-03','TELEVISI','UNIT',30);
5. delete ELK-02
delete from barang where kode_barang='ELK-02';
select * from barang;
6. insert ELK-04
insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-04','RADIO/TAPE','BUAH',35);
7. update ELK-03
UPDATE barang SET stok_barang = 99 WHERE kode_barang = 'ELK-03';
select * from barang;
8. insert ELK-05
insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-05','KOMPUTER','UNIT',28);
9. add column keterangan in table barang
ALTER TABLE barang ADD COLUMN keterangan VARCHAR(100);
select * from barang;
10. insert ELK-06
insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values('ELK-06','KIPAS ANGIN','BUAH',38);
11. update keterangan
UPDATE barang SET keterangan = 'hologram' WHERE kode_barang = 'ELK-03';
select * from barang;
D. convert to table
1. login to dbatools
delete from pgaudit_log;
COPY pgaudit_log FROM '/var/lib/pgsql/16/data/log/pgaudit-teguhth.log' DELIMITER ',' CSV;
select * from pgaudit_log;
2. check list pgaudit_log
select * from pgaudit_log;
E. scheduler for backup
[postgres@teguhth outputdb]$ crontab -l
0 * * * * /bin/bash /var/lib/pgsql/script/backup_inc_edb.sh >> /var/lib/pgsql/script/backup_inc_edb.log
[postgres@teguhth outputdb]$
F. pgaudit sample triger to table
INSERT INTO "pgaudit_log" ("log_time", "user_name", "dbname", "process_id", "connection_from", "session_id", "session_line_num", "command_tag", "session_start_time", "virtual_transaction_id", "transaction_id", "error_severity", "sql_state_code", "message", "detail", "hint", "internal_query", "internal_query_pos", "context", "query", "query_pos", "location", "application_name", "backend_type", "leader_pid", "query_id", "custom_1", "custom_2", "custom_3", "console") VALUES ('2025-09-09 10:56:30.355 WIB', 'postgres', 'dwh', '2376', '[local]', '68bfa54e.948', '4', 'CREATE TABLE', '2025-09-09 10:55:58 WIB', '3/3', '902', '00000', 'SESSION', '2', '1', 'DDL', 'CREATE TABLE', NULL, NULL, 'create table barang(
KODE_BARANG char(6) not null ,
NAMA_BARANG varchar(25),
SATUAN_BARANG varchar(20),
STOK_BARANG decimal(4),
primary key (KODE_BARANG));', '<not logged>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'psql');
INSERT INTO "pgaudit_log" ("log_time", "user_name", "dbname", "process_id", "connection_from", "session_id", "session_line_num", "command_tag", "session_start_time", "virtual_transaction_id", "transaction_id", "error_severity", "sql_state_code", "message", "detail", "hint", "internal_query", "internal_query_pos", "context", "query", "query_pos", "location", "application_name", "backend_type", "leader_pid", "query_id", "custom_1", "custom_2", "custom_3", "console") VALUES ('2025-09-09 10:56:59.540 WIB', 'postgres', 'dwh', '2376', '[local]', '68bfa54e.948', '5', 'INSERT', '2025-09-09 10:55:58 WIB', '3/4', '0', '00000', 'SESSION', '3', '1', 'WRITE', 'INSERT', 'TABLE', 'public.barang', 'insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values(''ELK-01'',''RICE COOKER'',''BUAH'',20);', '<not logged>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'psql');
INSERT INTO "pgaudit_log" ("log_time", "user_name", "dbname", "process_id", "connection_from", "session_id", "session_line_num", "command_tag", "session_start_time", "virtual_transaction_id", "transaction_id", "error_severity", "sql_state_code", "message", "detail", "hint", "internal_query", "internal_query_pos", "context", "query", "query_pos", "location", "application_name", "backend_type", "leader_pid", "query_id", "custom_1", "custom_2", "custom_3", "console") VALUES ('2025-09-09 10:57:12.666 WIB', 'postgres', 'dwh', '2376', '[local]', '68bfa54e.948', '6', 'INSERT', '2025-09-09 10:55:58 WIB', '3/5', '0', '00000', 'SESSION', '4', '1', 'WRITE', 'INSERT', 'TABLE', 'public.barang', 'insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values(''ELK-02'',''LEMARI ES'',''UNIT'',8);', '<not logged>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'psql');
INSERT INTO "pgaudit_log" ("log_time", "user_name", "dbname", "process_id", "connection_from", "session_id", "session_line_num", "command_tag", "session_start_time", "virtual_transaction_id", "transaction_id", "error_severity", "sql_state_code", "message", "detail", "hint", "internal_query", "internal_query_pos", "context", "query", "query_pos", "location", "application_name", "backend_type", "leader_pid", "query_id", "custom_1", "custom_2", "custom_3", "console") VALUES ('2025-09-09 10:57:20.930 WIB', 'postgres', 'dwh', '2376', '[local]', '68bfa54e.948', '7', 'INSERT', '2025-09-09 10:55:58 WIB', '3/6', '0', '00000', 'SESSION', '5', '1', 'WRITE', 'INSERT', 'TABLE', 'public.barang', 'insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values(''ELK-03'',''TELEVISI'',''UNIT'',30);', '<not logged>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'psql');
INSERT INTO "pgaudit_log" ("log_time", "user_name", "dbname", "process_id", "connection_from", "session_id", "session_line_num", "command_tag", "session_start_time", "virtual_transaction_id", "transaction_id", "error_severity", "sql_state_code", "message", "detail", "hint", "internal_query", "internal_query_pos", "context", "query", "query_pos", "location", "application_name", "backend_type", "leader_pid", "query_id", "custom_1", "custom_2", "custom_3", "console") VALUES ('2025-09-09 10:57:29.099 WIB', 'postgres', 'dwh', '2376', '[local]', '68bfa54e.948', '8', 'DELETE', '2025-09-09 10:55:58 WIB', '3/7', '0', '00000', 'SESSION', '6', '1', 'WRITE', 'DELETE', 'TABLE', 'public.barang', 'delete from barang where kode_barang=''ELK-02'';', '<not logged>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'psql');
INSERT INTO "pgaudit_log" ("log_time", "user_name", "dbname", "process_id", "connection_from", "session_id", "session_line_num", "command_tag", "session_start_time", "virtual_transaction_id", "transaction_id", "error_severity", "sql_state_code", "message", "detail", "hint", "internal_query", "internal_query_pos", "context", "query", "query_pos", "location", "application_name", "backend_type", "leader_pid", "query_id", "custom_1", "custom_2", "custom_3", "console") VALUES ('2025-09-09 10:57:47.745 WIB', 'postgres', 'dwh', '2376', '[local]', '68bfa54e.948', '9', 'SELECT', '2025-09-09 10:55:58 WIB', '3/8', '0', '00000', 'SESSION', '7', '1', 'READ', 'SELECT', 'TABLE', 'public.barang', 'select * from barang;', '<not logged>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'psql');
INSERT INTO "pgaudit_log" ("log_time", "user_name", "dbname", "process_id", "connection_from", "session_id", "session_line_num", "command_tag", "session_start_time", "virtual_transaction_id", "transaction_id", "error_severity", "sql_state_code", "message", "detail", "hint", "internal_query", "internal_query_pos", "context", "query", "query_pos", "location", "application_name", "backend_type", "leader_pid", "query_id", "custom_1", "custom_2", "custom_3", "console") VALUES ('2025-09-09 10:58:02.170 WIB', 'postgres', 'dwh', '2376', '[local]', '68bfa54e.948', '10', 'INSERT', '2025-09-09 10:55:58 WIB', '3/9', '0', '00000', 'SESSION', '8', '1', 'WRITE', 'INSERT', 'TABLE', 'public.barang', 'insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values(''ELK-04'',''RADIO/TAPE'',''BUAH'',35);', '<not logged>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'psql');
INSERT INTO "pgaudit_log" ("log_time", "user_name", "dbname", "process_id", "connection_from", "session_id", "session_line_num", "command_tag", "session_start_time", "virtual_transaction_id", "transaction_id", "error_severity", "sql_state_code", "message", "detail", "hint", "internal_query", "internal_query_pos", "context", "query", "query_pos", "location", "application_name", "backend_type", "leader_pid", "query_id", "custom_1", "custom_2", "custom_3", "console") VALUES ('2025-09-09 10:58:13.056 WIB', 'postgres', 'dwh', '2376', '[local]', '68bfa54e.948', '11', 'UPDATE', '2025-09-09 10:55:58 WIB', '3/10', '0', '00000', 'SESSION', '9', '1', 'WRITE', 'UPDATE', 'TABLE', 'public.barang', 'UPDATE barang SET stok_barang = 99 WHERE kode_barang = ''ELK-03'';', '<not logged>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'psql');
INSERT INTO "pgaudit_log" ("log_time", "user_name", "dbname", "process_id", "connection_from", "session_id", "session_line_num", "command_tag", "session_start_time", "virtual_transaction_id", "transaction_id", "error_severity", "sql_state_code", "message", "detail", "hint", "internal_query", "internal_query_pos", "context", "query", "query_pos", "location", "application_name", "backend_type", "leader_pid", "query_id", "custom_1", "custom_2", "custom_3", "console") VALUES ('2025-09-09 10:58:27.127 WIB', 'postgres', 'dwh', '2376', '[local]', '68bfa54e.948', '12', 'SELECT', '2025-09-09 10:55:58 WIB', '3/11', '0', '00000', 'SESSION', '10', '1', 'READ', 'SELECT', 'TABLE', 'public.barang', 'select * from barang;', '<not logged>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'psql');
INSERT INTO "pgaudit_log" ("log_time", "user_name", "dbname", "process_id", "connection_from", "session_id", "session_line_num", "command_tag", "session_start_time", "virtual_transaction_id", "transaction_id", "error_severity", "sql_state_code", "message", "detail", "hint", "internal_query", "internal_query_pos", "context", "query", "query_pos", "location", "application_name", "backend_type", "leader_pid", "query_id", "custom_1", "custom_2", "custom_3", "console") VALUES ('2025-09-09 10:58:40.475 WIB', 'postgres', 'dwh', '2376', '[local]', '68bfa54e.948', '13', 'INSERT', '2025-09-09 10:55:58 WIB', '3/12', '0', '00000', 'SESSION', '11', '1', 'WRITE', 'INSERT', 'TABLE', 'public.barang', 'insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values(''ELK-05'',''KOMPUTER'',''UNIT'',28);', '<not logged>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'psql');
INSERT INTO "pgaudit_log" ("log_time", "user_name", "dbname", "process_id", "connection_from", "session_id", "session_line_num", "command_tag", "session_start_time", "virtual_transaction_id", "transaction_id", "error_severity", "sql_state_code", "message", "detail", "hint", "internal_query", "internal_query_pos", "context", "query", "query_pos", "location", "application_name", "backend_type", "leader_pid", "query_id", "custom_1", "custom_2", "custom_3", "console") VALUES ('2025-09-09 10:58:51.988 WIB', 'postgres', 'dwh', '2376', '[local]', '68bfa54e.948', '14', 'ALTER TABLE', '2025-09-09 10:55:58 WIB', '3/13', '910', '00000', 'SESSION', '12', '1', 'DDL', 'ALTER TABLE', NULL, NULL, 'ALTER TABLE barang ADD COLUMN keterangan VARCHAR(100);', '<not logged>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'psql');
INSERT INTO "pgaudit_log" ("log_time", "user_name", "dbname", "process_id", "connection_from", "session_id", "session_line_num", "command_tag", "session_start_time", "virtual_transaction_id", "transaction_id", "error_severity", "sql_state_code", "message", "detail", "hint", "internal_query", "internal_query_pos", "context", "query", "query_pos", "location", "application_name", "backend_type", "leader_pid", "query_id", "custom_1", "custom_2", "custom_3", "console") VALUES ('2025-09-09 10:59:00.983 WIB', 'postgres', 'dwh', '2376', '[local]', '68bfa54e.948', '15', 'SELECT', '2025-09-09 10:55:58 WIB', '3/14', '0', '00000', 'SESSION', '13', '1', 'READ', 'SELECT', 'TABLE', 'public.barang', 'select * from barang;', '<not logged>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'psql');
INSERT INTO "pgaudit_log" ("log_time", "user_name", "dbname", "process_id", "connection_from", "session_id", "session_line_num", "command_tag", "session_start_time", "virtual_transaction_id", "transaction_id", "error_severity", "sql_state_code", "message", "detail", "hint", "internal_query", "internal_query_pos", "context", "query", "query_pos", "location", "application_name", "backend_type", "leader_pid", "query_id", "custom_1", "custom_2", "custom_3", "console") VALUES ('2025-09-09 10:59:09.159 WIB', 'postgres', 'dwh', '2376', '[local]', '68bfa54e.948', '16', 'INSERT', '2025-09-09 10:55:58 WIB', '3/15', '0', '00000', 'SESSION', '14', '1', 'WRITE', 'INSERT', 'TABLE', 'public.barang', 'insert into barang(KODE_BARANG,NAMA_BARANG,SATUAN_BARANG,STOK_BARANG) values(''ELK-06'',''KIPAS ANGIN'',''BUAH'',38);', '<not logged>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'psql');
INSERT INTO "pgaudit_log" ("log_time", "user_name", "dbname", "process_id", "connection_from", "session_id", "session_line_num", "command_tag", "session_start_time", "virtual_transaction_id", "transaction_id", "error_severity", "sql_state_code", "message", "detail", "hint", "internal_query", "internal_query_pos", "context", "query", "query_pos", "location", "application_name", "backend_type", "leader_pid", "query_id", "custom_1", "custom_2", "custom_3", "console") VALUES ('2025-09-09 11:00:44.840 WIB', 'postgres', 'dwh', '2376', '[local]', '68bfa54e.948', '17', 'UPDATE', '2025-09-09 10:55:58 WIB', '3/16', '0', '00000', 'SESSION', '15', '1', 'WRITE', 'UPDATE', 'TABLE', 'public.barang', 'UPDATE barang SET keterangan = ''hologram'' WHERE kode_barang = ''ELK-03''
;', '<not logged>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'psql');
INSERT INTO "pgaudit_log" ("log_time", "user_name", "dbname", "process_id", "connection_from", "session_id", "session_line_num", "command_tag", "session_start_time", "virtual_transaction_id", "transaction_id", "error_severity", "sql_state_code", "message", "detail", "hint", "internal_query", "internal_query_pos", "context", "query", "query_pos", "location", "application_name", "backend_type", "leader_pid", "query_id", "custom_1", "custom_2", "custom_3", "console") VALUES ('2025-09-09 11:01:00.084 WIB', 'postgres', 'dwh', '2376', '[local]', '68bfa54e.948', '18', 'SELECT', '2025-09-09 10:55:58 WIB', '3/17', '0', '00000', 'SESSION', '16', '1', 'READ', 'SELECT', 'TABLE', 'public.barang', 'select * from barang;', '<not logged>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'psql');
INSERT INTO "pgaudit_log" ("log_time", "user_name", "dbname", "process_id", "connection_from", "session_id", "session_line_num", "command_tag", "session_start_time", "virtual_transaction_id", "transaction_id", "error_severity", "sql_state_code", "message", "detail", "hint", "internal_query", "internal_query_pos", "context", "query", "query_pos", "location", "application_name", "backend_type", "leader_pid", "query_id", "custom_1", "custom_2", "custom_3", "console") VALUES ('2025-09-09 12:53:59.538 WIB', 'postgres', 'dbatools', '2822', '[local]', '68bfc0eb.b06', '1', 'DELETE', '2025-09-09 12:53:47 WIB', '4/472', '0', '00000', 'SESSION', '1', '1', 'WRITE', 'DELETE', 'TABLE', 'public.pgaudit_log', 'delete from pgaudit_log;', '<not logged>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'psql');
INSERT INTO "pgaudit_log" ("log_time", "user_name", "dbname", "process_id", "connection_from", "session_id", "session_line_num", "command_tag", "session_start_time", "virtual_transaction_id", "transaction_id", "error_severity", "sql_state_code", "message", "detail", "hint", "internal_query", "internal_query_pos", "context", "query", "query_pos", "location", "application_name", "backend_type", "leader_pid", "query_id", "custom_1", "custom_2", "custom_3", "console") VALUES ('2025-09-09 12:53:59.544 WIB', 'postgres', 'dbatools', '2822', '[local]', '68bfc0eb.b06', '2', 'COPY', '2025-09-09 12:53:47 WIB', '4/473', '0', '00000', 'SESSION', '2', '1', 'WRITE', 'INSERT', 'TABLE', 'public.pgaudit_log', 'COPY pgaudit_log FROM ''/var/lib/pgsql/16/data/log/pgaudit-teguhth.log'' DELIMITER '','' CSV;', '<not logged>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'psql');
G. script backup_inc_edb.sh
[postgres@teguhth script]$ cat backup_inc_edb.sh
# backup_inc_edb.sh
echo ""
Server=$(hostname)
ip_address=$(ip a | awk '/inet / && $2 !~ /^127\./ {gsub("addr:","",$2); print $2}')
# Print the IP address
dbversion=$(psql -Atc "select version();");
infotgl=$(date)
echo ".::: Backup Incremental All database using custom in '$Server' with IP $ip_address :::."
echo ""
echo "PostgreSQL Version : '$Server' $dbversion"
echo ""
#!/bin/bash
#
#clear;
year=`date +%Y`; month=`date +%m`; day=`date +%d`; hour=`date +%H`; min=`date +%M`; sec=`date +%S`; dayname=`date +%a`;underscore='_';
echo "Backup begin $infotgl"
echo ""
backuppath="/var/lib/pgsql/outputdb";
echo "delete from pgaudit_log;"
psql -d dbatools -Atc "delete from pgaudit_log;"
echo "convert log to table "
psql -d dbatools -Atc "COPY pgaudit_log FROM '/var/lib/pgsql/16/data/log/pgaudit-teguhth.log' DELIMITER ',' CSV;"
echo ""
##dbname=$(psql -Atc "SELECT datname FROM pg_database where datname not in('template0','template1','postgres');");
dbname=$(psql -Atc "SELECT datname FROM pg_database where datname not in('template0','template1','postgres');");
#dbname=$(psql -Atc "SELECT datname FROM pg_database where datname in('dwh');");
for listdb in $dbname
do
mkdir -p $backuppath/$year$month$day/$listdb;
path="$backuppath/$year$month$day/$listdb";
#dateinc=$(date +%Y"-"%m"-"%d" "%H":"%M":"%S"_%N");
dateinc=$(date +%Y"-"%m"-"%d" "%H":"%M":"%S" ");
countx=$(psql -d dbatools -Atc "SELECT count(query)
FROM (
SELECT (to_timestamp(trim(replace(session_start_time, 'WIB', '')), 'YYYY-MM-DD HH24:MI:SS') AT TIME ZONE 'Asia/Jakarta') AS session_ts,
dbname, command_tag, query
FROM pgaudit_log
) t
WHERE session_ts >= (date_trunc('hour', now()) AT TIME ZONE 'Asia/Jakarta') - INTERVAL '22 hour'
AND session_ts < (date_trunc('hour', now()) AT TIME ZONE 'Asia/Jakarta')
AND dbname = '$listdb'
AND command_tag != 'SELECT';");
#counttotal=$(psql -d dbatools -Atc "SELECT COUNT(*) FROM pgaudit_log");
counttotal=$(psql -d dbatools -Atc "SELECT count(query)
FROM (
SELECT (to_timestamp(trim(replace(session_start_time, 'WIB', '')), 'YYYY-MM-DD HH24:MI:SS') AT TIME ZONE 'Asia/Jakarta') AS session_ts,
dbname, command_tag, query
FROM pgaudit_log
) t
WHERE session_ts >= (date_trunc('hour', now()) AT TIME ZONE 'Asia/Jakarta') - INTERVAL '22 hour'
AND session_ts < (date_trunc('hour', now()) AT TIME ZONE 'Asia/Jakarta');");
echo "$dateinc --->>> Backup Incremental($countx transactions from $counttotal queries) for database '$listdb' in $path"
echo "--- $dateinc --->>> Backup Incremental ($countx transactions from $counttotal queries) for database '$listdb' in $path" > "$path"/INC_"$listdb"_"$year$month$day$underscore$hour$min$sec".sql;
#pg_dump $listdb | gzip -c > "$path"/INC_"$listdb"_"$year$month$day$underscore$hour$min$sec".sql.gz;
psql -d dbatools -Atc "SELECT query
FROM (
SELECT (to_timestamp(trim(replace(session_start_time, 'WIB', '')), 'YYYY-MM-DD HH24:MI:SS') AT TIME ZONE 'Asia/Jakarta') AS session_ts,
dbname, command_tag, query
FROM pgaudit_log
) t
WHERE session_ts >= (date_trunc('hour', now()) AT TIME ZONE 'Asia/Jakarta') - INTERVAL '1 hour'
AND session_ts < (date_trunc('hour', now()) AT TIME ZONE 'Asia/Jakarta') and dbname ='$listdb' AND command_tag !='SELECT'
ORDER BY session_ts asc;" >> "$path"/INC_"$listdb"_"$year$month$day$underscore$hour$min$sec".sql;
dateincy=$(date +%Y"-"%m"-"%d" "%H":"%M":"%S" ");
echo "--- $dateincy --->>> Finish Backup Incremental ($countx transactions from $counttotal queries)for database '$listdb' in $path" >> "$path"/INC_"$listdb"_"$year$month$day$underscore$hour$min$sec".sql;
echo "$dateincy --->>> Finish Backup Incremental ($countx transactions from $counttotal queries) for database '$listdb' in $path"
done;
echo ""
echo "Backup finish $infotgl"
echo ""
echo "Copyright by : Teguh Triharto"
echo "Website : https://www.linkedin.com/in/teguhth"
echo ""
[postgres@teguhth script]$
No comments:
Post a Comment