def back_up_database(self, backup_storage, temp_backup_path):
logger.info('Start backing up the database.')
file_path = '{database}_{timestamp}.dump'.format(
database=settings.DATABASES['default']['NAME'],
timestamp=self.timestamp
)
temp_file_path = '{backup_path}/{file_path}'.format(backup_path=temp_backup_path, file_path=file_path)
# Run the `pg_dump` command.
os.system('pg_dump -h {host} -U {user} {database} > {file_path}'.format(
host=settings.DATABASES['default']['HOST'],
user=settings.DATABASES['default']['USER'],
database=settings.DATABASES['default']['NAME'],
file_path=temp_file_path
))
# Store the dump file on the backup bucket.
with open(temp_file_path, 'rb') as database_backup_file:
target_file_path = '{timestamp}/{path}'.format(timestamp=self.timestamp, path=file_path)
backup_storage.save(target_file_path, database_backup_file)
logger.info('Database dump successfully copied to the target storage backend.')
评论列表
文章目录