Created
August 7, 2020 22:03
-
-
Save wallacesilva/c263cba3c240e13a39d21b762feb1690 to your computer and use it in GitHub Desktop.
Postgresql pg_dump parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
from urllib.parse import urlparse | |
def parse_command(database_url, exclude_tables): | |
parsed_url = urlparse(database_url) | |
parsed_data = { | |
'hostname': parsed_url.hostname, | |
'username': parsed_url.username, | |
'password': parsed_url.password, | |
'database': parsed_url.path.replace('/', ''), | |
'exclude_tables': '', | |
} | |
if exclude_tables: | |
parsed_data['exclude_tables'] = ' '.join(['--exclude-table-data={}'.format(table) for table in exclude_tables]) | |
return parsed_data | |
parser = argparse.ArgumentParser() | |
parser.add_argument('database_url', help='Postgresql DATABASE_URL', type=str) | |
parser.add_argument('--exclude_tables', help='Exclude table data from backup', nargs='*') | |
args = parser.parse_args() | |
database_url = args.database_url | |
exclude_tables = args.exclude_tables | |
command = 'PGPASSWORD={password} pg_dump -v -U {username} -Fc -h {hostname} {exclude_tables} {database} > database_backup.dump' | |
parsed_data = parse_command(database_url, exclude_tables) | |
print(command.format(**parsed_data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment