Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 295 lines (244 sloc) 9.38 KB
#!/bin/bash
# RESET DSPACE CONTENT SCRIPT
#
# -------------------------------------------------------------------
# WARNING: BEFORE RUNNING THIS SCRIPT YOU MUST GENERATE A SET OF AIPS
# USING THE DSPACE AIP BACKUP/RESTORE TOOLS:
# https://wiki.duraspace.org/display/DSDOC18/AIP+Backup+and+Restore
#
# Specifically, you would need to export an ENTIRE SITE, by running
# something similar to:
# dspace packager -d -a -t AIP -e [admin-email] -i 10673/0 -o includeBundles=ORIGINAL,LICENSE -o passwords=true SITE@10673-0.zip
# -------------------------------------------------------------------
#
# This script does the following:
# 1. Stops Tomcat
# 2. Backs up current DB & Assetstore to "$BACKUP_DIR"
# 3. Deletes existing DB & Assetstore
# 4. Resets DSpace back to a 'fresh-install' state.
# 5. Imports the AIPs in "$AIP_DIR" into DSpace as default content
# (This also autocreates several epeople and groups)
# SEE README in ~/AIP-restore/ for info on updating these AIPs
# 6. Refreshes all Indexes & Restarts Tomcat
###################
## GLOBAL VARIABLES
###################
HOME="/home/dspace"
# Load current user's profile/environment
if [ -r $HOME/.profile ]; then
echo "Loading user environment from $HOME/.profile";
source $HOME/.profile
fi
# Location of final backup before destroying all content (~/tmp/data-backup)
BACKUP_DIR="$HOME/tmp/data-backup"
# Location of AIPs to restore
AIP_DIR="$HOME/AIP-restore"
# Name of the main "SITE" AIP
SITE_AIP_NAME="SITE@10673-0.zip"
# Initial Admin email
ADMIN_EMAIL="dspacedemo+admin@gmail.com"
# Postgres Version and Cluster name
PG_VERSION="9.5"
PG_CLUSTER="main"
###################
## GLOBAL FUNCTIONS
###################
# Check for error function (exits when error occurs)
checkerr()
{
#if previous command returns non-zero result, exit with an error.
if [ $? != 0 ]; then
# TODO: Should this send an email??
# Perhaps using 'mailx'? (sudo apt-get install heirloom-mailx)
echo ""
echo "ERROR OCCURRED! (see above)";
echo " Unable to continue with 'reset' of DSpace content."
echo " WARNING: DSpace may be in an unstable state!!!!!"
echo ""
echo " A backup of the data prior to this 'reset' is available at"
echo " $BACKUP_DIR"
echo " You should be able to restore that data by doing the following..."
echo " 1. dropdb dspace"
echo " 2. createdb -U dspace -E UNICODE dspace"
echo " 3. psql -U dspace dspace < $BACKUP_DIR/pg_dump.sql"
echo " 4. mv $HOME/dspace/assetstore $HOME/dspace/assetstore.BAK"
echo " 5. mv $BACKUP_DIR/assetstore $HOME/dspace/assetstore"
exit $?;
fi
}
# If -n flag passed in, then this script runs in "non-interactive" mode
if [ "$1" != "-n" ]; then
echo ""
echo "This script will REMOVE *ALL* CONTENT from DSpace running on demo.dspace.org"
echo "and REPLACE it with the contents of the AIPs in $AIP_DIR."
echo " (NOTE: If you wish to skip this message in the future, pass the '-n' flag"
echo " into this script, which causes it to run in non-interactive mode)"
echo ""
echo -n "Are you sure you want to continue? (y/n) "
read -e CONTINUE
if [ $CONTINUE != "Y" -a $CONTINUE != "y" ]; then
echo "Cancelling execution & exiting..."
exit 1;
fi
fi
echo ""
echo "====================================================================="
echo "BEGINNING RESET of DSpace back to the AIP content in $AIP_DIR"
echo "====================================================================="
echo "Start Date/Time: `date`"
START_TIME=`date +%s`
echo ""
# --------------------------------------
# Step 1 : Stop Tomcat & reboot Postgres
# --------------------------------------
sudo service tomcat7 stop
checkerr
#echo "Stopping JETTY: "
#$HOME/bin/stop-jetty
#checkerr
echo "ok"
echo ""
# We reboot Postgres in order to kill any other sessions that may exist
# We must reboot Postgres WITHOUT using the 'service' (/etc/init.d) script
# so that we can specify the '-w' (wait) flag.
echo "Stopping PostgreSQL: "
# Stop Postgres quickly & wait until completely stopped (-w)
sudo /usr/bin/pg_ctlcluster $PG_VERSION $PG_CLUSTER stop -m fast -- -s -W
#$PGCTL stop -D "$PGDATA" -s -m fast -w
checkerr
echo "ok"
echo ""
echo "Starting PostgreSQL: "
# Start Postgres and wait until completely started (-w)
sudo /usr/bin/pg_ctlcluster $PG_VERSION $PG_CLUSTER start -- -w
#$PGCTL start -D "$PGDATA" -w >>$PGLOG 2>&1
checkerr
echo "ok"
echo ""
# --------------------------------------
# Step 2 : One final Backup (just in case)
# --------------------------------------
# Perform one last backup to ~/tmp/[date]
echo "Creating a backup of existing DSpace content"
# If backup directory already exists, delete it
if [ -d "$BACKUP_DIR" ]; then
echo "... Deleting existing $BACKUP_DIR temp directory";
rm -rf $BACKUP_DIR
checkerr
fi
echo "... Creating $BACKUP_DIR temp directory"
mkdir -p $BACKUP_DIR
checkerr
echo "... Performing full database dump to $BACKUP_DIR/pg_dump.sql"
/usr/bin/pg_dump -U dspace -E UNICODE dspace > $BACKUP_DIR/pg_dump.sql
checkerr
echo ""
# --------------------------------------
# Step 3 : Delete ALL existing content!
# --------------------------------------
echo "Removing all DSpace content (files + metadata)"
# Delete all data in the triplestore
echo "... Deleting all data in the triplestore"
$HOME/dspace/bin/dspace rdfizer --delete-all
#checkerr
# Move existing 'assetstore' folder (all content files)
echo "... Moving existing 'assetstore' to $BACKUP_DIR"
mv $HOME/dspace/assetstore $BACKUP_DIR
checkerr
# Drop the existing 'dspace' database
echo "... Dropping existing 'dspace' database"
/usr/bin/dropdb -U dspace dspace
checkerr
echo ""
# --------------------------------------
# Step 4 : Re-initialize an empty DSpace
# --------------------------------------
echo "Resetting DSpace back to a 'fresh-install' state"
# Recreate an empty 'assetstore' folder
echo "... Creating empty 'assetstore' folder"
mkdir $HOME/dspace/assetstore
checkerr
# Recreate an empty 'dspace' database
echo "... Creating empty 'dspace' database"
/usr/bin/createdb -h localhost -O dspace -U postgres -E UNICODE dspace
#/usr/bin/createdb -U dspace -E UNICODE dspace
checkerr
echo "... Creating the pgcrypto extension"
/usr/bin/psql -h localhost -U postgres -c "CREATE EXTENSION pgcrypto" dspace
checkerr
echo ""
echo "... Running migrations to assemble the tables"
# NOTE: This not only creates the tables, but also triggers loading Registries & creating default Groups
$HOME/dspace/bin/dspace database migrate
checkerr
echo ""
echo "... Creating initial administrative user ($ADMIN_EMAIL)"
$HOME/dspace/bin/dspace create-administrator -e $ADMIN_EMAIL -f Demo -l Administrator -c en -p dspace
checkerr
echo ""
# ----------------------------------------------
# Step 5 : Restore entire Site from backup AIPs
# ----------------------------------------------
echo "Recursively restoring content from AIPs at $AIP_DIR (starting with $SITE_AIP_NAME AIP)"
#create upload directory as it will be used as temporary directory
mkdir -p $HOME/dspace/upload
# This packager command performs a recursive AIP ingest, starting with the Site AIP
# * It creates missing metadata fields (createMetadataFields)
# * It also skips over any "could not find a parent DSpaceObject" errors (skipIfParentMissing)
# * Finally it runs in following modes:
# -u = non-interactive mode
# -r = restore mode
# -a = all mode (recursive restore)
# -f = force replace existing objects
# For more details see: https://wiki.duraspace.org/display/DSDOC18/AIP+Backup+and+Restore
$HOME/dspace/bin/dspace packager -u -r -a -f -t AIP -e $ADMIN_EMAIL -i 10673/0 -o skipIfParentMissing=true -o createMetadataFields=true $AIP_DIR/$SITE_AIP_NAME
checkerr
echo "Resetting database sequences (via update-sequences.sql)..."
/usr/bin/psql -U dspace -f $HOME/dspace/etc/postgres/update-sequences.sql dspace > /dev/null
checkerr
echo ""
# --------------------------------------
# Step 6 : Start up Tomcat
# --------------------------------------
#$HOME/bin/start-jetty
#checkerr
#echo ""
echo "Starting Tomcat..."
sudo service tomcat7 start
checkerr
echo ""
# --------------------------------------
# Step 8 : Rebuild/Reset Discovery indexes
# --------------------------------------
echo "Rebuilding Discovery (Solr) Indexes..."
# Note: the '-b' option tells Discovery to rebuild entire index (wiping out existing one)
$HOME/dspace/bin/dspace index-discovery -b
checkerr
# --------------------------------------
# Step 9 : Rebuild/Reset OAI indexes
# --------------------------------------
echo "Rebuilding OAI-PMH (Solr) Indexes..."
# NOTE: the '-c' option clears out existing items in the index.
$HOME/dspace/bin/dspace oai import -c
# Also immediately clean the cache
$HOME/dspace/bin/dspace oai clean-cache
checkerr
# --------------------------------------
# Step 10 : Rebuild/Reset Solr Statistics indexes
# --------------------------------------
echo "Rebuilding Statistics (Solr) Indexes..."
# NOTE: the '-b' and '-r' reindex all bitstreams and remove deleted ones. '-o' re-optimizes the index.
# (Unfortunately these have to be separate calls, as they are mutually exclusive)
$HOME/dspace/bin/dspace stats-util -b -r
checkerr
$HOME/dspace/bin/dspace stats-util -o
checkerr
echo ""
echo ""
echo "SUCCESS! The DSpace 'reset' is now complete!"
echo ""
echo " (However, should you notice any issues, you can restore DSpace to its previous state using the data backup at: $BACKUP_DIR)"
echo ""
echo "FINISHED at: `date`"
END_TIME=`date +%s`
echo "Elapsed Time: `expr $END_TIME - $START_TIME` seconds"
You can’t perform that action at this time.