Explorar el Código

Postgresql functions

Bob Mottram hace 7 años
padre
commit
6fff2720bc
Se han modificado 2 ficheros con 162 adiciones y 6 borrados
  1. 23
    6
      src/freedombone-utils-backup
  2. 139
    0
      src/freedombone-utils-postgresql

+ 23
- 6
src/freedombone-utils-backup Ver fichero

@@ -235,9 +235,14 @@ function backup_database_local_usb {
235 235
     if [ ! -d ${local_database_dir} ]; then
236 236
         mkdir -p ${local_database_dir}
237 237
     fi
238
-    keep_database_running
239 238
     echo $"Obtaining ${1} database backup"
240
-    mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.sql
239
+    if [ ! $USE_POSTGRESQL ]; then
240
+        keep_database_running
241
+        mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.sql
242
+    else
243
+        USE_POSTGRESQL=
244
+        pg_dump ${1} > ${local_database_dir}/${1}.sql
245
+    fi
241 246
     if [ -f ${local_database_dir}/${1}.sql ]; then
242 247
         if [ ! -s ${local_database_dir}/${1}.sql ]; then
243 248
             echo $"${1} database could not be saved"
@@ -545,9 +550,16 @@ function backup_database_remote {
545 550
     if [ ! -d ${local_database_dir} ]; then
546 551
         mkdir -p ${local_database_dir}
547 552
     fi
548
-    keep_database_running
553
+
549 554
     echo "Obtaining ${1} database backup"
550
-    mysqldump --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.sql
555
+    if [ ! $USE_POSTGRESQL ]; then
556
+        keep_database_running
557
+        mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.sql
558
+    else
559
+        USE_POSTGRESQL=
560
+        pg_dump ${1} > ${local_database_dir}/${1}.sql
561
+    fi
562
+
551 563
     if [ -f ${local_database_dir}/${1}.sql ]; then
552 564
         if [ ! -s ${local_database_dir}/${1}.sql ]; then
553 565
             echo $"${1} database could not be saved"
@@ -642,8 +654,13 @@ function restore_database_from_friend {
642 654
             rm -rf ${local_database_dir}
643 655
             exit 503
644 656
         fi
645
-        keep_database_running
646
-        mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${1} -o < ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
657
+        if [ ! $USE_POSTGRESQL ]; then
658
+            keep_database_running
659
+            mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${1} -o < ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
660
+        else
661
+            USE_POSTGRESQL=
662
+            mysqlsuccess=$(sudo -u postgres psql $database_name < ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
663
+        fi
647 664
         if [ ! "$?" = "0" ]; then
648 665
             echo "$mysqlsuccess"
649 666
             exit 964

+ 139
- 0
src/freedombone-utils-postgresql Ver fichero

@@ -0,0 +1,139 @@
1
+#!/bin/bash
2
+#
3
+# .---.                  .              .
4
+# |                      |              |
5
+# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
6
+# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
7
+# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
8
+#
9
+#                    Freedom in the Cloud
10
+#
11
+# postgresql database functions
12
+#
13
+# License
14
+# =======
15
+#
16
+# Copyright (C) 2017 Bob Mottram <bob@freedombone.net>
17
+#
18
+# This program is free software: you can redistribute it and/or modify
19
+# it under the terms of the GNU Affero General Public License as published by
20
+# the Free Software Foundation, either version 3 of the License, or
21
+# (at your option) any later version.
22
+#
23
+# This program is distributed in the hope that it will be useful,
24
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
25
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
+# GNU Affero General Public License for more details.
27
+#
28
+# You should have received a copy of the GNU Affero General Public License
29
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
30
+
31
+# Set this when calling backup and restore commands
32
+USE_POSTGRESQL=
33
+
34
+function get_postgresql_password {
35
+    POSTGRESQL_PASSWORD=$(${PROJECT_NAME}-pass -u root -a postgresql)
36
+    if [[ "$POSTGRESQL_PASSWORD" == *'failed'* ]]; then
37
+        echo $'Could not obtain postgresql password'
38
+        exit 7835272
39
+    fi
40
+}
41
+
42
+function install_postgresql {
43
+    if [[ $(is_completed $FUNCNAME) == "1" ]]; then
44
+        return
45
+    fi
46
+
47
+    function_check get_postgresql_password
48
+    get_postgresql_password
49
+    if [ ! $POSTGRESQL_PASSWORD ]; then
50
+        if [ -f $IMAGE_PASSWORD_FILE ]; then
51
+            POSTGRESQL_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
52
+        else
53
+            POSTGRESQL_PASSWORD="$(openssl rand -base64 32 | cut -c1-${MINIMUM_PASSWORD_LENGTH})"
54
+        fi
55
+    fi
56
+    ${PROJECT_NAME}-pass -u root -a postgresql -p "$POSTGRESQL_PASSWORD"
57
+
58
+    apt-get -yq install postgresql postgresql-contrib postgresql-client
59
+    apt-get -yq remove --purge apache2-bin*
60
+    if [ -d /etc/apache2 ]; then
61
+        rm -rf /etc/apache2
62
+        echo $'Removed Apache installation after postgresql install'
63
+    fi
64
+
65
+    if [ ! -d /etc/postgresql ]; then
66
+        echo $"ERROR: postgresql does not appear to have installed. $CHECK_MESSAGE"
67
+        exit 78352
68
+    fi
69
+
70
+    if [ ! -f /usr/bin/psql ]; then
71
+        echo $"ERROR: psql command does not appear to have installed. $CHECK_MESSAGE"
72
+        exit 835290
73
+    fi
74
+
75
+    mark_completed $FUNCNAME
76
+}
77
+
78
+function add_postgresql_user {
79
+    postgresql_username=$1
80
+    postgresql_password=$2
81
+    sudo -u postgres psql -c "create user $postgresql_username password 'postgresql_password'"
82
+}
83
+
84
+function remove_postgresql_user {
85
+    postgresql_username=$1
86
+    sudo -u postgres psql -c "drop user $postgresql_username"
87
+}
88
+
89
+function remove_database_postgresql {
90
+    database_name="$1"
91
+    sudo -u postgres psql -c "drop database $database_name"
92
+}
93
+
94
+function run_query_postgresql {
95
+    database_name=$1
96
+    database_query=$2
97
+    sudo -u postgres psql -d $database_name -c "$database_query"
98
+}
99
+
100
+function run_query_postgresql_with_output {
101
+    database_name=$1
102
+    database_query=$2
103
+    output=$(sudo -u postgres psql -d $database_name -c << EOF
104
+use $database_name;
105
+$database_query
106
+EOF
107
+)
108
+    echo "$output"
109
+}
110
+
111
+function initialise_database_postgresql {
112
+    database_name=$1
113
+    database_file=$2
114
+    sudo -u postgres psql $database_name < $database_file
115
+    if [ ! "$?" = "0" ]; then
116
+        exit 7238525
117
+    fi
118
+}
119
+
120
+function create_database_postgresql {
121
+    app_name="$1"
122
+    app_admin_password="$2"
123
+    app_admin_username=$3
124
+    if [ ! -d $INSTALL_DIR ]; then
125
+        mkdir $INSTALL_DIR
126
+    fi
127
+    if [ ! $app_admin_username ]; then
128
+        app_admin_username=${app_name}admin
129
+    fi
130
+
131
+    echo "create database ${app_name};
132
+CREATE USER '$app_admin_username@localhost' IDENTIFIED BY '${app_admin_password}';
133
+GRANT ALL PRIVILEGES ON ${app_name}.* TO '$app_admin_username@localhost';
134
+flush privileges;
135
+quit" > $INSTALL_DIR/batch.sql
136
+    chmod 600 $INSTALL_DIR/batch.sql
137
+    sudo -u postgres psql -d $database_name --file=$INSTALL_DIR/batch.sql
138
+    shred -zu $INSTALL_DIR/batch.sql
139
+}