Skip to content

Instantly share code, notes, and snippets.

@4sushi
Created November 18, 2021 12:59
Show Gist options
  • Save 4sushi/79799e5e5a889c6363b0df1a4daae006 to your computer and use it in GitHub Desktop.
Save 4sushi/79799e5e5a889c6363b0df1a4daae006 to your computer and use it in GitHub Desktop.
from google.cloud import bigquery
from datetime import date
# You have to create a dataset in your BQ project before running this script, here the name is "archive"
ARCHIVE_DATASET = 'archive'
def archive_table(bq, dataset, table):
d = date.today()
table_src_id = '{}.{}.{}'.format(bq.project, dataset, table)
table_dest_id = '{}.{}.{}-{}-{:%Y-%m-%d}'.format(bq.project, ARCHIVE_DATASET, dataset, table, d)
bq.copy_table(table_src_id, table_dest_id)
bq.delete_table(table_src_id)
print('archive table "{}" to "{}"'.format(table_src_id, table_dest_id))
def archive_dataset(bq, dataset):
for table in bq.list_tables(dataset):
archive_table(bq, dataset, table.table_id)
bq.delete_dataset(dataset)
bq = bigquery.Client()
archive_dataset(bq, 'dataset_name')
archive_table(bq, 'dataset_name2', 'table_name')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment