CouchDB查看移动

时间:2018-03-05 15:30:27

标签: couchdb

我知道只需复制数据即可备份数据 到某些存储(在Linux上,它们通常安装在 /var/lib/couchdb/*.couch)。同样在同一目录中的是数据 由观点索引。我也知道你可以制作一份视图副本 将HTTP请求发送到另一个数据库。但我不知道的是,是吗? 只需将视图复制到类似的存储空间即可保存视图 有数据吗?让我的问题更清楚一点:你能做出来吗? 没有其他数据库实例的视图备份?另外,我正在使用 CouchDB 1.6具有不同版本的文件结构 比2。

1 个答案:

答案 0 :(得分:0)

让我发一个答案,这只是我能想到的解决方案......当然可以改进或改变但它对我有用。 需要使用

从终端调用脚本(除非您在某些IDE中编辑它)
  

sys.argv [1]喜欢 python3 viewBackup.py 1 python3 viewBackup.py 2

(1是制作备份文件,2是上传该备份)。由于此脚本非常简单,因此无需检查备份文件是否已存在(如果有备份)

import requests
import json
import sys

# these are credentials of DB which views we want to backup
from_db = 'http://localhost:5984/'
from_user = 'root'
from_pswd = '1234'
from_auths = (from_user, from_pswd)

#`and these are credentials of DB where we want to upload our backuped views`
to_db = 'http://localhost:5984/'
to_user = 'root'
to_pswd = '1234'
to_auths = (from_user, from_pswd)

tf = 'bucket2views.json'




def getViews(source, credS):
    global tf
    data = _getDBList(source, credS)
    temp = []

    f = open(tf, 'w')
    for d in data:
        designs = requests.get(source + d + '/_all_docs?startkey="_design/"&endkey="_desing0"&include_docs=true',auth=credS).json()

        designs = designs['rows']


        for z in designs:
            z = z['doc']['_id'].split('/')
            z = z[1]
            try:

                print("getting views, it might take some time, please be patient")
                views = requests.get(source + d + '/_design/' + z, auth=credS).json()['views']
                print(" views gathered, start processing them")
                views2 = {'views': views}
                payload = json.dumps(views2)
                temp.append((d, z, payload))

            except Exception as e:
                pass


    f.write(json.dumps(temp))
    f.close()
    print(' views has been saved')

def _getDBList(server, auth):
    response = requests.get(server + '_all_dbs', auth=auth).json()
    return response

def uploadViews(srv, cred):
    f = open(tf, 'r')
    txt = f.read()
    f.close()

    jt = json.loads(txt)

    for item in jt:
        d = item[0]
        z = item[1]
        full_name = item[2]
        resp = requests.put(srv+ d +'/_design/'+ z, data=json.dumps(payload), auth=cred).json()
        print (json.dumps(resp))

def main():
    ans = sys.argv[1]
    # ans = 2
    if ans in [1, '1', 'getViews']:
        getViews(from_db, from_auths)

    elif ans in [2, '2', "uploadViews"]:
        uploadViews(to_db, to_auths)

    else:
        print('Possible inputs: 1, getViews, 2, uploadViews')

main()