2017-11-30 08:14:46 +02:00
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
from .helpers.ptrack_helpers import ProbackupTest, ProbackupException
|
|
|
|
|
|
|
|
|
|
|
|
module_name = 'exclude'
|
|
|
|
|
|
|
|
|
|
|
|
class ExcludeTest(ProbackupTest, unittest.TestCase):
|
|
|
|
|
|
|
|
# @unittest.skip("skip")
|
|
|
|
# @unittest.expectedFailure
|
|
|
|
def test_exclude_temp_tables(self):
|
2018-05-21 16:08:46 +02:00
|
|
|
"""
|
|
|
|
make node without archiving, create temp table, take full backup,
|
|
|
|
check that temp table not present in backup catalogue
|
|
|
|
"""
|
2017-11-30 08:14:46 +02:00
|
|
|
fname = self.id().split('.')[3]
|
|
|
|
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
|
2018-05-21 16:08:46 +02:00
|
|
|
node = self.make_simple_node(
|
2018-12-26 21:59:13 +02:00
|
|
|
base_dir=os.path.join(module_name, fname, 'node'),
|
2017-11-30 08:14:46 +02:00
|
|
|
set_replication=True,
|
2019-07-12 17:01:28 +02:00
|
|
|
initdb_params=['--data-checksums'])
|
2017-11-30 08:14:46 +02:00
|
|
|
|
|
|
|
self.init_pb(backup_dir)
|
|
|
|
self.add_instance(backup_dir, 'node', node)
|
2018-12-25 16:48:49 +02:00
|
|
|
node.slow_start()
|
2017-11-30 08:14:46 +02:00
|
|
|
|
|
|
|
with node.connect("postgres") as conn:
|
|
|
|
|
2018-05-21 16:08:46 +02:00
|
|
|
conn.execute(
|
|
|
|
"create temp table test as "
|
|
|
|
"select generate_series(0,50050000)::text")
|
2017-11-30 08:14:46 +02:00
|
|
|
conn.commit()
|
|
|
|
|
2018-05-21 16:08:46 +02:00
|
|
|
temp_schema_name = conn.execute(
|
|
|
|
"SELECT nspname FROM pg_namespace "
|
|
|
|
"WHERE oid = pg_my_temp_schema()")[0][0]
|
2017-11-30 08:14:46 +02:00
|
|
|
conn.commit()
|
|
|
|
|
2018-06-02 19:35:37 +02:00
|
|
|
temp_toast_schema_name = "pg_toast_" + temp_schema_name.replace(
|
|
|
|
"pg_", "")
|
2017-11-30 08:14:46 +02:00
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
conn.execute("create index test_idx on test (generate_series)")
|
|
|
|
conn.commit()
|
|
|
|
|
2018-06-02 19:35:37 +02:00
|
|
|
heap_path = conn.execute(
|
|
|
|
"select pg_relation_filepath('test')")[0][0]
|
2017-11-30 08:14:46 +02:00
|
|
|
conn.commit()
|
|
|
|
|
2018-06-02 19:35:37 +02:00
|
|
|
index_path = conn.execute(
|
|
|
|
"select pg_relation_filepath('test_idx')")[0][0]
|
2017-11-30 08:14:46 +02:00
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
heap_oid = conn.execute("select 'test'::regclass::oid")[0][0]
|
|
|
|
conn.commit()
|
|
|
|
|
2018-06-02 19:35:37 +02:00
|
|
|
toast_path = conn.execute(
|
|
|
|
"select pg_relation_filepath('{0}.{1}')".format(
|
|
|
|
temp_toast_schema_name, "pg_toast_" + str(heap_oid)))[0][0]
|
2017-11-30 08:14:46 +02:00
|
|
|
conn.commit()
|
|
|
|
|
2018-06-02 19:35:37 +02:00
|
|
|
toast_idx_path = conn.execute(
|
|
|
|
"select pg_relation_filepath('{0}.{1}')".format(
|
|
|
|
temp_toast_schema_name,
|
|
|
|
"pg_toast_" + str(heap_oid) + "_index"))[0][0]
|
2017-11-30 08:14:46 +02:00
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
temp_table_filename = os.path.basename(heap_path)
|
|
|
|
temp_idx_filename = os.path.basename(index_path)
|
|
|
|
temp_toast_filename = os.path.basename(toast_path)
|
|
|
|
temp_idx_toast_filename = os.path.basename(toast_idx_path)
|
|
|
|
|
2018-05-21 16:08:46 +02:00
|
|
|
self.backup_node(
|
|
|
|
backup_dir, 'node', node, backup_type='full', options=['--stream'])
|
2017-11-30 08:14:46 +02:00
|
|
|
|
|
|
|
for root, dirs, files in os.walk(backup_dir):
|
|
|
|
for file in files:
|
2018-05-21 16:08:46 +02:00
|
|
|
if file in [
|
|
|
|
temp_table_filename, temp_table_filename + ".1",
|
|
|
|
temp_idx_filename,
|
|
|
|
temp_idx_filename + ".1",
|
|
|
|
temp_toast_filename,
|
|
|
|
temp_toast_filename + ".1",
|
|
|
|
temp_idx_toast_filename,
|
|
|
|
temp_idx_toast_filename + ".1"
|
|
|
|
]:
|
|
|
|
self.assertEqual(
|
|
|
|
1, 0,
|
|
|
|
"Found temp table file in backup catalogue.\n "
|
|
|
|
"Filepath: {0}".format(file))
|
2017-11-30 08:14:46 +02:00
|
|
|
|
|
|
|
# Clean after yourself
|
|
|
|
self.del_test_dir(module_name, fname)
|
|
|
|
|
|
|
|
# @unittest.skip("skip")
|
2018-05-21 12:19:33 +02:00
|
|
|
def test_exclude_unlogged_tables_1(self):
|
|
|
|
"""
|
|
|
|
make node without archiving, create unlogged table, take full backup,
|
2019-07-12 17:01:28 +02:00
|
|
|
alter table to unlogged, take delta backup, restore delta backup,
|
2018-05-21 12:19:33 +02:00
|
|
|
check that PGDATA`s are physically the same
|
|
|
|
"""
|
2017-11-30 08:14:46 +02:00
|
|
|
fname = self.id().split('.')[3]
|
|
|
|
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
|
2018-05-21 12:19:33 +02:00
|
|
|
node = self.make_simple_node(
|
2018-12-26 21:59:13 +02:00
|
|
|
base_dir=os.path.join(module_name, fname, 'node'),
|
2017-11-30 08:14:46 +02:00
|
|
|
set_replication=True,
|
|
|
|
initdb_params=['--data-checksums'],
|
2018-05-21 12:19:33 +02:00
|
|
|
pg_options={
|
2019-05-01 01:39:08 +02:00
|
|
|
'autovacuum': 'off',
|
2019-07-12 17:01:28 +02:00
|
|
|
"shared_buffers": "10MB"})
|
2017-11-30 08:14:46 +02:00
|
|
|
|
|
|
|
self.init_pb(backup_dir)
|
|
|
|
self.add_instance(backup_dir, 'node', node)
|
2018-12-25 16:48:49 +02:00
|
|
|
node.slow_start()
|
2017-11-30 08:14:46 +02:00
|
|
|
|
|
|
|
conn = node.connect()
|
|
|
|
with node.connect("postgres") as conn:
|
|
|
|
|
2018-05-21 12:19:33 +02:00
|
|
|
conn.execute(
|
|
|
|
"create unlogged table test as "
|
|
|
|
"select generate_series(0,5005000)::text")
|
2017-11-30 08:14:46 +02:00
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
conn.execute("create index test_idx on test (generate_series)")
|
|
|
|
conn.commit()
|
|
|
|
|
2018-05-21 12:19:33 +02:00
|
|
|
self.backup_node(
|
|
|
|
backup_dir, 'node', node,
|
|
|
|
backup_type='full', options=['--stream'])
|
2017-11-30 08:14:46 +02:00
|
|
|
|
2018-05-21 12:19:33 +02:00
|
|
|
node.safe_psql('postgres', "alter table test set logged")
|
2017-11-30 08:14:46 +02:00
|
|
|
|
2018-05-21 12:19:33 +02:00
|
|
|
self.backup_node(
|
2019-07-12 17:01:28 +02:00
|
|
|
backup_dir, 'node', node, backup_type='delta',
|
2018-11-12 10:51:58 +02:00
|
|
|
options=['--stream']
|
2018-05-21 12:19:33 +02:00
|
|
|
)
|
2017-11-30 08:14:46 +02:00
|
|
|
|
2018-05-21 12:19:33 +02:00
|
|
|
pgdata = self.pgdata_content(node.data_dir)
|
2017-11-30 08:14:46 +02:00
|
|
|
|
2018-05-21 12:19:33 +02:00
|
|
|
node_restored = self.make_simple_node(
|
2018-12-26 21:59:13 +02:00
|
|
|
base_dir=os.path.join(module_name, fname, 'node_restored'))
|
|
|
|
|
2018-05-21 12:19:33 +02:00
|
|
|
node_restored.cleanup()
|
2017-11-30 08:14:46 +02:00
|
|
|
|
2018-05-21 12:19:33 +02:00
|
|
|
self.restore_node(
|
|
|
|
backup_dir, 'node', node_restored, options=["-j", "4"])
|
2017-11-30 08:14:46 +02:00
|
|
|
|
2018-05-21 12:19:33 +02:00
|
|
|
# Physical comparison
|
|
|
|
pgdata_restored = self.pgdata_content(node_restored.data_dir)
|
|
|
|
self.compare_pgdata(pgdata, pgdata_restored)
|
2017-11-30 08:14:46 +02:00
|
|
|
|
|
|
|
# Clean after yourself
|
|
|
|
self.del_test_dir(module_name, fname)
|
2020-05-21 12:44:22 +02:00
|
|
|
|
|
|
|
# @unittest.skip("skip")
|
|
|
|
def test_exclude_log_dir(self):
|
|
|
|
"""
|
|
|
|
check that by default 'log' and 'pg_log' directories are not backed up
|
|
|
|
"""
|
|
|
|
fname = self.id().split('.')[3]
|
|
|
|
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
|
|
|
|
node = self.make_simple_node(
|
|
|
|
base_dir=os.path.join(module_name, fname, 'node'),
|
|
|
|
set_replication=True,
|
|
|
|
initdb_params=['--data-checksums'],
|
|
|
|
pg_options={
|
|
|
|
'logging_collector': 'on',
|
|
|
|
'log_filename': 'postgresql.log'})
|
|
|
|
|
|
|
|
self.init_pb(backup_dir)
|
|
|
|
self.add_instance(backup_dir, 'node', node)
|
|
|
|
node.slow_start()
|
|
|
|
|
|
|
|
self.backup_node(
|
|
|
|
backup_dir, 'node', node,
|
|
|
|
backup_type='full', options=['--stream'])
|
|
|
|
|
|
|
|
log_dir = node.safe_psql(
|
|
|
|
'postgres',
|
|
|
|
'show log_directory').rstrip()
|
|
|
|
|
|
|
|
node.cleanup()
|
|
|
|
|
|
|
|
self.restore_node(
|
|
|
|
backup_dir, 'node', node, options=["-j", "4"])
|
|
|
|
|
|
|
|
# check that PGDATA/log or PGDATA/pg_log do not exists
|
|
|
|
path = os.path.join(node.data_dir, log_dir)
|
|
|
|
log_file = os.path.join(path, 'postgresql.log')
|
|
|
|
self.assertTrue(os.path.exists(path))
|
|
|
|
self.assertFalse(os.path.exists(log_file))
|
|
|
|
|
|
|
|
# Clean after yourself
|
|
|
|
self.del_test_dir(module_name, fname)
|
|
|
|
|
|
|
|
# @unittest.skip("skip")
|
|
|
|
def test_exclude_log_dir_1(self):
|
|
|
|
"""
|
|
|
|
check that "--backup-pg-log" works correctly
|
|
|
|
"""
|
|
|
|
fname = self.id().split('.')[3]
|
|
|
|
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
|
|
|
|
node = self.make_simple_node(
|
|
|
|
base_dir=os.path.join(module_name, fname, 'node'),
|
|
|
|
set_replication=True,
|
|
|
|
initdb_params=['--data-checksums'],
|
|
|
|
pg_options={
|
|
|
|
'logging_collector': 'on',
|
|
|
|
'log_filename': 'postgresql.log'})
|
|
|
|
|
|
|
|
self.init_pb(backup_dir)
|
|
|
|
self.add_instance(backup_dir, 'node', node)
|
|
|
|
node.slow_start()
|
|
|
|
|
|
|
|
log_dir = node.safe_psql(
|
|
|
|
'postgres',
|
|
|
|
'show log_directory').rstrip()
|
|
|
|
|
|
|
|
self.backup_node(
|
|
|
|
backup_dir, 'node', node,
|
|
|
|
backup_type='full', options=['--stream', '--backup-pg-log'])
|
|
|
|
|
|
|
|
node.cleanup()
|
|
|
|
|
|
|
|
self.restore_node(
|
|
|
|
backup_dir, 'node', node, options=["-j", "4"])
|
|
|
|
|
|
|
|
# check that PGDATA/log or PGDATA/pg_log do not exists
|
|
|
|
path = os.path.join(node.data_dir, log_dir)
|
|
|
|
log_file = os.path.join(path, 'postgresql.log')
|
|
|
|
self.assertTrue(os.path.exists(path))
|
|
|
|
self.assertTrue(os.path.exists(log_file))
|
|
|
|
|
|
|
|
# Clean after yourself
|
|
|
|
self.del_test_dir(module_name, fname)
|