-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcron_postgresql.sh
executable file
·82 lines (70 loc) · 2.02 KB
/
cron_postgresql.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/sh
set -e
# Chosen action
ACTION=$1
# Options
# Name
NAME=cron_postgresql
# Binary: full path only
DAEMON="$PREFIX/bin/psql"
# Current directory
DIRECTORY="`pwd`"
# Check INI file is there
PGINI=./postgresql.ini
if [ ! -f $PGINI ]; then
echo "No PostgreSQL INI file found. Cancel."
exit 1
fi
# Check internet connection OK
wget -q --spider https://google.com
if [ ! $? -eq 0 ]; then
echo "No internet connection !"
exit 1
else
echo "connection ok"
fi
# Get PostgreSQL queries options from the ini file
PGACTIVE=$(sed -n '/^[ \t]*\[postgresql\]/,/\[/s/^[ \t]*active[ \t]*=[ \t]*//p' $PGINI)
PGCONNECTION=$(sed -n '/^[ \t]*\[postgresql\]/,/\[/s/^[ \t]*connection[ \t]*=[ \t]*//p' $PGINI)
PGACTION=$(sed -n '/^[ \t]*\[postgresql\]/,/\[/s/^[ \t]*run_action[ \t]*=[ \t]*//p' $PGINI)
REPEAT=$(sed -n '/^[ \t]*\[postgresql\]/,/\[/s/^[ \t]*repeat_minutes[ \t]*=[ \t]*//p' $PGINI)
# Check if synchro is active
if [ "$PGACTIVE" != "true" ]
then
echo "Action is not active. Cancel."
exit 0
fi
# Check if current minute of date corresponds to the repeat_minutes given in configuration
current_minute=$(date '+%-M')
REPEAT=$(($REPEAT + 0))
if [ $REPEAT -gt 0 ]
then
modulo="$(( current_minute % REPEAT ))"
modulo=$(($modulo + 0))
if [ $modulo -gt 0 ]
then
echo "Current minute does not correspond to given repeat minutes"
exit 0
fi
else
echo "Wrong value for repeat_minutes: $REPEAT"
fi
# Check schema lizsync exists
PGTEST="SELECT count(*) FROM information_schema.tables WHERE table_schema = 'lizsync' AND table_name = 'logged_relations'"
$DAEMON $PGCONNECTION -t -c "$PGTEST" -v "ON_ERROR_STOP=1" > sup
PGCHECK=$(cat sup | sed 's/[^0-9]//g')
rm sup
if [ $PGCHECK = 0 ]
then
echo "LizSync not installed. Nothing to do"
exit 0
fi
# config
CONFIGPG=".config-cron-postgresql"
echo "$PGACTION" > $DIRECTORY/$CONFIGPG
# Options for the binary call
DAEMON_OPTS="$PGCONNECTION -f $DIRECTORY/$CONFIGPG"
# Run daemon
echo "$NAME: $ACTION"
./run_daemon.sh $ACTION $NAME $DAEMON "$DAEMON_OPTS"
exit 0