~jfb-tempo-consulting/unifield-wm/sync-env-py3

« back to all changes in this revision

Viewing changes to manage_syncdb.sh

  • Committer: Olivier DOSSMANN
  • Date: 2013-09-17 07:21:50 UTC
  • Revision ID: od@tempo-consulting.fr-20130917072150-gld4t6ckxstgfvvb
[NEW] Scripts that create a synchronization environment, dump files and create an archive with dumps + some scripts to restore DB and launch an OpenERP server

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env bash
 
2
#
 
3
# manage_syncdb.sh
 
4
#
 
5
# Synchronization databases management
 
6
#
 
7
 
 
8
#####
 
9
## VARIABLES
 
10
###
 
11
 
 
12
PROGRAM=`basename $0`
 
13
VERSION="0.0.0"
 
14
config="`basename ${PROGRAM} .sh`rc"
 
15
current="$PWD"
 
16
 
 
17
#####
 
18
## FUNCTIONS
 
19
###
 
20
 
 
21
# display this program's usage
 
22
usage() {
 
23
  cat << EOF
 
24
usage: $PROGRAM [command] [dbname]
 
25
 
 
26
This script helps to manage synchro databases.
 
27
 
 
28
COMMANDS:
 
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)
 
37
EOF
 
38
}
 
39
 
 
40
# display an error
 
41
error() {
 
42
  echo -e "$1"
 
43
}
 
44
 
 
45
# display an error and quit
 
46
error_and_exit() {
 
47
  error "$1"
 
48
  exit 1
 
49
}
 
50
 
 
51
# check if configuration file exists
 
52
check_config() {
 
53
  if ! [ -f "${config}" ] ; then
 
54
    error_and_exit "Configuration file not found: $config."
 
55
  fi
 
56
}
 
57
 
 
58
# check if some db with this prefix exists
 
59
check_db() {
 
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!"
 
63
fi
 
64
}
 
65
 
 
66
# backup all DB that have a prefix "${dbname}_"
 
67
backup() {
 
68
  check_db "$1"
 
69
  for db in `psql template1 -t -c "SELECT datname from pg_database where datname ilike '${1}_%';"`; do
 
70
    dumpfile="${db}.dump"
 
71
    if [ -a "$dumpfile" ] ; then
 
72
      error_and_exit "$dumpfile already exists! Aborted."
 
73
    fi
 
74
    echo -e -n "Saving '$db' to $dumpfile: "
 
75
    pg_dump -Fc $db > $dumpfile
 
76
    echo "DONE."
 
77
  done
 
78
}
 
79
 
 
80
# restore all .dump file that have a prefix with "${dbname}_"
 
81
restore() {
 
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"
 
88
      echo "DONE."
 
89
    fi
 
90
    echo -e -n "Restoring $db: "
 
91
    createdb $db && pg_restore -d $db $file >/dev/null 2>&1
 
92
    echo "DONE."
 
93
  done
 
94
}
 
95
 
 
96
# drop all DB that begins with the given prefix + _ char
 
97
drop() {
 
98
  check_db "$1"
 
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"
 
102
    echo "DONE."
 
103
  done
 
104
}
 
105
 
 
106
# start a unifield server on free ports (between 8100 and 8200)
 
107
start_serv() {
 
108
  # check if configuration is here and right completed
 
109
  check_config
 
110
  source "$config"
 
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}."
 
114
    fi
 
115
    # check directories existence
 
116
    if ! [ -d ${!var} ] ; then
 
117
      error_and_exit "Directory not found (${var} variable in $config file): ${!var}"
 
118
    fi
 
119
  done
 
120
  # search free ports
 
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);
 
124
  while(<>) {
 
125
    $port{$&} = 1 if m/:\K\d+\b/ and $& >= $min and $& <= $max;
 
126
  }
 
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);
 
132
      last;
 
133
    }
 
134
  }'`
 
135
  echo "DONE."
 
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?"
 
142
    fi
 
143
  else
 
144
    echo -n "Creating ${current}/tmp directory: "
 
145
    mkdir ${current}/tmp
 
146
    echo "DONE."
 
147
  fi
 
148
  touch ${logfile}
 
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
 
156
  sleep 15s
 
157
  echo "DONE."
 
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}"
 
161
}
 
162
 
 
163
stop_serv() {
 
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!"
 
168
  fi
 
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
 
173
}
 
174
 
 
175
#####
 
176
## TESTS
 
177
###
 
178
 
 
179
# We need 2 params at least
 
180
if [ $# -lt 2 ] ; then
 
181
  error "Need at least 2 parameters."
 
182
  usage
 
183
  exit 1
 
184
fi
 
185
 
 
186
prefix=$1
 
187
command=$2
 
188
 
 
189
#####
 
190
## MAIN
 
191
###
 
192
 
 
193
# Check command
 
194
case $command in 
 
195
  backup)
 
196
  backup "$prefix"
 
197
  ;;
 
198
  restore)
 
199
  restore "$prefix"
 
200
  ;;
 
201
  drop)
 
202
  drop "$prefix"
 
203
  ;;
 
204
  start)
 
205
  start_serv "$prefix"
 
206
  ;;
 
207
  stop)
 
208
  stop_serv "$prefix"
 
209
  ;;
 
210
  extract)
 
211
  drop "$prefix" && restore "$prefix" && start_serv "$prefix"
 
212
  ;;
 
213
  help)
 
214
  usage
 
215
  ;;
 
216
  *)
 
217
  error "Command $command not found!"
 
218
  usage
 
219
  exit 1
 
220
  ;;
 
221
esac
 
222
 
 
223
exit 0