src = Server(args.source) dest = Server(args.dest) count = 0 for dbname in src : db = src[dbname] if (len(dbname) >= 4 and dbname[:4] == "mica" ) or dbname == "_users" : try : newdb = dest[dbname] except couchdb.http.ResourceNotFound, e : dest.create(dbname) newdb = dest[dbname] security = db.security print "Copying " + str(dbname) + " security parameters: " + str(security) newdb.security = security if db.info()["doc_count"] != newdb.info()["doc_count"] : print "Replicating: " + str(dbname) src.replicate(args.source + "/" + dbname, args.dest + "/" + dbname, continuous = True) else : print "Already replicated: " + str(dbname) continue while db.info()["doc_count"] > newdb.info()["doc_count"] : print "Source count: " + str(db.info()["doc_count"]) + " dest count: " + str(newdb.info()["doc_count"]) sleep(5) count += 1 print "DBs: " + str(count)
def replicate(database, url, device, device_password, device_id, db_login, db_password, to_local=False, continuous=True, deleted=True, seq=None, ids=None): ''' Run a replication from a CouchDB database to a remote Cozy instance. Args: * *database*: Name of the local datbase. * *url*: Url of the remote Cozy. * *device*: name of the current device (that should be regitered to the remote Cozy). * *device_password*: password of the current device to connect to the Remote Cozy. * *device_id*: ID of the device. * *db_login*: * *db_password*: Optionl args: * *to_local*: if True, data go from remote Cozy to local Couch. * *continuous*: if False, it's a single shot replication. * *deleted*: if false deleted documents are not replicated. * *seq*: sequence number from where to start the replication. * *ids*: Document ids to replicate. ''' url = url.split('/') local = 'http://%s:%s@localhost:5984/%s' % \ (db_login, db_password, database) remote = "https://%s:%s@%s/cozy" % (device, device_password, url[2]) server = Server('http://localhost:5984/') if to_local: target = local source = remote else: target = remote source = local if deleted: filter_name = "%s/filter" % device_id else: filter_name = "%s/filterDocType" % device_id if seq is None and ids is None: server.replicate(source, target, continuous=continuous, filter=filter_name) elif seq is None: server.replicate(source, target, continuous=continuous, ids=ids) else: server.replicate(source, target, continuous=continuous, filter=filter_name, since_seq=seq) if continuous and to_local: logger.info( '[Replication] Continous replication to local database started.') elif continuous and not to_local: logger.info( '[Replication] Continous replication to remote Cozy started.') elif not continuous and to_local: logger.info( '[Replication] One shot replication to local database started.') elif not continuous and not to_local: logger.info( '[Replication] One shot replication to remote Cozy started.')
class CouchFSDocument(fuse.Fuse): ''' Fuse implementation behavior: handles synchronisation with database when a change occurs or when users want to access to his/her file system. ''' def __init__(self, mountpoint, uri=None, *args, **kwargs): ''' Configure file system, database and store remote Cozy informations. ''' # Configure fuse fuse.Fuse.__init__(self, *args, **kwargs) self.fuse_args.mountpoint = mountpoint self.fuse_args.add('allow_other') self.currentFile = "" # Configure database self.server = Server('http://*****:*****@localhost:5984/%s' % (username, password, DATABASE) url = self.urlCozy.split('/') target = "https://%s:%s@%s/cozy" % (self.loginCozy, self.passwordCozy, url[2]) self.rep = self.server.replicate(source, target, doc_ids=ids)