2017-02-12 22:42:10 +02:00
|
|
|
import os
|
2017-06-27 11:43:45 +02:00
|
|
|
import unittest
|
2017-02-12 22:42:10 +02:00
|
|
|
from datetime import datetime, timedelta
|
2017-06-27 07:42:52 +02:00
|
|
|
from .helpers.ptrack_helpers import ProbackupTest
|
2017-02-12 22:42:10 +02:00
|
|
|
|
|
|
|
|
2017-07-12 16:28:28 +02:00
|
|
|
module_name = 'retention'
|
|
|
|
|
2017-02-12 22:42:10 +02:00
|
|
|
|
2017-07-12 16:28:28 +02:00
|
|
|
class RetentionTest(ProbackupTest, unittest.TestCase):
|
2017-05-05 15:21:49 +02:00
|
|
|
|
2017-05-22 13:17:43 +02:00
|
|
|
# @unittest.skip("skip")
|
|
|
|
# @unittest.expectedFailure
|
2017-05-05 15:21:49 +02:00
|
|
|
def test_retention_redundancy_1(self):
|
|
|
|
"""purge backups using redundancy-based retention policy"""
|
|
|
|
fname = self.id().split('.')[3]
|
2017-07-12 16:28:28 +02:00
|
|
|
node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname),
|
2017-05-05 15:21:49 +02:00
|
|
|
initdb_params=['--data-checksums'],
|
|
|
|
pg_options={'wal_level': 'replica'}
|
|
|
|
)
|
2017-07-12 16:28:28 +02:00
|
|
|
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
|
2017-06-20 12:57:23 +02:00
|
|
|
self.init_pb(backup_dir)
|
|
|
|
self.add_instance(backup_dir, 'node', node)
|
|
|
|
self.set_archiving(backup_dir, 'node', node)
|
2017-05-05 15:21:49 +02:00
|
|
|
node.start()
|
|
|
|
|
2017-06-20 12:57:23 +02:00
|
|
|
with open(os.path.join(backup_dir, 'backups', 'node', "pg_probackup.conf"), "a") as conf:
|
2017-05-05 15:21:49 +02:00
|
|
|
conf.write("retention-redundancy = 1\n")
|
|
|
|
|
|
|
|
# Make backups to be purged
|
2017-06-20 12:57:23 +02:00
|
|
|
self.backup_node(backup_dir, 'node', node)
|
|
|
|
self.backup_node(backup_dir, 'node', node, backup_type="page")
|
2017-05-05 15:21:49 +02:00
|
|
|
# Make backups to be keeped
|
2017-06-20 12:57:23 +02:00
|
|
|
self.backup_node(backup_dir, 'node', node)
|
|
|
|
self.backup_node(backup_dir, 'node', node, backup_type="page")
|
2017-05-05 15:21:49 +02:00
|
|
|
|
2017-06-20 12:57:23 +02:00
|
|
|
self.assertEqual(len(self.show_pb(backup_dir, 'node')), 4)
|
2017-05-05 15:21:49 +02:00
|
|
|
|
|
|
|
# Purge backups
|
2017-06-20 12:57:23 +02:00
|
|
|
log = self.delete_expired(backup_dir, 'node')
|
|
|
|
self.assertEqual(len(self.show_pb(backup_dir, 'node')), 2)
|
2017-05-05 15:21:49 +02:00
|
|
|
|
|
|
|
# Check that WAL segments were deleted
|
|
|
|
min_wal = None
|
|
|
|
max_wal = None
|
|
|
|
for line in log.splitlines():
|
2017-06-27 07:42:52 +02:00
|
|
|
if line.startswith("INFO: removed min WAL segment"):
|
2017-05-05 15:21:49 +02:00
|
|
|
min_wal = line[31:-1]
|
2017-06-27 07:42:52 +02:00
|
|
|
elif line.startswith("INFO: removed max WAL segment"):
|
2017-05-05 15:21:49 +02:00
|
|
|
max_wal = line[31:-1]
|
2018-01-28 03:36:27 +02:00
|
|
|
|
|
|
|
if not min_wal:
|
|
|
|
self.assertTrue(False, "min_wal is empty")
|
|
|
|
|
|
|
|
if not max_wal:
|
|
|
|
self.assertTrue(False, "max_wal is not set")
|
|
|
|
|
2017-06-27 11:43:45 +02:00
|
|
|
for wal_name in os.listdir(os.path.join(backup_dir, 'wal', 'node')):
|
2017-05-05 15:21:49 +02:00
|
|
|
if not wal_name.endswith(".backup"):
|
2017-06-27 07:42:52 +02:00
|
|
|
#wal_name_b = wal_name.encode('ascii')
|
|
|
|
self.assertEqual(wal_name[8:] > min_wal[8:], True)
|
|
|
|
self.assertEqual(wal_name[8:] > max_wal[8:], True)
|
2017-05-05 15:21:49 +02:00
|
|
|
|
2017-06-27 07:42:52 +02:00
|
|
|
# Clean after yourself
|
2017-07-12 16:28:28 +02:00
|
|
|
self.del_test_dir(module_name, fname)
|
2017-05-05 15:21:49 +02:00
|
|
|
|
|
|
|
# @unittest.skip("123")
|
|
|
|
def test_retention_window_2(self):
|
|
|
|
"""purge backups using window-based retention policy"""
|
|
|
|
fname = self.id().split('.')[3]
|
2017-07-12 16:28:28 +02:00
|
|
|
node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname),
|
2017-05-05 15:21:49 +02:00
|
|
|
initdb_params=['--data-checksums'],
|
|
|
|
pg_options={'wal_level': 'replica'}
|
|
|
|
)
|
2017-07-12 16:28:28 +02:00
|
|
|
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
|
2017-06-20 12:57:23 +02:00
|
|
|
self.init_pb(backup_dir)
|
|
|
|
self.add_instance(backup_dir, 'node', node)
|
|
|
|
self.set_archiving(backup_dir, 'node', node)
|
2017-05-05 15:21:49 +02:00
|
|
|
node.start()
|
|
|
|
|
2018-01-18 03:35:27 +02:00
|
|
|
with open(
|
|
|
|
os.path.join(
|
|
|
|
backup_dir,
|
|
|
|
'backups',
|
|
|
|
'node',
|
|
|
|
"pg_probackup.conf"), "a") as conf:
|
2017-05-05 15:21:49 +02:00
|
|
|
conf.write("retention-redundancy = 1\n")
|
|
|
|
conf.write("retention-window = 1\n")
|
|
|
|
|
|
|
|
# Make backups to be purged
|
2017-06-20 12:57:23 +02:00
|
|
|
self.backup_node(backup_dir, 'node', node)
|
|
|
|
self.backup_node(backup_dir, 'node', node, backup_type="page")
|
2017-05-05 15:21:49 +02:00
|
|
|
# Make backup to be keeped
|
2017-06-20 12:57:23 +02:00
|
|
|
self.backup_node(backup_dir, 'node', node)
|
2017-05-05 15:21:49 +02:00
|
|
|
|
2017-06-27 11:43:45 +02:00
|
|
|
backups = os.path.join(backup_dir, 'backups', 'node')
|
2017-05-05 15:21:49 +02:00
|
|
|
days_delta = 5
|
2017-06-27 11:43:45 +02:00
|
|
|
for backup in os.listdir(backups):
|
2017-06-20 12:57:23 +02:00
|
|
|
if backup == 'pg_probackup.conf':
|
|
|
|
continue
|
2018-01-18 03:35:27 +02:00
|
|
|
with open(
|
|
|
|
os.path.join(
|
|
|
|
backups, backup, "backup.control"), "a") as conf:
|
2017-05-05 15:21:49 +02:00
|
|
|
conf.write("recovery_time='{:%Y-%m-%d %H:%M:%S}'\n".format(
|
|
|
|
datetime.now() - timedelta(days=days_delta)))
|
|
|
|
days_delta -= 1
|
|
|
|
|
|
|
|
# Make backup to be keeped
|
2017-06-20 12:57:23 +02:00
|
|
|
self.backup_node(backup_dir, 'node', node, backup_type="page")
|
2017-05-05 15:21:49 +02:00
|
|
|
|
2017-06-20 12:57:23 +02:00
|
|
|
self.assertEqual(len(self.show_pb(backup_dir, 'node')), 4)
|
2017-05-05 15:21:49 +02:00
|
|
|
|
|
|
|
# Purge backups
|
2017-06-20 12:57:23 +02:00
|
|
|
self.delete_expired(backup_dir, 'node')
|
|
|
|
self.assertEqual(len(self.show_pb(backup_dir, 'node')), 2)
|
2017-05-05 15:21:49 +02:00
|
|
|
|
2017-06-27 07:42:52 +02:00
|
|
|
# Clean after yourself
|
2017-07-12 16:28:28 +02:00
|
|
|
self.del_test_dir(module_name, fname)
|
2017-12-19 12:17:58 +02:00
|
|
|
|
|
|
|
# @unittest.skip("123")
|
|
|
|
def test_retention_wal(self):
|
|
|
|
"""purge backups using window-based retention policy"""
|
|
|
|
fname = self.id().split('.')[3]
|
2018-01-18 03:35:27 +02:00
|
|
|
node = self.make_simple_node(
|
|
|
|
base_dir="{0}/{1}/node".format(module_name, fname),
|
2017-12-19 12:17:58 +02:00
|
|
|
initdb_params=['--data-checksums'],
|
|
|
|
pg_options={'wal_level': 'replica'}
|
|
|
|
)
|
|
|
|
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
|
|
|
|
self.init_pb(backup_dir)
|
|
|
|
self.add_instance(backup_dir, 'node', node)
|
|
|
|
self.set_archiving(backup_dir, 'node', node)
|
|
|
|
node.start()
|
|
|
|
|
|
|
|
node.safe_psql(
|
|
|
|
"postgres",
|
2018-01-18 03:35:27 +02:00
|
|
|
"create table t_heap as select i as id, md5(i::text) as text, "
|
|
|
|
"md5(repeat(i::text,10))::tsvector as tsvector "
|
|
|
|
"from generate_series(0,100500) i")
|
2017-12-19 12:17:58 +02:00
|
|
|
|
|
|
|
# Take FULL BACKUP
|
|
|
|
self.backup_node(backup_dir, 'node', node)
|
|
|
|
node.safe_psql(
|
|
|
|
"postgres",
|
2018-01-18 03:35:27 +02:00
|
|
|
"insert into t_heap select i as id, md5(i::text) as text, "
|
|
|
|
"md5(repeat(i::text,10))::tsvector as tsvector "
|
|
|
|
"from generate_series(0,100500) i")
|
2017-12-19 12:17:58 +02:00
|
|
|
|
|
|
|
self.backup_node(backup_dir, 'node', node)
|
|
|
|
|
|
|
|
backups = os.path.join(backup_dir, 'backups', 'node')
|
|
|
|
days_delta = 5
|
|
|
|
for backup in os.listdir(backups):
|
|
|
|
if backup == 'pg_probackup.conf':
|
|
|
|
continue
|
2018-01-18 03:35:27 +02:00
|
|
|
with open(
|
|
|
|
os.path.join(
|
|
|
|
backups, backup, "backup.control"), "a") as conf:
|
2017-12-19 12:17:58 +02:00
|
|
|
conf.write("recovery_time='{:%Y-%m-%d %H:%M:%S}'\n".format(
|
|
|
|
datetime.now() - timedelta(days=days_delta)))
|
|
|
|
days_delta -= 1
|
|
|
|
|
|
|
|
# Make backup to be keeped
|
|
|
|
self.backup_node(backup_dir, 'node', node, backup_type="page")
|
|
|
|
|
2018-01-18 03:35:27 +02:00
|
|
|
self.assertEqual(len(self.show_pb(backup_dir, 'node')), 3)
|
2017-12-19 12:17:58 +02:00
|
|
|
|
|
|
|
# Purge backups
|
2018-01-18 03:35:27 +02:00
|
|
|
self.delete_expired(
|
|
|
|
backup_dir, 'node', options=['--retention-window=2'])
|
2017-12-19 12:17:58 +02:00
|
|
|
self.assertEqual(len(self.show_pb(backup_dir, 'node')), 2)
|
|
|
|
|
|
|
|
# Clean after yourself
|
|
|
|
self.del_test_dir(module_name, fname)
|