2016-06-30 16:55:23 +02:00
#!/bin/bash
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
# Database functions
#
# License
# =======
#
2016-10-31 17:24:49 +01:00
# Copyright (C) 2014-2016 Bob Mottram <bob@freedombone.net>
2016-06-30 16:55:23 +02:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2016-07-03 17:13:34 +02:00
# default MariaDB password
MARIADB_PASSWORD=
# Used to indicate whether the backup contains MariaDB databases or not
BACKUP_INCLUDES_DATABASES="no"
2016-11-02 18:49:21 +01:00
function remove_backup_database_local {
database_name=$1
sed -i "/# Backup the ${database_name} database/,/# End of ${database_name} database backup/d" /usr/bin/backupdatabases
sed -i "/# Backup ${database_name}/,/# End of backup for ${database_name}/d" /etc/cron.weekly/backupdatabasesweekly
sed -i "/# Backup ${database_name}/,/# End of backup for ${database_name}/d" /etc/cron.monthly/backupdatabasesmonthly
sed -i "/${database_name}/d" /etc/cron.hourly/repair
}
2016-06-30 16:55:23 +02:00
function backup_database_local {
2016-09-30 22:20:26 +02:00
# Makes local backups of databases which can then be automatically rolled
# back if corruption is detected
database_name=$1
backup_databases_script=/usr/bin/backupdatabases
2016-11-02 18:37:11 +01:00
if ! grep -q "# Backup the ${database_name} database" $backup_databases_script; then
echo "# Backup the ${database_name} database" >> $backup_databases_script
echo "TEMPFILE=/root/${database_name}.sql" >> $backup_databases_script
echo 'DAILYFILE=/var/backups/${database_name}_daily.sql' >> $backup_databases_script
echo "mysqldump --password=\"\$MYSQL_PASSWORD\" ${database_name} > \$TEMPFILE" >> $backup_databases_script
echo 'FILESIZE=$(stat -c%s $TEMPFILE)' >> $backup_databases_script
echo 'if [ "$FILESIZE" -eq "0" ]; then' >> $backup_databases_script
echo ' if [ -f $DAILYFILE ]; then' >> $backup_databases_script
echo ' cp $DAILYFILE $TEMPFILE' >> $backup_databases_script
echo '' >> $backup_databases_script
echo ' # try to restore yesterdays database' >> $backup_databases_script
echo " mysql -u root --password=\"\$MYSQL_PASSWORD\" ${database_name} -o < \$DAILYFILE" >> $backup_databases_script
echo '' >> $backup_databases_script
echo ' # Send a warning email' >> $backup_databases_script
echo " echo \"Unable to create a backup of the ${database_name} database. Attempted to restore from yesterdays backup\" | mail -s \"${database_name} backup\" \$EMAIL" >> $backup_databases_script
echo ' else' >> $backup_databases_script
echo ' # Send a warning email' >> $backup_databases_script
echo " echo \"Unable to create a backup of the ${database_name} database.\" | mail -s \"${database_name} backup\" \$EMAIL" >> $backup_databases_script
echo ' fi' >> $backup_databases_script
echo 'else' >> $backup_databases_script
echo ' chmod 600 $TEMPFILE' >> $backup_databases_script
echo ' mv $TEMPFILE $DAILYFILE' >> $backup_databases_script
echo '' >> $backup_databases_script
echo ' # Make the backup readable only by root' >> $backup_databases_script
echo ' chmod 600 $DAILYFILE' >> $backup_databases_script
echo 'fi' >> $backup_databases_script
echo "# End of ${database_name} database backup" >> $backup_databases_script
fi
2016-09-30 22:20:26 +02:00
weekly_backup_script=/etc/cron.weekly/backupdatabasesweekly
2016-11-02 18:37:11 +01:00
if ! grep -q "Backup ${database_name}" ${weekly_backup_script}; then
echo "# Backup ${database_name}" >> ${weekly_backup_script}
2016-09-30 22:20:26 +02:00
echo "if [ -f /var/backups/${database_name}_weekly.sql ]; then" >> ${weekly_backup_script}
echo " cp -f /var/backups/${database_name}_weekly.sql /var/backups/${database_name}_2weekly.sql" >> ${weekly_backup_script}
echo 'fi' >> ${weekly_backup_script}
echo "if [ -f /var/backups/${database_name}_daily.sql ]; then" >> ${weekly_backup_script}
echo " cp -f /var/backups/${database_name}_daily.sql /var/backups/${database_name}_weekly.sql" >> ${weekly_backup_script}
echo 'fi' >> ${weekly_backup_script}
2016-11-02 18:37:11 +01:00
echo "# End of backup for ${database_name}" >> ${weekly_backup_script}
2016-09-30 22:20:26 +02:00
fi
monthly_backup_script=/etc/cron.monthly/backupdatabasesmonthly
2016-11-02 18:37:11 +01:00
if ! grep -q "Backup ${database_name}" ${monthly_backup_script}; then
echo "# Backup ${database_name}" >> ${monthly_backup_script}
2016-09-30 22:20:26 +02:00
echo "if [ -f /var/backups/${database_name}_monthly.sql ]; then" >> ${monthly_backup_script}
echo " cp -f /var/backups/${database_name}_monthly.sql /var/backups/${database_name}_2monthly.sql" >> ${monthly_backup_script}
echo 'fi' >> ${monthly_backup_script}
echo "if [ -f /var/backups/${database_name}_weekly.sql ]; then" >> ${monthly_backup_script}
echo " cp -f /var/backups/${database_name}_weekly.sql /var/backups/${database_name}_monthly.sql" >> ${monthly_backup_script}
echo 'fi' >> ${monthly_backup_script}
2016-11-02 18:37:11 +01:00
echo "# End of backup for ${database_name}" >> ${monthly_backup_script}
2016-09-30 22:20:26 +02:00
fi
if ! grep -q "${database_name}" /etc/cron.hourly/repair; then
echo "${PROJECT_NAME}-repair-database ${database_name}" >> /etc/cron.hourly/repair
# remove legacy stuff
sed -i 's|/usr/bin/repairdatabase redmatrix||g' /etc/cron.hourly/repair
fi
2016-06-30 16:55:23 +02:00
}
function get_mariadb_password {
2016-11-20 21:39:14 +01:00
# migrate from database password file to using the password store
DATABASE_PASSWORD_FILE=/root/dbpass
2016-11-19 20:17:33 +01:00
if [ -f $DATABASE_PASSWORD_FILE ]; then
MARIADB_PASSWORD=$(cat $DATABASE_PASSWORD_FILE)
2016-11-20 21:39:14 +01:00
${PROJECT_NAME}-pass -u root -a mariadb -p "$MARIADB_PASSWORD"
2016-11-21 11:51:48 +01:00
stored_password=$(${PROJECT_NAME}-pass -u root -a mariadb)
if [[ "$stored_password" == "$MARIADB_PASSWORD" ]]; then
2016-11-20 21:39:14 +01:00
shred -zu $DATABASE_PASSWORD_FILE
echo $'MariaDB password moved into password store'
return
fi
2016-09-30 22:20:26 +02:00
fi
2016-11-20 21:39:14 +01:00
MARIADB_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mariadb)
2016-06-30 16:55:23 +02:00
}
function install_mariadb {
2016-10-16 20:50:56 +02:00
if [[ $(is_completed $FUNCNAME) == "1" ]]; then
2016-09-30 22:20:26 +02:00
return
fi
2017-06-03 17:22:51 +02:00
apt-get -yq install software-properties-common debconf-utils
2016-10-23 20:38:14 +02:00
apt-get -yq update
2016-09-30 22:20:26 +02:00
function_check get_mariadb_password
get_mariadb_password
if [ ! $MARIADB_PASSWORD ]; then
if [ -f $IMAGE_PASSWORD_FILE ]; then
MARIADB_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
else
MARIADB_PASSWORD="$(openssl rand -base64 32 | cut -c1-${MINIMUM_PASSWORD_LENGTH})"
fi
2016-11-20 21:39:14 +01:00
${PROJECT_NAME}-pass -u root -a mariadb -p "$MARIADB_PASSWORD"
2016-09-30 22:20:26 +02:00
fi
debconf-set-selections <<< "mariadb-server mariadb-server/root_password password $MARIADB_PASSWORD"
debconf-set-selections <<< "mariadb-server mariadb-server/root_password_again password $MARIADB_PASSWORD"
2017-06-05 15:52:47 +02:00
apt-get -yq install mariadb-server mariadb-client
2016-10-23 20:38:14 +02:00
apt-get -yq remove --purge apache*
2016-09-30 22:20:26 +02:00
if [ -d /etc/apache2 ]; then
rm -rf /etc/apache2
echo $'Removed Apache installation after MariaDB install'
fi
if [ ! -d /etc/mysql ]; then
echo $"ERROR: mariadb-server does not appear to have installed. $CHECK_MESSAGE"
exit 54
fi
2016-12-01 12:31:26 +01:00
if [ ! -f /usr/bin/mysql ]; then
echo $"ERROR: mariadb-server does not appear to have installed. $CHECK_MESSAGE"
exit 34672
fi
2017-06-05 15:42:24 +02:00
#if [ ! -f /etc/mysql/conf.d/my.cnf ]; then
# echo $'/etc/mysql/conf.d/my.cnf not found'
# exit 6376245
#fi
2017-06-03 17:49:31 +02:00
systemctl restart mysql
2016-09-30 22:20:26 +02:00
mysqladmin -u root password "$MARIADB_PASSWORD"
2016-10-16 20:50:56 +02:00
mark_completed $FUNCNAME
2016-06-30 16:55:23 +02:00
}
function backup_databases_script_header {
2016-09-30 22:20:26 +02:00
if [ ! -f /usr/bin/backupdatabases ]; then
# daily
echo '#!/bin/sh' > /usr/bin/backupdatabases
echo '' >> /usr/bin/backupdatabases
echo "EMAIL='$MY_EMAIL_ADDRESS'" >> /usr/bin/backupdatabases
echo '' >> /usr/bin/backupdatabases
2016-11-20 21:39:14 +01:00
echo "MYSQL_PASSWORD=\$(${PROJECT_NAME}-pass -u root -a mariadb)" >> /usr/bin/backupdatabases
2016-09-30 22:20:26 +02:00
echo 'umask 0077' >> /usr/bin/backupdatabases
echo '' >> /usr/bin/backupdatabases
echo '# exit if we are backing up to friends servers' >> /usr/bin/backupdatabases
echo "if [ -f $FRIENDS_SERVERS_LIST ]; then" >> /usr/bin/backupdatabases
echo ' exit 1' >> /usr/bin/backupdatabases
echo 'fi' >> /usr/bin/backupdatabases
chmod 600 /usr/bin/backupdatabases
chmod +x /usr/bin/backupdatabases
echo '#!/bin/sh' > /etc/cron.daily/backupdatabasesdaily
echo '/usr/bin/backupdatabases' >> /etc/cron.daily/backupdatabasesdaily
chmod 600 /etc/cron.daily/backupdatabasesdaily
chmod +x /etc/cron.daily/backupdatabasesdaily
# weekly
echo '#!/bin/sh' > /etc/cron.weekly/backupdatabasesweekly
echo '' >> /etc/cron.weekly/backupdatabasesweekly
echo 'umask 0077' >> /etc/cron.weekly/backupdatabasesweekly
chmod 600 /etc/cron.weekly/backupdatabasesweekly
chmod +x /etc/cron.weekly/backupdatabasesweekly
# monthly
echo '#!/bin/sh' > /etc/cron.monthly/backupdatabasesmonthly
echo '' >> /etc/cron.monthly/backupdatabasesmonthly
echo 'umask 0077' >> /etc/cron.monthly/backupdatabasesmonthly
chmod 600 /etc/cron.monthly/backupdatabasesmonthly
chmod +x /etc/cron.monthly/backupdatabasesmonthly
fi
2016-06-30 16:55:23 +02:00
}
function repair_databases_script {
2016-09-30 22:20:26 +02:00
if [ -f /etc/cron.hourly/repair ]; then
sed -i "s|/usr/bin/repairdatabase|${PROJECT_NAME}-repair-database|g" /etc/cron.hourly/repair
fi
2016-06-30 16:55:23 +02:00
2016-10-16 20:50:56 +02:00
if [[ $(is_completed $FUNCNAME) == "1" ]]; then
2016-09-30 22:20:26 +02:00
return
fi
2016-06-30 16:55:23 +02:00
2016-11-20 21:39:14 +01:00
db_pass=$(${PROJECT_NAME}-pass -u root -p mariadb)
if [[ "$db_pass" == 'Error:'* ]]; then
2016-09-30 22:20:26 +02:00
return
fi
2016-06-30 16:55:23 +02:00
2016-09-30 22:20:26 +02:00
echo '#!/bin/bash' > /etc/cron.hourly/repair
echo '' >> /etc/cron.hourly/repair
chmod 600 /etc/cron.hourly/repair
chmod +x /etc/cron.hourly/repair
2016-06-30 16:55:23 +02:00
2016-10-16 20:50:56 +02:00
mark_completed $FUNCNAME
2016-06-30 16:55:23 +02:00
}
function remove_database {
2016-09-30 22:20:26 +02:00
app_name="$1"
if [ ! -d $INSTALL_DIR ]; then
mkdir $INSTALL_DIR
fi
echo "drop database ${app_name};
2016-06-30 16:55:23 +02:00
quit" > $INSTALL_DIR/batch.sql
2016-09-30 22:20:26 +02:00
chmod 600 $INSTALL_DIR/batch.sql
mysql -u root --password="$MARIADB_PASSWORD" < $INSTALL_DIR/batch.sql
shred -zu $INSTALL_DIR/batch.sql
2016-06-30 16:55:23 +02:00
}
function create_database {
2016-09-30 22:20:26 +02:00
app_name="$1"
app_admin_password="$2"
app_admin_username=$3
if [ ! -d $INSTALL_DIR ]; then
mkdir $INSTALL_DIR
fi
if [ ! $app_admin_username ]; then
app_admin_username=${app_name}admin
fi
echo "create database ${app_name};
2016-06-30 16:55:23 +02:00
CREATE USER '$app_admin_username@localhost' IDENTIFIED BY '${app_admin_password}';
GRANT ALL PRIVILEGES ON ${app_name}.* TO '$app_admin_username@localhost';
quit" > $INSTALL_DIR/batch.sql
2016-09-30 22:20:26 +02:00
chmod 600 $INSTALL_DIR/batch.sql
mysql -u root --password="$MARIADB_PASSWORD" < $INSTALL_DIR/batch.sql
shred -zu $INSTALL_DIR/batch.sql
2016-07-03 17:13:34 +02:00
}
function initialise_database {
2016-09-30 22:20:26 +02:00
database_name=$1
database_file=$2
mysql -u root --password="$MARIADB_PASSWORD" -D $database_name < $database_file
if [ ! "$?" = "0" ]; then
exit 62952
fi
2016-07-03 17:13:34 +02:00
}
function run_query {
2016-09-30 22:20:26 +02:00
database_name=$1
database_query=$2
mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name
2016-06-30 16:55:23 +02:00
}
2017-04-20 14:43:55 +02:00
function run_query_with_output {
database_name=$1
database_query=$2
output=$(mysql -u root --password="$MARIADB_PASSWORD" << EOF
use $database_name;
$database_query
EOF
)
echo "$output"
}
2016-07-03 22:22:12 +02:00
function drop_database {
2016-09-30 22:20:26 +02:00
database_name=$1
get_mariadb_password
mysqladmin -uroot -p"$MARIADB_PASSWORD" -f drop $database_name
2016-07-03 22:22:12 +02:00
}
2016-07-25 20:46:32 +02:00
function database_reinstall {
2016-10-23 20:38:14 +02:00
apt-get -yq purge mariadb*
2016-09-30 22:20:26 +02:00
rm -rf /var/lib/mysql
rm -rf /etc/mysql
2016-10-23 20:38:14 +02:00
apt-get -yq install mariadb-server
2016-07-25 20:46:32 +02:00
}
2016-12-15 21:20:12 +01:00
function install_rethinkdb {
if [ ! -d $INSTALL_DIR ]; then
mkdir -p $INSTALL_DIR
fi
cd $INSTALL_DIR
echo "deb http://download.rethinkdb.com/apt `lsb_release -cs` main" | tee /etc/apt/sources.list.d/rethinkdb.list
wget -qO- https://download.rethinkdb.com/apt/pubkey.gpg | apt-key add -
apt-get update
apt-get -yq install rethinkdb
2016-12-17 18:00:11 +01:00
2016-12-18 00:48:20 +01:00
echo 'runuser=rethinkdb' > /etc/rethinkdb/instances.d/default.conf
echo 'rungroup=rethinkdb' >> /etc/rethinkdb/instances.d/default.conf
2016-12-17 18:00:11 +01:00
echo '# pid-file=/var/run/rethinkdb/rethinkdb.pid' >> /etc/rethinkdb/instances.d/default.conf
echo '# directory=/var/lib/rethinkdb/default' >> /etc/rethinkdb/instances.d/default.conf
2016-12-18 00:48:20 +01:00
echo '# log-file=/var/log/rethinkdb' >> /etc/rethinkdb/instances.d/default.conf
echo 'bind=127.0.0.1' >> /etc/rethinkdb/instances.d/default.conf
2016-12-17 18:00:11 +01:00
echo '# canonical-address=' >> /etc/rethinkdb/instances.d/default.conf
echo '# driver-port=28015' >> /etc/rethinkdb/instances.d/default.conf
echo '# cluster-port=29015' >> /etc/rethinkdb/instances.d/default.conf
echo '# join=example.com:29015' >> /etc/rethinkdb/instances.d/default.conf
echo '# port-offset=0' >> /etc/rethinkdb/instances.d/default.conf
echo '# reql-http-proxy=socks5://example.com:1080' >> /etc/rethinkdb/instances.d/default.conf
2016-12-18 00:48:20 +01:00
echo '# http-port=8091' >> /etc/rethinkdb/instances.d/default.conf
2016-12-17 18:00:11 +01:00
echo '# no-http-admin' >> /etc/rethinkdb/instances.d/default.conf
echo '# cores=2' >> /etc/rethinkdb/instances.d/default.conf
echo '# cache-size=1024' >> /etc/rethinkdb/instances.d/default.conf
echo '# io-threads=64' >> /etc/rethinkdb/instances.d/default.conf
echo '# direct-io' >> /etc/rethinkdb/instances.d/default.conf
echo '# server-name=server1' >> /etc/rethinkdb/instances.d/default.conf
2016-12-15 21:20:12 +01:00
systemctl restart rethinkdb
}
function remove_rethinkdb {
if [ ! -d /etc/rethinkdb ]; then
return
fi
apt-get -yq remove rethinkdb
if [ -d /etc/rethinkdb ]; then
rm -rf /etc/rethinkdb
fi
if [ -f /etc/apt/sources.list.d/rethinkdb.list ]; then
rm /etc/apt/sources.list.d/rethinkdb.list
apt-get update
fi
}
2016-06-30 16:55:23 +02:00
# NOTE: deliberately there is no "exit 0"