Ticket #40: oml2-irods-dumper.sh

File oml2-irods-dumper.sh, 3.0 KB (added by Olivier Mehani, 11 years ago)
Line 
1#!/bin/bash
2#
3# Script dumping an OML database to SQL text and storing it into IRODS.
4# Copyright 2012-2013 National ICT Australia (NICTA), Australia
5#
6# Permission is hereby granted, free of charge, to any person obtaining a copy
7# of this software and associated documentation files (the "Software"), to deal
8# in the Software without restriction, including without limitation the rights
9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10# copies of the Software, and to permit persons to whom the Software is
11# furnished to do so, subject to the following conditions:
12#
13# The above copyright notice and this permission notice shall be included in
14# all copies or substantial portions of the Software.
15#
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22# THE SOFTWARE.
23#
24irodsHost=irods.example.com
25#irodsPort=1247
26export irodsHost irodsPort
27
28#BACKEND=sqlite
29SQLITEDATAPATH=/var/lib/oml2
30BACKEND=postgresql
31PGDATAPATH=oml2@localhost:5432
32
33LOGFILE=oml2-irods-dumper.log
34function log ()
35{
36        echo "$@" >&2
37        echo "$@" >> ${LOGFILE}
38}
39
40IPUT=iput
41SQLITE3=sqlite3
42PGDUMP=pg_dump
43
44CONT=0
45while [ $CONT = 0 ] ; do
46        case "$1" in
47                --id)
48                        log "WARN       --id should be --domain"
49                        ;&
50                --domain)
51                        shift
52                        DOMAIN=$1
53                        ;;
54                --token)
55                        shift
56                        ITICKET=$1
57                        ;;
58                --path)
59                        shift
60                        IPATH=$1
61                        ;;
62                "")
63                        ;;
64                *)
65                        log "WARN       Unknown parameter '$1'"
66                        ;;
67        esac
68        shift
69        CONT=$?
70done
71
72if [ -z "${DOMAIN}" -o -z "${IPATH}" ]; then
73        log "ERROR      Missing parameters"
74        log "usage: $0 --domain DOMAIN --path IPATH [--token ITICKET]"
75        exit 1
76fi
77
78case ${BACKEND} in
79        sqlite)
80                DB="file:${SQLITEDATAPATH}/${DOMAIN}.sq3"
81                ;;
82        postgresql)
83                DB="postgresql://${PGDATAPATH}/${DOMAIN}"
84                ;;
85        *)
86                log "ERROR      Unknown DB backend '${BACKEND}'"
87                exit 1
88                ;;
89esac
90
91case "${DB}" in
92        file:*)
93                DBNAME=${DB/file:/}
94                DBFILE=/tmp/${DBNAME//\/}.`date +%Y-%m-%d_%H:%M:%S%z`.sqlite.sql
95                log "INFO       Dumping SQLite3 DB ${DBNAME} as ${DBFILE} and pushing to iRODS"
96                ${SQLITE3} ${DBNAME} .dump > ${DBFILE}
97                ;;
98        postgresql://*)
99                # Separate the components of the URI by gradually eating them off the TMP variable
100                TMP="${DB/postgresql:\/\//}"
101                USER=${TMP/@*/}
102                TMP=${TMP/${USER}@/}
103                HOST=${TMP/:*/}
104                TMP=${TMP/${HOST}:/}
105                PORT=${TMP/\/*/}
106                TMP=${TMP/${PORT}\//}
107                DBNAME=${TMP}
108                DBFILE=/tmp/${DBNAME}.`date +%Y-%m-%d_%H:%M:%S%z`.pg.sql
109                log "INFO       Dumping PostgreSQL DB ${DBNAME} as ${DBFILE} and pushing to iRODS"
110                ${PGDUMP} -U ${USER} -h ${HOST} -p ${PORT} ${DBNAME} > ${DBFILE}
111                ;;
112        *)
113                log "ERROR      Don't know how to handle ${DB}"
114                ;;
115esac
116log "INFO       Pushing ${DBFILE} to irods://irodsHost:irodsPort${IPATH}"
117${IPUT} ${ITICKET:+-t ${ITICKET}} ${DBFILE} ${IPATH}
118rm ${DBFILE}