Commit 655d4a4b authored by Dave Lane's avatar Dave Lane

first commit

parents
*.conf
backups
This diff is collapsed.
Couchdb-tools
=============
Requires bash and jq (JSON Query tool) and the included couchdb-backup.sh tool
These tools provide scripts to
* backup/export (as JSON files) all the databases in a CouchDB instance, to which you have web-based access. Each backup is made in a designated backup directory, and in a dated directory within it, with a JSON file for each database exported as well as a list of the database names.
* delete all existing DBs in CouchDB instance (use carefully!)
* restore/import (from JSON files created by the backup process) databases into an existing CouchDB instance. Will not overwrite existing databases (they must be deleted to be replaced). When restoring you can choose a backup date from those previously backed up in your designated backup directory.
This set of tools was written to copy a set of databases from a production CouchDB instance and recreate the database on a local development server.
Each script has its own configuration file (with the same name as the relevant script) to help avoid inadvertently working on the wrong CouchDB.
Note: tested on Ubuntu Linux, but should on any system running bash and relevant dependencies
Provided by the Open Education Resource Foundation (http://oerfoundation.org), who are committed to using open source technologies to develop an openly licensed (CC-By or CC-By-SA) tertiary curriculum (assembled on http://wikieducator.org) for the benefit of learners and tertiary institutions of the world via the OERuniversitas project: http://oeru.org
See our Technology Blog for more open source projects: https://tech.oeru.org
# This is configuration for
# couchdb-backup-all.sh in this directory
COUCHBAK='[path to couchdb-backup.sh]' # probably './couchdb-backup.sh'
HOST=[URL to CouchDB server] # probably http://localhost
PORT=[CouchDB Port] # default would be 5984
USER=[CouchDB Username]
PW=[CouchDB Password]
BUDIR="[path to backup directory]" # I normally use "./backups"
#!/bin/bash
# Values to set in Conf file
# COUCHBAK="[path to couchdb-backup.sh]"
# HOST=[host] # http://localhost
# PORT=[port] # probably some variant of 5984
# USER=[username] # possibly 'admin'
# PW=[password]
# BUDIR="[path to backup directory]"
# read in config info, from the config
# file provided, or, if none is provided
# from the default file...
BASE=`basename -s .sh $0`
CONF=./${BASE}.conf
if test -r $CONF ; then
. $CONF
echo "Reading config file: $CONF"
else
echo "Config file $CONF does not exist!"
exit 1
fi
# standard commands
CURL=`which curl`
JQ=`which jq`
# Get the databases
DBS=`$CURL -X GET -H "Content-Type:application/json" ${HOST}:${PORT}/_all_dbs | $JQ -r '.[]|.'`
DATE=`date '+%Y%d%m_%H%M'`
if [ ! -d $BUDIR ] ; then
echo "creating $BUDIR..."
mkdir $BUDIR
fi
if [ ! -d $BUDIR/$DATE ] ; then
echo "creating backup dir $BUDIR/$DATE"
mkdir ${BUDIR}/${DATE}
fi
echo "copying database names into $BUDIR/$DATE/dbnames.txt"
echo "$DBS" > $BUDIR/$DATE/dbnames.txt
for DB in $DBS
do
BAK=${BUDIR}/${DATE}/${DB}-back-${DATE}.json
echo "backing up $DB at $DATE into $BAK using user $USER"
$COUCHBAK -b -H http://localhost -P 15984 -d $DB -f $BAK -u $USER -p $PW
done
This diff is collapsed.
# This is configuration for
# couchdb-delete-all.sh in this directory
HOST=[URL to CouchDB server] # probably http://localhost
PORT=[CouchDB Port] # default would be 5984
USER=[CouchDB Username]
PW=[CouchDB Password]
#!/bin/bash
# This removes all the DBs for an instance of CouchDB, usually to replace them for development purposes
# Values to set in Conf file
# HOST=[host] # http://localhost
# PORT=[port] # probably some variant of 5984
# USER=[username] # possibly 'admin'
# PW=[password]
# read in config info, from the config
# file provided, or, if none is provided
# from the default file...
BASE=`basename -s .sh $0`
CONF=./${BASE}.conf
if test -r $CONF ; then
. $CONF
echo "Reading config file: $CONF"
else
echo "Config file $CONF does not exist!"
exit 1
fi
CURL=`which curl`
EXISTING=`$CURL -X GET -H "Content-Type:application/json" ${HOST}:${PORT}/_all_dbs`
echo "Are you sure you want to remove the following databases:\n ${EXISTING}"
echo "On the http://${HOST}:${PORT}"
# requires user input
select yn in "Yes" "No"; do
case $yn in
Yes ) DELETE=true; break;;
No ) exit 1;;
esac
done
# Get the databases
if [ $DELETE ]; then
echo "Deleting $EXISTING..."
for DB in $DBS
do
if [[ ! "$EXISTING" = *"\"${DB}\""* ]]; then
echo "creating $DB"
$CURL -X PUT http://${USER}:${PW}@${HOST}:${PORT}/${DB}
echo "restoring database $DB"
FILES=( ${BUDIR}/${BU}/${DB}-back-*.json )
FILE=${FILES[-1]}
$CURL -d @${FILE} -X POST http://${USER}:${PW}@${HOST}:${PORT}/${DB}/_bulk_docs -H "Content-Type:application/json"
else
echo "$DB already exists, skipping"
fi
done
else
echo "dbnames.txt file NOT found in $BUDIR/$BU"
exit 1
fi
# This is configuration for
# couchdb-restore-all.sh in this directory
#
HOST=[URL to CouchDB server] # probably http://localhost
PORT=[CouchDB Port] # default would be 5984
USER=[CouchDB Username]
PW=[CouchDB Password]
BUDIR="[path to backup directory]" # I normally use "./backups"
#!/bin/bash
# Copyright 2016 OER Foundation
# Author: Dave Lane <dave@oerfoundation.org>
# see LICENSE for terms of reuse
# This script will create local JSON exports (suitable for re-importing) for each database in
# a couchdb instance. These files, placed in a dated directory within the designated backup directory
# will have a filename in the form dbname-backup-date_time.json
# read in config info, from the config
# file provided, or, if none is provided
# from the default file...
BASE=`basename -s .sh $0`
CONF=./${BASE}.conf
if test -r $CONF ; then
. $CONF
echo "Reading config file: $CONF"
else
echo "Config file $CONF does not exist!"
exit 1
fi
# standard commands
CURL=`which curl`
# Select the backup to restore
echo "Please select a backup to restore from the following dated backups"
BUS=`find $BUDIR -name "dbnames.txt" -exec dirname {} \;`
select BU in $BUS;
do
test -n "$BU" && break
echo ">>> Invalid Selection"
exit 1
done
# if we get here, a $BU has been selected
echo "Restoring from $BU"
# Get the databases
if [ -f "$BU/dbnames.txt" ]; then
DBS=`cat $BU/dbnames.txt`
EXISTING=`$CURL -X GET -H "Content-Type:application/json" ${HOST}:${PORT}/_all_dbs`
for DB in $DBS
do
if [[ ! "$EXISTING" = *"\"${DB}\""* ]]; then
echo "creating $DB"
$CURL -X PUT http://${USER}:${PW}@${HOST}:${PORT}/${DB}
echo "restoring database $DB"
FILES=( ${BU}/${DB}-back-*.json )
FILE=${FILES[-1]}
$CURL -d @${FILE} -X POST http://${USER}:${PW}@${HOST}:${PORT}/${DB}/_bulk_docs -H "Content-Type:application/json"
else
echo "$DB already exists, skipping"
fi
done
else
echo "dbnames.txt file NOT found in $BU"
exit 1
fi
# This is configuration for
# the scripts in this directory
#
# for each config, create a file with these details, named after the
# script file, e.g. couchdb-restore-all.sh -> couchdb-restore-all.config
#
# Values to set in Conf file
COUCHBAK="[path to couchdb-backup.sh]"
HOST=[host] # http://localhost
PORT=[port] # probably some variant of 5984
USER=[username] # possibly 'admin'
PW=[password]
BUDIR="[path to backup directory]"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment