anastasis-dbconfig (3786B)
1 #!/bin/bash 2 # This file is part of GNU ANASTASIS. 3 # Copyright (C) 2023 Anastasis Systems SA 4 # 5 # ANASTASIS is free software; you can redistribute it and/or modify it under the 6 # terms of the GNU Lesser General Public License as published by the Free Software 7 # Foundation; either version 2.1, or (at your option) any later version. 8 # 9 # ANASTASIS is distributed in the hope that it will be useful, but WITHOUT ANY 10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 12 # 13 # You should have received a copy of the GNU Lesser General Public License along with 14 # ANASTASIS; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 # 16 # @author Christian Grothoff 17 # 18 # 19 # Error checking on 20 set -eu 21 22 RESET_DB=0 23 SKIP_DBINIT=0 24 DBUSER="anastasis-httpd" 25 CFGFILE="/etc/anastasis/anastasis.conf" 26 27 # Parse command-line options 28 while getopts 'c:hrsu:' OPTION; do 29 case "$OPTION" in 30 c) 31 CFGFILE="$OPTARG" 32 ;; 33 h) 34 echo 'Supported options:' 35 echo " -c FILENAME -- write configuration to FILENAME (default: $CFGFILE)" 36 echo " -h -- print this help text" 37 echo " -r -- reset database (dangerous)" 38 echo " -s -- skip database initialization" 39 echo " -u USER -- anastasis-httpd to be run by USER (default: $DBUSER)" 40 exit 0 41 ;; 42 r) 43 RESET_DB="1" 44 ;; 45 s) 46 SKIP_DBINIT="1" 47 ;; 48 u) 49 DBUSER="$OPTARG" 50 ;; 51 ?) 52 echo "Unrecognized command line option '$OPTION'" 1>&2 53 exit 1 54 ;; 55 esac 56 done 57 58 if ! id postgres > /dev/null 59 then 60 echo "Could not find 'postgres' user. Please install Postgresql first" 61 exit 1 62 fi 63 64 if [ "$(id -u)" -ne 0 ] 65 then 66 echo "This script must be run as root" 67 exit 1 68 fi 69 70 if [ 0 = "$SKIP_DBINIT" ] 71 then 72 if ! anastasis-dbinit -v 2> /dev/null 73 then 74 echo "Required 'anastasis-dbinit' not found. Please fix your installation." 75 exit 1 76 fi 77 DBINIT=$(which anastasis-dbinit) 78 fi 79 80 if ! id "$DBUSER" > /dev/null 81 then 82 echo "Could not find '$DBUSER' user. Please set it up first" 83 exit 1 84 fi 85 86 echo "Setting up database user $DBUSER." 1>&2 87 88 if ! sudo -i -u postgres createuser "$DBUSER" 2> /dev/null 89 then 90 echo "Database user '$DBUSER' already existed. Continuing anyway." 1>&2 91 fi 92 93 DBPATH=$(anastasis-config \ 94 -c "$CFGFILE" \ 95 -s stasis-postgres \ 96 -o CONFIG) 97 98 if ! echo "$DBPATH" | grep "postgres://" > /dev/null 99 then 100 echo "Invalid database configuration value '$DBPATH'." 1>&2 101 exit 1 102 fi 103 104 DBNAME=$(echo "$DBPATH" \ 105 | sed \ 106 -e "s/postgres:\/\/.*\///" \ 107 -e "s/?.*//") 108 109 110 if sudo -i -u postgres psql "$DBNAME" < /dev/null 2> /dev/null 111 then 112 if [ 1 = "$RESET_DB" ] 113 then 114 echo "Deleting existing database $DBNAME." 1>&2 115 if ! sudo -i -u postgres dropdb "$DBNAME" 116 then 117 echo "Failed to delete existing database '$DBNAME'" 118 exit 1 119 fi 120 DO_CREATE=1 121 else 122 echo "Database '$DBNAME' already exists, continuing anyway." 123 DO_CREATE=0 124 fi 125 else 126 DO_CREATE=1 127 fi 128 129 if [ 1 = "$DO_CREATE" ] 130 then 131 echo "Creating database $DBNAME." 1>&2 132 133 if ! sudo -i -u postgres createdb -O "$DBUSER" "$DBNAME" 134 then 135 echo "Failed to create database '$DBNAME'" 136 exit 1 137 fi 138 fi 139 140 if [ 0 = "$SKIP_DBINIT" ] 141 then 142 echo "Initializing database $DBNAME." 1>&2 143 if ! sudo -u "$DBUSER" "$DBINIT" -c "$CFGFILE" 144 then 145 echo "Failed to initialize database schema" 146 exit 1 147 fi 148 fi 149 150 echo "Database configuration finished." 1>&2 151 152 exit 0