def setup_replicator(self): ''' Target: - clone a database in PostgreSQL. ''' connecter = self.get_connecter() self.logger.debug(Messenger.BEGINNING_EXE_REPLICATOR) replicator = self.get_replicator(connecter) pg_superuser = connecter.is_pg_superuser() if not pg_superuser: connecter.cursor.execute(Queries.GET_PG_DB_SOME_DATA, (replicator.original_dbname, )) db = connecter.cursor.fetchone() if db['owner'] != connecter.user: self.logger.stop_exe(Messenger.ACTION_DB_NO_SUPERUSER) # Terminate every connection to the database which is going to be # replicated, if necessary if self.args.terminate: terminator = Terminator(connecter, target_dbs=[replicator.original_dbname], logger=self.logger) terminator.terminate_backend_dbs([replicator.original_dbname]) # Clone the database replicator.replicate_pg_db() # Close connection to PostgreSQL connecter.pg_disconnect()
def setup_dropper(self): ''' Target: - delete specified databases in PostgreSQL. ''' connecter = self.get_connecter() self.logger.debug(Messenger.BEGINNING_EXE_DROPPER) dropper = self.get_dropper(connecter) # Terminate every connection to the target databases if necessary if self.args.terminate: terminator = Terminator(connecter, target_dbs=dropper.dbnames, logger=self.logger) terminator.terminate_backend_dbs(dropper.dbnames) # Delete the databases dropper.drop_pg_dbs(dropper.dbnames) # Close connection to PostgreSQL connecter.pg_disconnect()
def setup_vacuumer(self): ''' Target: - executes the vacuumer taking into account the value of its variables. ''' connecter = self.get_connecter() self.logger.debug(Messenger.BEGINNING_EXE_VACUUMER) vacuumer = self.get_vacuumer(connecter) # Check if the role of user connected to PostgreSQL is superuser pg_superuser = connecter.is_pg_superuser() if not pg_superuser: # Users who are not superusers will only be able to vacuum the # databases they own vacuumer.db_owner = connecter.user self.logger.warning(Messenger.ACTION_DB_NO_SUPERUSER) # Get PostgreSQL databases' names, connection permissions and owners dbs_all = connecter.get_pg_dbs_data(vacuumer.ex_templates, vacuumer.db_owner) # Show and log their names Orchestrator.show_dbs(dbs_all, self.logger) # Get the target databases in a list vacuum_list = DbSelector.get_filtered_dbs( dbs_all, vacuumer.in_dbs, vacuumer.ex_dbs, vacuumer.in_regex, vacuumer.ex_regex, vacuumer.in_priority, self.logger) # Terminate every connection to these target databases if necessary if self.args.terminate: terminator = Terminator(connecter, target_dbs=vacuum_list, logger=self.logger) terminator.terminate_backend_dbs(vacuum_list) # Vacuum the target databases vacuumer.vacuum_dbs(vacuum_list) # Close connection to PostgreSQL connecter.pg_disconnect()
def setup_alterer(self): ''' Target: - change the owner of the specified databases in PostgreSQL. ''' connecter = self.get_connecter() self.logger.debug(Messenger.BEGINNING_EXE_ALTERER) alterer = self.get_alterer(connecter) # Check if the role of user connected to PostgreSQL is superuser pg_superuser = connecter.is_pg_superuser() if not pg_superuser: # Users who are not superusers will only be able to backup the # databases they own owner = connecter.user self.logger.highlight('warning', Messenger.ACTION_DB_NO_SUPERUSER, 'yellow', effect='bold') else: owner = '' # Get PostgreSQL databases' names, connection permissions and owners dbs_all = connecter.get_pg_dbs_data(ex_templates=False, db_owner=owner) # Show and log their names Orchestrator.show_dbs(dbs_all, self.logger) # Get the target databases in a list alt_list = DbSelector.get_filtered_dbs( dbs_all=dbs_all, in_dbs=alterer.in_dbs, logger=self.logger) # Terminate every connection to the target databases if necessary if self.args.terminate: terminator = Terminator(connecter, target_dbs=alt_list, logger=self.logger) terminator.terminate_backend_dbs(alt_list) # Delete the databases alterer.alter_dbs_owner(alt_list) # Close connection to PostgreSQL connecter.pg_disconnect()
def setup_backer(self): ''' Target: - executes the backer depending on the type of backup to make, the role of the user who is connected to PostgreSQL and the rest of the conditions. It calls a terminator if necessary. ''' connecter = self.get_connecter() # Get databases or clusters' backer depending on the option selected # by the user in console if self.args.cluster: self.logger.debug(Messenger.BEGINNING_EXE_CL_BACKER) backer = self.get_cl_backer(connecter) else: self.logger.debug(Messenger.BEGINNING_EXE_DB_BACKER) backer = self.get_db_backer(connecter) # If necessary, add group and bkp_path to the mailer to be sent within # the process information if self.args.config_mailer: self.logger.mailer.add_group(backer.group) path = backer.bkp_path + backer.group self.logger.mailer.add_bkp_path(path) # Check if the role of user connected to PostgreSQL is superuser pg_superuser = connecter.is_pg_superuser() if not pg_superuser: if self.args.cluster is False: # Users who are not superusers will only be able to backup the # databases they own backer.db_owner = connecter.user self.logger.highlight( 'warning', Messenger.ACTION_DB_NO_SUPERUSER, 'yellow', effect='bold') else: # Backup the cluster can only be made by superuser self.logger.stop_exe(Messenger.ACTION_CL_NO_SUPERUSER) # Make the backups if self.args.cluster is False: # Backup databases # Get PostgreSQL databases' names, connection permissions and # owners dbs_all = connecter.get_pg_dbs_data(backer.ex_templates, backer.db_owner) # Show and log their names Orchestrator.show_dbs(dbs_all, self.logger) # Get the target databases in a list bkp_list = DbSelector.get_filtered_dbs( dbs_all, backer.in_dbs, backer.ex_dbs, backer.in_regex, backer.ex_regex, backer.in_priority, self.logger) # Terminate every connection to these target databases if necessary if self.args.terminate: terminator = Terminator(connecter, target_dbs=bkp_list, logger=self.logger) terminator.terminate_backend_dbs(bkp_list) backer.backup_dbs(bkp_list) # Make databases' backup else: # Backup a cluster # Terminate every connection to any database of the cluster if # necessary if self.args.terminate: terminator = Terminator(connecter, target_all=True, logger=self.logger) terminator.terminate_backend_all() backer.backup_cl() # Make cluster's backup # Close connection to PostgreSQL connecter.pg_disconnect()