|
| 1 | +#!/usr/bin/env ash |
| 2 | + |
| 3 | +github_bareos='raw.githubusercontent.com/bareos/bareos' |
| 4 | +webui_admin_conf='master/webui/install/bareos/bareos-dir.d/profile/webui-admin.conf' |
| 5 | +admin_conf='master/webui/install/bareos/bareos-dir.d/console/admin.conf.example' |
| 6 | + |
| 7 | +if [ ! -f /etc/bareos/bareos-config.control ]; then |
| 8 | + tar xzf /bareos-dir.tgz --backup=simple --suffix=.before-control |
| 9 | + |
| 10 | + # Download default admin profile config |
| 11 | + if [ ! -f /etc/bareos/bareos-dir.d/profile/webui-admin.conf ]; then |
| 12 | + curl --silent --insecure "https://${github_bareos}/${webui_admin_conf}" \ |
| 13 | + --output /etc/bareos/bareos-dir.d/profile/webui-admin.conf |
| 14 | + fi |
| 15 | + |
| 16 | + # Download default webUI admin config |
| 17 | + if [ ! -f /etc/bareos/bareos-dir.d/console/admin.conf ]; then |
| 18 | + curl --silent --insecure "https://${github_bareos}/${admin_conf}" \ |
| 19 | + --output /etc/bareos/bareos-dir.d/console/admin.conf |
| 20 | + fi |
| 21 | + |
| 22 | + # Update bareos-director configs |
| 23 | + # Director / mycatalog & mail report |
| 24 | + sed -i "s#dbuser =.*#dbuser = $DB_USER#" \ |
| 25 | + /etc/bareos/bareos-dir.d/catalog/MyCatalog.conf |
| 26 | + sed -i 's#dbpassword =.*#dbpassword = '\""${DB_PASSWORD}"\"'#' \ |
| 27 | + /etc/bareos/bareos-dir.d/catalog/MyCatalog.conf |
| 28 | + sed -i 's#dbname =.*#dbname = '\""${DB_NAME}"\"'\n dbaddress = '\""${DB_HOST}"\"'\n dbport = '\""${DB_PORT}"\"'#' \ |
| 29 | + /etc/bareos/bareos-dir.d/catalog/MyCatalog.conf |
| 30 | + sed -i 's#dbdriver = .*#dbdriver = '\""postgresql"\"'#' \ |
| 31 | + /etc/bareos/bareos-dir.d/catalog/MyCatalog.conf |
| 32 | + [ -n "${SENDER_MAIL}" ] && sed -i "s#<%r#<${SENDER_MAIL}#g" \ |
| 33 | + /etc/bareos/bareos-dir.d/messages/Daemon.conf |
| 34 | + sed -i "s#/usr/bin/bsmtp -h localhost#/usr/bin/bsmtp -h ${SMTP_HOST}#" \ |
| 35 | + /etc/bareos/bareos-dir.d/messages/Daemon.conf |
| 36 | + sed -i "s#mail = root#mail = ${ADMIN_MAIL}#" \ |
| 37 | + /etc/bareos/bareos-dir.d/messages/Daemon.conf |
| 38 | + [ -n "${SENDER_MAIL}" ] && sed -i "s#<%r#<${SENDER_MAIL}#g" \ |
| 39 | + /etc/bareos/bareos-dir.d/messages/Standard.conf |
| 40 | + sed -i "s#/usr/bin/bsmtp -h localhost#/usr/bin/bsmtp -h ${SMTP_HOST}#g" \ |
| 41 | + /etc/bareos/bareos-dir.d/messages/Standard.conf |
| 42 | + sed -i "s#mail = root#mail = ${ADMIN_MAIL}#" \ |
| 43 | + /etc/bareos/bareos-dir.d/messages/Standard.conf |
| 44 | + |
| 45 | + # Setup webhook |
| 46 | + if [ "${WEBHOOK_NOTIFICATION}" = true ]; then |
| 47 | + sed -i "s#/usr/bin/bsmtp -h.*#/usr/local/bin/webhook-notify %t %e %c %l %n\"#" \ |
| 48 | + /etc/bareos/bareos-dir.d/messages/Daemon.conf |
| 49 | + sed -i "s#/usr/bin/bsmtp -h.*#/usr/local/bin/webhook-notify %t %e %c %l %n\"#" \ |
| 50 | + /etc/bareos/bareos-dir.d/messages/Standard.conf |
| 51 | + fi |
| 52 | + |
| 53 | + # storage daemon |
| 54 | + sed -i 's#Address = .*#Address = '\""${BAREOS_SD_HOST}"\"'#' \ |
| 55 | + /etc/bareos/bareos-dir.d/storage/File.conf |
| 56 | + sed -i 's#Password = .*#Password = '\""${BAREOS_SD_PASSWORD}"\"'#' \ |
| 57 | + /etc/bareos/bareos-dir.d/storage/File.conf |
| 58 | + |
| 59 | + # client/file daemon |
| 60 | + sed -i 's#Address = .*#Address = '\""${BAREOS_FD_HOST}"\"'#' \ |
| 61 | + /etc/bareos/bareos-dir.d/client/bareos-fd.conf |
| 62 | + sed -i 's#Password = .*#Password = '\""${BAREOS_FD_PASSWORD}"\"'#' \ |
| 63 | + /etc/bareos/bareos-dir.d/client/bareos-fd.conf |
| 64 | + |
| 65 | + # webUI |
| 66 | + sed -i 's#Password = .*#Password = '\""${BAREOS_WEBUI_PASSWORD}"\"'#' \ |
| 67 | + /etc/bareos/bareos-dir.d/console/admin.conf |
| 68 | + |
| 69 | + |
| 70 | + # MyCatalog Backup |
| 71 | + sed -i "s#/var/lib/bareos/bareos.sql#/var/lib/bareos-director/bareos.sql#" \ |
| 72 | + /etc/bareos/bareos-dir.d/fileset/Catalog.conf |
| 73 | + sed -i "s#make_catalog_backup MyCatalog#make_catalog_backup ${DB_NAME} ${DB_USER} '' ${DB_HOST}#" \ |
| 74 | + /etc/bareos/bareos-dir.d/job/BackupCatalog.conf |
| 75 | + |
| 76 | + # Add pgpass file to ${DB_USER} home |
| 77 | + homedir=$(getent passwd "$DB_USER" | cut -d: -f6) |
| 78 | + echo "${DB_HOST}:${DB_PORT}:${DB_NAME}:${DB_USER}:${DB_PASSWORD}" > "${homedir}/.pgpass" |
| 79 | + chmod 600 "${homedir}/.pgpass" |
| 80 | + chown "${DB_USER}" "${homedir}/.pgpass" |
| 81 | + |
| 82 | + # Control file |
| 83 | + touch /etc/bareos/bareos-config.control |
| 84 | +fi |
| 85 | + |
| 86 | +if [[ -z ${CI_TEST} ]] ; then |
| 87 | + # Waiting Postgresql is up |
| 88 | + sqlup=1 |
| 89 | + while [ "$sqlup" -ne 0 ] ; do |
| 90 | + echo "Waiting for postgresql..." |
| 91 | + pg_isready --host="${DB_HOST}" --port="${DB_PORT}" --user="${DB_ADMIN_USER}" |
| 92 | + if [ $? -ne 0 ] ; then |
| 93 | + sqlup=1 |
| 94 | + sleep 5 |
| 95 | + else |
| 96 | + sqlup=0 |
| 97 | + echo "...postgresql is alive" |
| 98 | + fi |
| 99 | + done |
| 100 | +fi |
| 101 | + |
| 102 | +export PGUSER=${DB_ADMIN_USER} |
| 103 | +export PGHOST=${DB_HOST} |
| 104 | +export PGPASSWORD=${DB_ADMIN_PASSWORD} |
| 105 | +[[ -z "${DB_INIT}" ]] && DB_INIT='false' |
| 106 | +[[ -z "${DB_UPDATE}" ]] && DB_UPDATE='false' |
| 107 | + |
| 108 | +if [ ! -f /etc/bareos/bareos-db.control ] && [ "${DB_INIT}" == 'true' ] ; then |
| 109 | + # Init Postgres DB |
| 110 | + echo "Bareos DB init" |
| 111 | + echo "Bareos DB init: Create user ${DB_USER}" |
| 112 | + psql -c "create user ${DB_USER} with createdb createrole login;" |
| 113 | + echo "Bareos DB init: Set user password" |
| 114 | + psql -c "alter user ${DB_USER} password '${DB_PASSWORD}';" |
| 115 | + /etc/bareos/scripts/create_bareos_database 2>/dev/null |
| 116 | + /etc/bareos/scripts/make_bareos_tables 2>/dev/null |
| 117 | + /etc/bareos/scripts/grant_bareos_privileges 2>/dev/null |
| 118 | + # Control file |
| 119 | + touch /etc/bareos/bareos-db.control |
| 120 | +fi |
| 121 | + |
| 122 | +if [ "${DB_UPDATE}" == 'true' ] ; then |
| 123 | + # Try Postgres upgrade |
| 124 | + echo "Bareoos DB update" |
| 125 | + echo "Bareoos DB update: Update tables" |
| 126 | + /etc/bareos/scripts/update_bareos_tables 2>/dev/null |
| 127 | + echo "Bareoos DB update: Grant privileges" |
| 128 | + /etc/bareos/scripts/grant_bareos_privileges 2>/dev/null |
| 129 | +fi |
| 130 | + |
| 131 | +# Fix permissions |
| 132 | +find /etc/bareos ! -user bareos -exec chown bareos {} \; |
| 133 | +chown -R bareos:bareos /var/lib/bareos /var/log/bareos |
| 134 | + |
| 135 | +# Run Dockerfile CMD |
| 136 | +exec "$@" |
| 137 | + |
0 commit comments