5
# Synchronization databases management
14
config="`basename ${PROGRAM} .sh`rc"
21
# display this program's usage
24
usage: $PROGRAM [command] [dbname]
26
This script helps to manage synchro databases.
29
backup backup all databases prefixed by <dbname>_
30
restore restore all databases prefixed by <dbname>_ with a '.dump' suffix
31
drop drop all databases prefixed by <dbname>_
32
start launch a openerp server (need a config file '${config}')
33
stop stop a openerp server
34
<dbname> prefix of database to use
35
extract do a drop, restore and start for the given dbname (after an \
36
archive extraction, for an example)
45
# display an error and quit
51
# check if configuration file exists
53
if ! [ -f "${config}" ] ; then
54
error_and_exit "Configuration file not found: $config."
58
# check if some db with this prefix exists
60
db_exists=`psql -l|grep "$1_*"|wc -l`
61
if [ $db_exists -eq 0 ] ; then
62
error_and_exit "No database with '$1' prefix found!"
66
# backup all DB that have a prefix "${dbname}_"
69
for db in `psql template1 -t -c "SELECT datname from pg_database where datname ilike '${1}_%';"`; do
71
if [ -a "$dumpfile" ] ; then
72
error_and_exit "$dumpfile already exists! Aborted."
74
echo -e -n "Saving '$db' to $dumpfile: "
75
pg_dump -Fc $db > $dumpfile
80
# restore all .dump file that have a prefix with "${dbname}_"
82
for file in `ls ${1}_*`; do
83
db=`basename $file .dump`
84
exists=`psql template1 -t -c "SELECT COUNT(datname) from pg_database WHERE datname = '$db'"`
85
if [ $exists == 1 ] ; then
86
echo -n "$db exists. Deleting: "
87
dropdb $db || error_and_exit "An error occured @DB deletion: $db"
90
echo -e -n "Restoring $db: "
91
createdb $db && pg_restore -d $db $file >/dev/null 2>&1
96
# drop all DB that begins with the given prefix + _ char
99
for db in `psql template1 -t -c "SELECT datname from pg_database where datname ilike '${1}_%';"`; do
100
echo -n "Deleting $db: "
101
dropdb $db || error_and_exit "An error occured @DB deletion: $db"
106
# start a unifield server on free ports (between 8100 and 8200)
108
# check if configuration is here and right completed
111
for var in wm server addons web sync ; do
112
if [ -z ${!var} ] ; then
113
error_and_exit "Variable not found in configuration file ($config): ${var}."
115
# check directories existence
116
if ! [ -d ${!var} ] ; then
117
error_and_exit "Directory not found (${var} variable in $config file): ${!var}"
121
echo -n "Searching free ports to launch server: "
122
read XMLRPCPORT NETRPCPORT WEBPORT <<<`netstat -anltp 2> /dev/null | perl -e '%port = ();
123
($min, $max) = (8100, 8200);
125
$port{$&} = 1 if m/:\K\d+\b/ and $& >= $min and $& <= $max;
127
for my $i ($min..$max) {
128
if( not exists $port{$i} and
129
not exists $port{$i+1} and
130
not exists $port{$i+2}) {
131
print join(" ", $i, $i+1, $i+2);
136
# Prepare files and directories
137
pidfile="${current}/tmp/$1.pid"
138
logfile="${current}/tmp/$1.log"
139
if [ -a "${current}/tmp" ] ; then
140
if ! [ -d "${current}/tmp" ] ; then
141
error_and_exit "Directory ${current}/tmp not found! Is that a file?"
144
echo -n "Creating ${current}/tmp directory: "
149
echo -n "Launching openerp-server: "
150
LANG=C start-stop-daemon --start --pidfile ${pidfile} \
151
--background --make-pidfile --exec ${server}/bin/openerp-server.py -- \
152
--addons-path=${addons},${wm},${sync} --additional-xml --without-demo=all \
153
--logfile=${logfile} \
154
--no-xmlrpcs --no-xmlrpc --netrpc-port=${NETRPCPORT}
155
# Wait 15 second before launching MKDB script
158
echo -e "Ports used:\nxmlrpc: ${XMLRPCPORT}\nnetrpc: ${NETRPCPORT}"
159
echo "You can launch a web server using this command:"
160
echo "${web}/openerp-web.py --openerp-port=${NETRPCPORT}"
164
pidfile="${current}/tmp/$1.pid"
165
logfile="${current}/tmp/$1.log"
166
if ! [ -a "$pidfile" ] ; then
167
error_and_exit "PID file $pidfile not found!"
169
echo "Stopping server for $1"
170
start-stop-daemon --stop --quiet --pidfile $pidfile --oknodo
171
# Delete some files (PID and log)
172
rm -f $pidfile $logfile
179
# We need 2 params at least
180
if [ $# -lt 2 ] ; then
181
error "Need at least 2 parameters."
211
drop "$prefix" && restore "$prefix" && start_serv "$prefix"
217
error "Command $command not found!"