cloudant / python-cloudant

A Python library for Cloudant and CouchDB
Apache License 2.0
163 stars 55 forks source link

Backing up #485

Closed Mradr closed 4 years ago

Mradr commented 4 years ago

I see there is a JS way of backing up the database - is there a way to backup the whole database as well using python-cloudant?

eiri commented 4 years ago

Hi @Mradr,

It's not that there is a JS way of backing up a database per se, it's more that our backup tool is written in javascript and happened to provide API.

There is no build-in backup functionality in python-cloudant. If you just need a backup we recommend to use couchbackup as CLI tool. If you looking for a programmatic solution with python it can be done with walking a database changes feed, something as a following snippet, with possible adjustment for documents batching, depending on a size of the target database


    client = Cloudant.iam(ACCOUNT_NAME, API_KEY, connect=True)
    db = client[DB_NAME]

    docs = []
    changes = db.changes(include_docs=True, style='all_docs')
    for change in changes:
        docs.append(change['doc'])

    with open("backup.json", "w") as backup:
        backup.write(json.dumps(docs))

    client.disconnect()