5
# Synchronization databases management
14
configdir=`dirname $0`
15
config="${configdir}/`basename ${PROGRAM} .sh`rc"
22
# display this program's usage
25
usage: $PROGRAM [dbname] [command] [branch]
27
This script helps to manage synchro databases.
30
backup backup all databases prefixed by <dbname>_
31
restore restore all databases prefixed by <dbname>_ with a '.dump' suffix
32
drop drop all databases prefixed by <dbname>_
33
start launch a openerp server (need a config file '${config}')
34
stop stop a openerp server
35
<dbname> prefix of database to use
36
extract do a drop, restore and start for the given dbname (after an \
37
archive extraction, for an example)
38
<branch> path to the WM branch to use (only useful for start command)
47
# display an error and quit
53
# check if configuration file exists
55
if ! [ -f "${config}" ] ; then
56
error_and_exit "Configuration file not found: $config."
60
# check if some db with this prefix exists
62
db_exists=`psql -l|grep "$1_*"|wc -l`
63
if [ $db_exists -eq 0 ] ; then
64
error_and_exit "No database with '$1' prefix found!"
68
# backup all DB that have a prefix "${dbname}_"
71
for db in `psql template1 -t -c "SELECT datname from pg_database where datname ilike '${1}_%';"`; do
73
if [ -a "$dumpfile" ] ; then
74
error_and_exit "$dumpfile already exists! Aborted."
76
echo -e -n "Saving '$db' to $dumpfile: "
77
pg_dump -Fc $db > $dumpfile
82
# restore all .dump file that have a prefix with "${dbname}_"
84
for file in `ls ${1}_*`; do
85
# do not make process on directories
86
if [ -f $file ] ; then
87
db=`basename $file .dump`
88
exists=`psql template1 -t -c "SELECT COUNT(datname) from pg_database WHERE datname = '$db'"`
89
if [ $exists == 1 ] ; then
90
echo -n "$db exists. Deleting: "
91
dropdb $db || error_and_exit "An error occured @DB deletion: $db"
94
echo -e -n "Restoring $db: "
95
createdb $db && pg_restore -d $db $file >/dev/null 2>&1
101
# drop all DB that begins with the given prefix + _ char
104
for db in `psql template1 -t -c "SELECT datname from pg_database where datname ilike '${1}_%';"`; do
105
echo -n "Deleting $db: "
106
dropdb $db || error_and_exit "An error occured @DB deletion: $db"
111
# start a unifield server on free ports (between 8100 and 8200)
113
# check if configuration is here and right completed
116
for var in server addons web sync ; do
117
if [ -z ${!var} ] ; then
118
error_and_exit "Variable not found in configuration file ($config): ${var}."
120
# check directories existence
121
if ! [ -d ${!var} ] ; then
122
error_and_exit "Directory not found (${var} variable in $config file): ${!var}"
125
# check branch existence
126
if ! [ -d "$2" ] ; then
127
error_and_exit "Directory not found: $2"
130
echo -n "Searching free ports to launch server: "
131
read XMLRPCPORT NETRPCPORT WEBPORT <<<`netstat -anltp 2> /dev/null | perl -e '%port = ();
132
($min, $max) = (8100, 8200);
134
$port{$&} = 1 if m/:\K\d+\b/ and $& >= $min and $& <= $max;
136
for my $i ($min..$max) {
137
if( not exists $port{$i} and
138
not exists $port{$i+1} and
139
not exists $port{$i+2}) {
140
print join(" ", $i, $i+1, $i+2);
145
# Prepare files and directories
146
pidfile="${configdir}/tmp/$1.pid"
147
logfile="${configdir}/tmp/$1.log"
148
if [ -a "${configdir}/tmp" ] ; then
149
if ! [ -d "${configdir}/tmp" ] ; then
150
error_and_exit "Directory ${configdir}/tmp not found! Is that a file?"
153
echo -n "Creating ${configdir}/tmp directory: "
154
mkdir ${configdir}/tmp
158
echo -n "Launching openerp-server: "
159
LANG=C start-stop-daemon --start --pidfile ${pidfile} \
160
--background --make-pidfile --exec ${server}/bin/openerp-server.py -- \
161
--addons-path=${addons},${wm},${sync} --additional-xml --without-demo=all \
162
--logfile=${logfile} \
163
--no-xmlrpcs --no-xmlrpc --netrpc-port=${NETRPCPORT}
164
# Wait 15 second before launching MKDB script
167
echo -e "Ports used:\nxmlrpc: ${XMLRPCPORT}\nnetrpc: ${NETRPCPORT}"
168
echo "You can launch a web server using this command:"
169
echo "${web}/openerp-web.py --openerp-port=${NETRPCPORT}"
173
pidfile="${configdir}/tmp/$1.pid"
174
logfile="${configdir}/tmp/$1.log"
175
if ! [ -a "$pidfile" ] ; then
176
error_and_exit "PID file $pidfile not found!\nThis probably means that no server is working."
178
echo "Stopping server for $1"
179
start-stop-daemon --stop --quiet --pidfile $pidfile --oknodo
180
# Delete some files (PID and log)
181
rm -f $pidfile $logfile
188
# We need 2 params at least
189
if [ $# -lt 2 ] ; then
190
error "Need at least 2 parameters."
199
if [ -z "$branch" ] ; then
202
if [ -z ${wm} ] ; then
203
error_and_exit "Variable not found in configuration file ($config): wm."
225
start_serv "$prefix" "$branch"
231
drop "$prefix" && restore "$prefix" && start_serv "$prefix" "$branch"
237
error "Command $command not found!"