From d2c84906d0ce4810a6011f43d7a45195ef131f1c Mon Sep 17 00:00:00 2001 From: Robert Lucey Date: Sat, 5 Aug 2023 15:17:47 +0100 Subject: [PATCH] Add backup tool --- setup.py | 5 ++-- src/via/bin/backup_raw_journeys.py | 39 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/via/bin/backup_raw_journeys.py diff --git a/setup.py b/setup.py index 312e378..1af7d0b 100644 --- a/setup.py +++ b/setup.py @@ -34,8 +34,9 @@ install_requires=INSTALL_REQUIRES, entry_points={ 'console_scripts': [ - 'pull_journeys = via.bin.pull_journeys:main', - 'generate_geojson = via.bin.generate_geojson:main' + 'via_pull_journeys = via.bin.pull_external_journeys:main', + 'via_generate_geojson = via.bin.generate_geojson:main', + 'via_backup_raw_journeys = via.bin.backup_raw_journeys:main' ] } ) diff --git a/src/via/bin/backup_raw_journeys.py b/src/via/bin/backup_raw_journeys.py new file mode 100644 index 0000000..017406b --- /dev/null +++ b/src/via/bin/backup_raw_journeys.py @@ -0,0 +1,39 @@ +import argparse +import json + + +def rm_obj_id(journey_data): + del journey_data["_id"] + return journey_data + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument( + "backup_file", + help="Path to the backup json file", + ) + args = parser.parse_args() + + if not args.backup_file.endswith(".json"): + raise ValueError("backup file must be a .json file") + + from via.utils import get_mongo_interface + from via.settings import MONGO_RAW_JOURNEYS_COLLECTION + + mongo_interface = get_mongo_interface() + with open(args.backup_file, "w") as fh: + fh.write( + json.dumps( + [ + rm_obj_id(journey_data) + for journey_data in getattr( + mongo_interface, MONGO_RAW_JOURNEYS_COLLECTION + ).find() + ] + ) + ) + + +if __name__ == "__main__": + main()