Created
October 23, 2010 12:23
-
-
Save zerok/642146 to your computer and use it in GitHub Desktop.
A simple backup script for couchdbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Copyright (c) 2010, Horst Gutmann <[email protected]> | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright notice, | |
this list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
* Neither the name of the project's author nor the names of its contributors | |
may be used to endorse or promote products derived from this software | |
without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
------------------------------------------------------------------------------- | |
This is a simple script for backing up a couchdb with n history steps. | |
Configuration is handled by a simple configuration file passed as first | |
argument. It describes what database should be replicated to what machine | |
and how many historical copies should be created:: | |
[main] | |
parts=db1 | |
[DEFAULT] | |
history=3 | |
source_server=http://localhost:5984/ | |
target_server=http://whatever.couchone.com/ | |
[db1] | |
source=my_database | |
target=my_database_backup{0} | |
Both, source and target, can be either local database names or remote | |
addresses. The target has to support one placeholder for the number of the | |
backup copy. The history-setting allows you to specify how many historical | |
copies should be preserved. my_database_backup0 is the most recent backup | |
while my_database_backup3 (if history==3) is the oldest one. | |
To start the backup simply run `python couch_backup.py mybackup.cfg`, | |
Note that the script will try to create the target databases if the don't exist | |
yet. | |
**WARNING:** The created databases are by default public. If you don't want | |
that, create the required databases either in advance with the security | |
settings of your choice or change them after the automatical creation. | |
""" | |
import ConfigParser | |
import couchdb | |
import optparse | |
options = optparse.OptionParser() | |
options.add_option('--dry-run', action='store_true', dest='dryrun', | |
default=False) | |
opts, args = options.parse_args() | |
cfg = ConfigParser.ConfigParser() | |
cfg.read(args[0]) | |
def are_same_server(s1, s2): | |
if s1.rstrip('/') == s2.rstrip('/'): | |
return True | |
return False | |
parts = cfg.get('main', 'parts').split() | |
for part in parts: | |
source = cfg.get(part, 'source') | |
source_server = cfg.get(part, 'source_server') | |
target = cfg.get(part, 'target') | |
target_server = cfg.get(part, 'target_server') | |
try: | |
handler = cfg.get(part, 'handler') | |
except ConfigParser.NoOptionError, e: | |
handler = source_server | |
history = int(cfg.get(part, 'history')) | |
# Produce the final source and target addresses | |
if are_same_server(source_server, handler): | |
real_source = source | |
else: | |
real_source = source_server.rstrip('/') + '/' + source | |
if are_same_server(target_server, handler): | |
real_target = target | |
else: | |
real_target = target_server.rstrip('/') + '/' + target | |
# Open the target server and create all missing datbases | |
targetSrv = couchdb.Server(target_server) | |
for i in range(history+1): | |
print "Creating db {0}".format(target.format(i)) | |
if not opts.dryrun: | |
try: | |
targetSrv.create(target.format(i)) | |
except couchdb.http.PreconditionFailed, e: | |
pass | |
# Replicate using the handler server | |
handlerSrv = couchdb.Server(handler) | |
for i in reversed(range(history)): | |
print "Replicating {0} to {1}".format(target.format(i), | |
target.format(i+1)) | |
if not opts.dryrun: | |
targetSrv.replicate(target.format(i), target.format(i+1)) | |
print "Replicating {0} to {1}".format(real_source, real_target.format(0)) | |
if not opts.dryrun: | |
handlerSrv.replicate(real_source, real_target.format(0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment