# Load settings listSettings = configService.getConfig() # We init duplicity try: backupService.initDuplicity(BACKUP_PATH, backend) except Exception as e: logger.info( "No backup found (probably the first) or already initialized") pass # We dump the container if needed # Get all services (potential dump) if DISABLE_DUMP != "true": listServices = rancherService.getServices() listDump = backupService.searchDump(BACKUP_PATH, listServices, listSettings) backupService.runDump(listDump) # We dump the rancher settings if DISABLE_DUMP_RANCHER != "true": listStacks = rancherService.getStacks() backupService.dumpStacksSettings(BACKUP_PATH + '/rancher', listStacks) # We run the backup backupService.runDuplicity(BACKUP_PATH, backend, BK_FULL_FREQ, BK_KEEP_FULL, BK_KEEP_FULL_CHAIN, VOLUME_SIZE) except Exception as e: logger.error("Unattented error occur : %s", e.message)
settings['duplicity']['source-path'], backend) logger.info("Duplicity initialization is finished.") except Exception as e: logger.info( "No backup found (probably the first) or already initialized" ) pass else: logger.info("Duplicity disabled...") # We dump the databases services if needed if settings['module']['databases'] is True: logger.info("Start to dump databases...") listServices = rancherService.getServices() listDump = backupService.searchDump( settings['duplicity']['source-path'] + '/dump', listServices) backupService.runDump(listDump) logger.info("The dumping databases is finished.") # We dump the rancher stack settings if needed if settings['module']['stack'] is True: logger.info("Start to export stack as json...") listStacks = rancherService.getStacks() backupService.dumpStacksSettings( settings['duplicity']['source-path'] + '/rancher', listStacks) logger.info("The exporting of stack if finished") # We dump the rancher database if needed if settings['module']['rancher-db'] is True:
def testSearchDump(self, mock_config): backupService = Backup() Config('/fake/path') # Search template backup with image name listServices = [{ 'type': 'service', 'name': 'test', 'state': 'active', 'launchConfig': { 'imageUuid': 'test/postgres:latest', 'environment': { 'POSTGRES_USER': '******', 'POSTGRES_DB': 'test', 'POSTGRES_PASSWORD': '******' } }, 'links': { 'environment': 'https://fake/environment', 'instances': 'https://fake/instances', }, 'stack': { 'name': 'stack-test' }, 'instances': [{ 'state': 'disabled', 'primaryIpAddress': '10.0.0.1', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.2', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.3', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }], }, { 'type': 'service', 'name': 'test2', 'state': 'active', 'launchConfig': { 'imageUuid': 'test/mysql:latest', 'environment': { 'MYSQL_USER': '******', 'MYSQL_DATABASE': 'test', 'MYSQL_PASSWORD': '******' } }, 'links': { 'environment': 'https://fake/environment', 'instances': 'https://fake/instances', }, 'stack': { 'name': 'stack-test' }, 'instances': [{ 'state': 'disabled', 'primaryIpAddress': '10.1.0.1', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.1.0.2', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.1.0.3', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }], }] result = backupService.searchDump('/tmp/backup', listServices) targetResult = [{ 'service': listServices[0], 'target_dir': '/tmp/backup/stack-test/test', 'commands': [ 'pg_dump -h 10.0.0.2 -U user -d test -f /tmp/backup/stack-test/test/test.dump' ], 'environments': ['PGPASSWORD:pass'], 'image': 'postgres:latest' }] self.assertEqual(targetResult, result) # Search backup with labels # Search template backup with image name listServices = [{ 'type': 'service', 'name': 'test', 'state': 'active', 'launchConfig': { 'imageUuid': 'test/my-db:latest', 'environment': { 'POSTGRES_USER': '******', 'POSTGRES_DB': 'test', 'POSTGRES_PASSWORD': '******' }, 'labels': { 'backup.type': 'postgres' } }, 'links': { 'environment': 'https://fake/environment', 'instances': 'https://fake/instances', }, 'stack': { 'name': 'stack-test' }, 'instances': [{ 'state': 'disabled', 'primaryIpAddress': '10.0.0.1', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.2', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.3', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }], }, { 'type': 'service', 'name': 'test2', 'state': 'active', 'launchConfig': { 'imageUuid': 'test/my-db:latest', 'environment': { 'MYSQL_USER': '******', 'MYSQL_DATABASE': 'test', 'MYSQL_PASSWORD': '******' }, 'labels': { 'backup.type': 'mysql' } }, 'links': { 'environment': 'https://fake/environment', 'instances': 'https://fake/instances', }, 'stack': { 'name': 'stack-test' }, 'instances': [{ 'state': 'disabled', 'primaryIpAddress': '10.1.0.1', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.1.0.2', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.1.0.3', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }], }] result = backupService.searchDump('/tmp/backup', listServices) targetResult = [{ 'service': listServices[0], 'target_dir': '/tmp/backup/stack-test/test', 'commands': [ 'pg_dump -h 10.0.0.2 -U user -d test -f /tmp/backup/stack-test/test/test.dump' ], 'environments': ['PGPASSWORD:pass'], 'image': 'postgres:latest' }] self.assertEqual(targetResult, result)
def testTemplateMongodb(self): backupService = Backup() # When no user and password listServices = [{ 'type': 'service', 'name': 'test', 'state': 'active', 'launchConfig': { 'imageUuid': 'mongo:latest', 'environment': {} }, 'links': { 'stack': 'https://fake/stack', 'instances': 'https://fake/instances', }, 'stack': { 'name': 'stack-test' }, 'instances': [{ 'state': 'disabled', 'primaryIpAddress': '10.0.0.1', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.2', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.3', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }], }] targetResult = [{ 'service': listServices[0], 'target_dir': '/tmp/backup/stack-test/test', 'commands': ['mongodump --host 10.0.0.2 --out /tmp/backup/stack-test/test'], 'environments': [], 'image': 'mongo:latest' }] result = backupService.searchDump('/tmp/backup', listServices) self.assertEqual(targetResult, result) # When user and password listServices = [{ 'type': 'service', 'name': 'test', 'state': 'active', 'launchConfig': { 'imageUuid': 'mongo:latest', 'environment': { 'MONGO_USER': '******', 'MONGO_PASS': '******' } }, 'links': { 'stack': 'https://fake/stack', 'instances': 'https://fake/instances', }, 'stack': { 'name': 'stack-test' }, 'instances': [{ 'state': 'disabled', 'primaryIpAddress': '10.0.0.1', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.2', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.3', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }], }] targetResult = [{ 'service': listServices[0], 'target_dir': '/tmp/backup/stack-test/test', 'commands': [ 'mongodump --host 10.0.0.2 -u user -p pass --out /tmp/backup/stack-test/test' ], 'environments': [], 'image': 'mongo:latest' }] result = backupService.searchDump('/tmp/backup', listServices) self.assertEqual(targetResult, result)
def testTemplatePostgresql(self): backupService = Backup() # When user, password and database are setted listServices = [{ 'type': 'service', 'name': 'test', 'state': 'active', 'launchConfig': { 'imageUuid': 'test/postgres:latest', 'environment': { 'POSTGRES_USER': '******', 'POSTGRES_DB': 'test', 'POSTGRES_PASSWORD': '******' } }, 'links': { 'stack': 'https://fake/stack', 'instances': 'https://fake/instances', }, 'stack': { 'name': 'stack-test' }, 'instances': [{ 'state': 'disabled', 'primaryIpAddress': '10.0.0.1', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.2', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.3', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }], }] targetResult = [{ 'service': listServices[0], 'target_dir': '/tmp/backup/stack-test/test', 'commands': [ 'pg_dump -h 10.0.0.2 -U user -d test -f /tmp/backup/stack-test/test/test.dump' ], 'environments': ['PGPASSWORD:pass'], 'image': 'postgres:latest' }] result = backupService.searchDump('/tmp/backup', listServices) self.assertEqual(targetResult, result) # When user, password are setted listServices = [{ 'type': 'service', 'name': 'test', 'state': 'active', 'launchConfig': { 'imageUuid': 'test/postgres:latest', 'environment': { 'POSTGRES_USER': '******', 'POSTGRES_PASSWORD': '******' } }, 'links': { 'stack': 'https://fake/stack', 'instances': 'https://fake/instances', }, 'stack': { 'name': 'stack-test' }, 'instances': [{ 'state': 'disabled', 'primaryIpAddress': '10.0.0.1', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.2', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.3', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }], }] targetResult = [{ 'service': listServices[0], 'target_dir': '/tmp/backup/stack-test/test', 'commands': [ 'pg_dump -h 10.0.0.2 -U user -d user -f /tmp/backup/stack-test/test/user.dump' ], 'environments': ['PGPASSWORD:pass'], 'image': 'postgres:latest' }] result = backupService.searchDump('/tmp/backup', listServices) self.assertEqual(targetResult, result) # When password and database are setted listServices = [{ 'type': 'service', 'name': 'test', 'state': 'active', 'launchConfig': { 'imageUuid': 'test/postgres:latest', 'environment': { 'POSTGRES_DB': 'test', 'POSTGRES_PASSWORD': '******' } }, 'links': { 'stack': 'https://fake/stack', 'instances': 'https://fake/instances', }, 'stack': { 'name': 'stack-test' }, 'instances': [{ 'state': 'disabled', 'primaryIpAddress': '10.0.0.1', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.2', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.3', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }], }] targetResult = [{ 'service': listServices[0], 'target_dir': '/tmp/backup/stack-test/test', 'commands': [ 'pg_dump -h 10.0.0.2 -U postgres -d test -f /tmp/backup/stack-test/test/test.dump' ], 'environments': ['PGPASSWORD:pass'], 'image': 'postgres:latest' }] result = backupService.searchDump('/tmp/backup', listServices) self.assertEqual(targetResult, result) # When password are setted listServices = [{ 'type': 'service', 'name': 'test', 'state': 'active', 'launchConfig': { 'imageUuid': 'test/postgres:latest', 'environment': { 'POSTGRES_PASSWORD': '******' } }, 'links': { 'stack': 'https://fake/stack', 'instances': 'https://fake/instances', }, 'stack': { 'name': 'stack-test' }, 'instances': [{ 'state': 'disabled', 'primaryIpAddress': '10.0.0.1', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.2', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.3', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }], }] targetResult = [{ 'service': listServices[0], 'target_dir': '/tmp/backup/stack-test/test', 'commands': [ 'pg_dumpall -h 10.0.0.2 -U postgres --clean -f /tmp/backup/stack-test/test/all-databases.dump' ], 'environments': ['PGPASSWORD:pass'], 'image': 'postgres:latest' }] result = backupService.searchDump('/tmp/backup', listServices) self.assertEqual(targetResult, result)
def testTemplateMariadb(self): backupService = Backup() # When user, password and database are setted listServices = [{ 'type': 'service', 'name': 'test2', 'state': 'active', 'launchConfig': { 'imageUuid': 'test/mariadb:latest', 'environment': { 'MYSQL_USER': '******', 'MYSQL_DATABASE': 'test', 'MYSQL_PASSWORD': '******' } }, 'links': { 'stack': 'https://fake/stack', 'instances': 'https://fake/instances', }, 'stack': { 'name': 'stack-test' }, 'instances': [{ 'state': 'disabled', 'primaryIpAddress': '10.1.0.1', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.1.0.2', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.1.0.3', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }], }] targetResult = [{ 'service': listServices[0], 'target_dir': '/backup/stack-test/test2', 'commands': [ "sh -c 'mysqldump -h 10.1.0.2 -u user test > /backup/stack-test/test2/test.dump'", ], 'environments': ['MYSQL_PWD:pass'], 'image': 'mariadb:latest' }] result = backupService.searchDump('/backup', listServices) self.assertEqual(targetResult, result) # When user, password are setted listServices = [{ 'type': 'service', 'name': 'test2', 'state': 'active', 'launchConfig': { 'imageUuid': 'test/mariadb:latest', 'environment': { 'MYSQL_USER': '******', 'MYSQL_PASSWORD': '******' } }, 'links': { 'stack': 'https://fake/stack', 'instances': 'https://fake/instances', }, 'stack': { 'name': 'stack-test' }, 'instances': [{ 'state': 'disabled', 'primaryIpAddress': '10.1.0.1', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.1.0.2', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.1.0.3', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }], }] targetResult = [{ 'service': listServices[0], 'target_dir': '/backup/stack-test/test2', 'commands': [ "sh -c 'mysqldump -h 10.1.0.2 -u user --all-databases > /backup/stack-test/test2/all-databases.dump'", ], 'environments': ['MYSQL_PWD:pass'], 'image': 'mariadb:latest' }] result = backupService.searchDump('/backup', listServices) self.assertEqual(targetResult, result) # When root password and database are setted listServices = [{ 'type': 'service', 'name': 'test2', 'state': 'active', 'launchConfig': { 'imageUuid': 'test/mariadb:latest', 'environment': { 'MYSQL_DATABASE': 'test', 'MYSQL_ROOT_PASSWORD': '******' } }, 'links': { 'stack': 'https://fake/stack', 'instances': 'https://fake/instances', }, 'stack': { 'name': 'stack-test' }, 'instances': [{ 'state': 'disabled', 'primaryIpAddress': '10.1.0.1', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.1.0.2', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.1.0.3', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }], }] targetResult = [{ 'service': listServices[0], 'target_dir': '/backup/stack-test/test2', 'commands': [ "sh -c 'mysqldump -h 10.1.0.2 -u root test > /backup/stack-test/test2/test.dump'", ], 'environments': ['MYSQL_PWD:pass'], 'image': 'mariadb:latest' }] result = backupService.searchDump('/backup', listServices) self.assertEqual(targetResult, result) # When root password is setted listServices = [{ 'type': 'service', 'name': 'test2', 'state': 'active', 'launchConfig': { 'imageUuid': 'test/mariadb:latest', 'environment': { 'MYSQL_ROOT_PASSWORD': '******' } }, 'links': { 'stack': 'https://fake/stack', 'instances': 'https://fake/instances', }, 'stack': { 'name': 'stack-test' }, 'instances': [{ 'state': 'disabled', 'primaryIpAddress': '10.1.0.1', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.1.0.2', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.1.0.3', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }], }] targetResult = [{ 'service': listServices[0], 'target_dir': '/backup/stack-test/test2', 'commands': [ "sh -c 'mysqldump -h 10.1.0.2 -u root --all-databases > /backup/stack-test/test2/all-databases.dump'", ], 'environments': ['MYSQL_PWD:pass'], 'image': 'mariadb:latest' }] result = backupService.searchDump('/backup', listServices) self.assertEqual(targetResult, result) # When user, password, root password and database are setted listServices = [{ 'type': 'service', 'name': 'test2', 'state': 'active', 'launchConfig': { 'imageUuid': 'test/mariadb:latest', 'environment': { 'MYSQL_USER': '******', 'MYSQL_DATABASE': 'test', 'MYSQL_PASSWORD': '******', 'MYSQL_ROOT_PASSWORD': '******' } }, 'links': { 'stack': 'https://fake/stack', 'instances': 'https://fake/instances', }, 'stack': { 'name': 'stack-test' }, 'instances': [{ 'state': 'disabled', 'primaryIpAddress': '10.1.0.1', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.1.0.2', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.1.0.3', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }], }] targetResult = [{ 'service': listServices[0], 'target_dir': '/backup/stack-test/test2', 'commands': [ "sh -c 'mysqldump -h 10.1.0.2 -u user test > /backup/stack-test/test2/test.dump'", ], 'environments': ['MYSQL_PWD:pass'], 'image': 'mariadb:latest' }] result = backupService.searchDump('/backup', listServices) self.assertEqual(targetResult, result)
def testTemplateElasticsearch(self): backupService = Backup() listServices = [{ 'type': 'service', 'name': 'test', 'state': 'active', 'launchConfig': { 'imageUuid': 'elasticsearch:latest', 'environment': {} }, 'links': { 'stack': 'https://fake/stack', 'instances': 'https://fake/instances', }, 'stack': { 'name': 'stack-test' }, 'instances': [{ 'state': 'disabled', 'primaryIpAddress': '10.0.0.1', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.2', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }, { 'state': 'running', 'primaryIpAddress': '10.0.0.3', 'host': { 'name': 'host-1' }, 'links': { 'hosts': 'https://fake/hosts' } }], }] targetResult = [{ 'service': listServices[0], 'target_dir': '/tmp/backup/stack-test/test', 'commands': [ "-c 'rm -rf /tmp/backup/stack-test/test/*.json'", "-c 'elasticdump --input=http://10.0.0.2:9200/ --output=/tmp/backup/stack-test/test/dump_mapping.json --type=mapping'", "-c 'elasticdump --input=http://10.0.0.2:9200/ --output=/tmp/backup/stack-test/test/dump_data.json --type=data'", ], 'environments': [], 'entrypoint': "/bin/sh", 'image': 'taskrabbit/elasticsearch-dump:latest' }] result = backupService.searchDump('/tmp/backup', listServices) self.assertEqual(targetResult, result)