-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer_postgis_db.sh
executable file
·91 lines (70 loc) · 1.55 KB
/
transfer_postgis_db.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
83
84
85
86
87
88
89
90
91
#!/bin/bash
usage()
{
cat << EOF
usage: $0 options
This script transfer a postgis database from one server to another
OPTIONS:
-h
Show this message
-o
Origin server
-d
Destination server
Default value localhost
-db
Database name
-ow
Owner
Default value postgres
-dir
Backup folder
Default value /var/tmp
EOF
}
ORIGIN=
DESTINATION="localhost"
DBNAME=
OWNER="postgres"
BKPFOLDER="/var/tmp"
PGISRESTORE=$(pg_config --sharedir)/contrib/postgis-2.2/postgis_restore.pl
# A string with command options
OPTIONS=$@
# An array with all the arguments
ARGUMENTS=($OPTIONS)
# Loop index
IDX=0
for OPT in $OPTIONS
do
# Incrementing index
IDX=`expr $IDX + 1`
case "$OPT" in
-h)
usage; exit 1;;
-o)
ORIGIN=${ARGUMENTS[$IDX]};;
-d)
DESTINATION=${ARGUMENTS[$IDX]};;
-f)
BKPFOLDER=${ARGUMENTS[$IDX]};;
-db)
DBNAME=${ARGUMENTS[$IDX]};;
-ow)
OWNER=${ARGUMENTS[$IDX]};;
esac
done
if [[ -z $ORIGIN ]] || [[ -z $DESTINATION ]] || [[ -z $DBNAME ]] || [[ -z $OWNER ]]
then
usage
exit 1
fi
# get data from server
pg_dump -h $ORIGIN -p 5432 -U postgres -Fc -b -v -f "$BKPFOLDER/$DBNAME.backup" $DBNAME
# create new database
psql -h $DESTINATION -p 5432 -U postgres -c "CREATE DATABASE \"$DBNAME\" OWNER \"$OWNER\";"
# create db extention
psql -h $DESTINATION -p 5432 -U postgres -d $DBNAME -c "CREATE EXTENSION postgis;"
# put data on database
perl $PGISRESTORE "$BKPFOLDER/$DBNAME.backup" | psql -h $DESTINATION -U postgres $DBNAME 2>> $BKPFOLDER/backup.log
#change owner
./change_db_owner.sh -d $DBNAME -o $OWNER