taler-directory-dbconfig (3593B)
1 #!/bin/bash 2 # This file is part of GNU TALER. 3 # Copyright (C) 2026 Taler Systems SA 4 # 5 # TALER 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 # TALER 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 # TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 # 16 # @author Christian Grothoff 17 # @author Martin Schanzenbach 18 # 19 # 20 # Error checking on 21 set -eu 22 23 RESET_DB=0 24 SKIP_DBINIT=0 25 DBUSER="taler-directory" 26 CFGFILE="/etc/taler-directory/taler-directory.conf" 27 28 # Parse command-line options 29 while getopts 'c:hrsu:' OPTION; do 30 case "$OPTION" in 31 c) 32 CFGFILE="$OPTARG" 33 ;; 34 h) 35 echo 'Supported options:' 36 echo " -c FILENAME -- use configuration FILENAME (default: $CFGFILE)" 37 echo " -h -- print this help text" 38 echo " -r -- reset database (dangerous)" 39 echo " -s -- skip database initialization" 40 echo " -u USER -- taler-directory to be run by USER (default: $DBUSER)" 41 exit 0 42 ;; 43 r) 44 RESET_DB="1" 45 ;; 46 s) 47 SKIP_DBINIT="1" 48 ;; 49 u) 50 DBUSER="$OPTARG" 51 ;; 52 ?) 53 echo "Unrecognized command line option '$OPTION'" 1 &>2 54 exit 1 55 ;; 56 esac 57 done 58 59 if ! id postgres >/dev/null; then 60 echo "Could not find 'postgres' user. Please install Postgresql first" 61 exit 1 62 fi 63 64 if [ "$(id -u)" -ne 0 ]; then 65 echo "This script must be run as root" 66 exit 1 67 fi 68 69 if [ 0 = "$SKIP_DBINIT" ]; then 70 if ! taler-directory-dbinit -h 1>/dev/null 2>/dev/null; then 71 echo "Required 'taler-directory-dbinit' not found. Please fix your installation." 72 exit 1 73 fi 74 DBINIT=$(which taler-directory-dbinit) 75 fi 76 77 if ! id "$DBUSER" >/dev/null; then 78 echo "Could not find '$DBUSER' user. Please set it up first" 79 exit 1 80 fi 81 82 echo "Setting up database user $DBUSER." 1>&2 83 84 if ! sudo -i -u postgres createuser "$DBUSER" 2>/dev/null; then 85 echo "Database user '$DBUSER' already existed. Continuing anyway." 1>&2 86 fi 87 88 DBPATH=$(taler-directory-config \ 89 -c "$CFGFILE" \ 90 -s taldir-pq \ 91 -o connection_string) 92 93 if ! echo "$DBPATH" | grep "postgres://" >/dev/null; then 94 echo "Invalid database configuration value '$DBPATH'." 1>&2 95 exit 1 96 fi 97 98 DBNAME_KEYVAL=$(echo "$DBPATH" | 99 sed \ 100 -e "s/?.*//" \ 101 -e "s/postgres:\/\/.*\///") 102 103 DBNAME=$(echo $DBNAME_KEYVAL | cut -d'=' -f2 | xargs) 104 105 if sudo -i -u postgres psql "$DBNAME" </dev/null 2>/dev/null; then 106 if [ 1 = "$RESET_DB" ]; then 107 echo "Deleting existing database $DBNAME." 1>&2 108 if ! sudo -i -u postgres dropdb "$DBNAME"; then 109 echo "Failed to delete existing database '$DBNAME'" 110 exit 1 111 fi 112 DO_CREATE=1 113 else 114 echo "Database '$DBNAME' already exists, continuing anyway." 115 DO_CREATE=0 116 fi 117 else 118 DO_CREATE=1 119 fi 120 121 if [ 1 = "$DO_CREATE" ]; then 122 echo "Creating database $DBNAME." 1>&2 123 if ! sudo -i -u postgres createdb -O "$DBUSER" "$DBNAME"; then 124 echo "Failed to create database '$DBNAME'" 125 exit 1 126 fi 127 fi 128 129 if [ 0 = "$SKIP_DBINIT" ]; then 130 echo "Initializing database $DBNAME." 1>&2 131 if ! sudo -u "$DBUSER" "$DBINIT" -c "$CFGFILE"; then 132 echo "Failed to initialize database schema" 133 exit 1 134 fi 135 fi 136 137 echo "Database configuration finished." 1>&2 138 139 exit 0