freedombone-utils-database 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Database functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 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. # default MariaDB password
  31. MARIADB_PASSWORD=
  32. # Used to indicate whether the backup contains MariaDB databases or not
  33. BACKUP_INCLUDES_DATABASES="no"
  34. function remove_backup_database_local {
  35. database_name=$1
  36. sed -i "/# Backup the ${database_name} database/,/# End of ${database_name} database backup/d" /usr/bin/backupdatabases
  37. sed -i "/# Backup ${database_name}/,/# End of backup for ${database_name}/d" /etc/cron.weekly/backupdatabasesweekly
  38. sed -i "/# Backup ${database_name}/,/# End of backup for ${database_name}/d" /etc/cron.monthly/backupdatabasesmonthly
  39. sed -i "/${database_name}/d" /etc/cron.hourly/repair
  40. }
  41. function backup_database_local {
  42. # Makes local backups of databases which can then be automatically rolled
  43. # back if corruption is detected
  44. database_name=$1
  45. backup_databases_script=/usr/bin/backupdatabases
  46. if ! grep -q "# Backup the ${database_name} database" $backup_databases_script; then
  47. echo "# Backup the ${database_name} database" >> $backup_databases_script
  48. echo "TEMPFILE=/root/${database_name}.sql" >> $backup_databases_script
  49. echo 'DAILYFILE=/var/backups/${database_name}_daily.sql' >> $backup_databases_script
  50. echo "mysqldump --password=\"\$MYSQL_PASSWORD\" ${database_name} > \$TEMPFILE" >> $backup_databases_script
  51. echo 'FILESIZE=$(stat -c%s $TEMPFILE)' >> $backup_databases_script
  52. echo 'if [ "$FILESIZE" -eq "0" ]; then' >> $backup_databases_script
  53. echo ' if [ -f $DAILYFILE ]; then' >> $backup_databases_script
  54. echo ' cp $DAILYFILE $TEMPFILE' >> $backup_databases_script
  55. echo '' >> $backup_databases_script
  56. echo ' # try to restore yesterdays database' >> $backup_databases_script
  57. echo " mysql -u root --password=\"\$MYSQL_PASSWORD\" ${database_name} -o < \$DAILYFILE" >> $backup_databases_script
  58. echo '' >> $backup_databases_script
  59. echo ' # Send a warning email' >> $backup_databases_script
  60. 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
  61. echo ' else' >> $backup_databases_script
  62. echo ' # Send a warning email' >> $backup_databases_script
  63. echo " echo \"Unable to create a backup of the ${database_name} database.\" | mail -s \"${database_name} backup\" \$EMAIL" >> $backup_databases_script
  64. echo ' fi' >> $backup_databases_script
  65. echo 'else' >> $backup_databases_script
  66. echo ' chmod 600 $TEMPFILE' >> $backup_databases_script
  67. echo ' mv $TEMPFILE $DAILYFILE' >> $backup_databases_script
  68. echo '' >> $backup_databases_script
  69. echo ' # Make the backup readable only by root' >> $backup_databases_script
  70. echo ' chmod 600 $DAILYFILE' >> $backup_databases_script
  71. echo 'fi' >> $backup_databases_script
  72. echo "# End of ${database_name} database backup" >> $backup_databases_script
  73. fi
  74. weekly_backup_script=/etc/cron.weekly/backupdatabasesweekly
  75. if ! grep -q "Backup ${database_name}" ${weekly_backup_script}; then
  76. echo "# Backup ${database_name}" >> ${weekly_backup_script}
  77. echo "if [ -f /var/backups/${database_name}_weekly.sql ]; then" >> ${weekly_backup_script}
  78. echo " cp -f /var/backups/${database_name}_weekly.sql /var/backups/${database_name}_2weekly.sql" >> ${weekly_backup_script}
  79. echo 'fi' >> ${weekly_backup_script}
  80. echo "if [ -f /var/backups/${database_name}_daily.sql ]; then" >> ${weekly_backup_script}
  81. echo " cp -f /var/backups/${database_name}_daily.sql /var/backups/${database_name}_weekly.sql" >> ${weekly_backup_script}
  82. echo 'fi' >> ${weekly_backup_script}
  83. echo "# End of backup for ${database_name}" >> ${weekly_backup_script}
  84. fi
  85. monthly_backup_script=/etc/cron.monthly/backupdatabasesmonthly
  86. if ! grep -q "Backup ${database_name}" ${monthly_backup_script}; then
  87. echo "# Backup ${database_name}" >> ${monthly_backup_script}
  88. echo "if [ -f /var/backups/${database_name}_monthly.sql ]; then" >> ${monthly_backup_script}
  89. echo " cp -f /var/backups/${database_name}_monthly.sql /var/backups/${database_name}_2monthly.sql" >> ${monthly_backup_script}
  90. echo 'fi' >> ${monthly_backup_script}
  91. echo "if [ -f /var/backups/${database_name}_weekly.sql ]; then" >> ${monthly_backup_script}
  92. echo " cp -f /var/backups/${database_name}_weekly.sql /var/backups/${database_name}_monthly.sql" >> ${monthly_backup_script}
  93. echo 'fi' >> ${monthly_backup_script}
  94. echo "# End of backup for ${database_name}" >> ${monthly_backup_script}
  95. fi
  96. if ! grep -q "${database_name}" /etc/cron.hourly/repair; then
  97. echo "${PROJECT_NAME}-repair-database ${database_name}" >> /etc/cron.hourly/repair
  98. # remove legacy stuff
  99. sed -i 's|/usr/bin/repairdatabase redmatrix||g' /etc/cron.hourly/repair
  100. fi
  101. }
  102. function get_mariadb_password {
  103. # migrate from database password file to using the password store
  104. DATABASE_PASSWORD_FILE=/root/dbpass
  105. if [ -f $DATABASE_PASSWORD_FILE ]; then
  106. MARIADB_PASSWORD=$(cat $DATABASE_PASSWORD_FILE)
  107. ${PROJECT_NAME}-pass -u root -a mariadb -p "$MARIADB_PASSWORD"
  108. stored_password=$(${PROJECT_NAME}-pass -u root -a mariadb)
  109. if [[ "$stored_password" == "$MARIADB_PASSWORD" ]]; then
  110. shred -zu $DATABASE_PASSWORD_FILE
  111. echo $'MariaDB password moved into password store'
  112. return
  113. fi
  114. fi
  115. MARIADB_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mariadb)
  116. }
  117. function install_mariadb {
  118. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  119. return
  120. fi
  121. apt-get -yq install python-software-properties debconf-utils
  122. apt-get -yq install software-properties-common
  123. apt-get -yq update
  124. function_check get_mariadb_password
  125. get_mariadb_password
  126. if [ ! $MARIADB_PASSWORD ]; then
  127. if [ -f $IMAGE_PASSWORD_FILE ]; then
  128. MARIADB_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  129. else
  130. MARIADB_PASSWORD="$(openssl rand -base64 32 | cut -c1-${MINIMUM_PASSWORD_LENGTH})"
  131. fi
  132. ${PROJECT_NAME}-pass -u root -a mariadb -p "$MARIADB_PASSWORD"
  133. fi
  134. debconf-set-selections <<< "mariadb-server mariadb-server/root_password password $MARIADB_PASSWORD"
  135. debconf-set-selections <<< "mariadb-server mariadb-server/root_password_again password $MARIADB_PASSWORD"
  136. apt-get -yq install mariadb-server
  137. apt-get -yq remove --purge apache*
  138. if [ -d /etc/apache2 ]; then
  139. rm -rf /etc/apache2
  140. echo $'Removed Apache installation after MariaDB install'
  141. fi
  142. if [ ! -d /etc/mysql ]; then
  143. echo $"ERROR: mariadb-server does not appear to have installed. $CHECK_MESSAGE"
  144. exit 54
  145. fi
  146. if [ ! -f /usr/bin/mysql ]; then
  147. echo $"ERROR: mariadb-server does not appear to have installed. $CHECK_MESSAGE"
  148. exit 34672
  149. fi
  150. mysqladmin -u root password "$MARIADB_PASSWORD"
  151. mark_completed $FUNCNAME
  152. }
  153. function backup_databases_script_header {
  154. if [ ! -f /usr/bin/backupdatabases ]; then
  155. # daily
  156. echo '#!/bin/sh' > /usr/bin/backupdatabases
  157. echo '' >> /usr/bin/backupdatabases
  158. echo "EMAIL='$MY_EMAIL_ADDRESS'" >> /usr/bin/backupdatabases
  159. echo '' >> /usr/bin/backupdatabases
  160. echo "MYSQL_PASSWORD=\$(${PROJECT_NAME}-pass -u root -a mariadb)" >> /usr/bin/backupdatabases
  161. echo 'umask 0077' >> /usr/bin/backupdatabases
  162. echo '' >> /usr/bin/backupdatabases
  163. echo '# exit if we are backing up to friends servers' >> /usr/bin/backupdatabases
  164. echo "if [ -f $FRIENDS_SERVERS_LIST ]; then" >> /usr/bin/backupdatabases
  165. echo ' exit 1' >> /usr/bin/backupdatabases
  166. echo 'fi' >> /usr/bin/backupdatabases
  167. chmod 600 /usr/bin/backupdatabases
  168. chmod +x /usr/bin/backupdatabases
  169. echo '#!/bin/sh' > /etc/cron.daily/backupdatabasesdaily
  170. echo '/usr/bin/backupdatabases' >> /etc/cron.daily/backupdatabasesdaily
  171. chmod 600 /etc/cron.daily/backupdatabasesdaily
  172. chmod +x /etc/cron.daily/backupdatabasesdaily
  173. # weekly
  174. echo '#!/bin/sh' > /etc/cron.weekly/backupdatabasesweekly
  175. echo '' >> /etc/cron.weekly/backupdatabasesweekly
  176. echo 'umask 0077' >> /etc/cron.weekly/backupdatabasesweekly
  177. chmod 600 /etc/cron.weekly/backupdatabasesweekly
  178. chmod +x /etc/cron.weekly/backupdatabasesweekly
  179. # monthly
  180. echo '#!/bin/sh' > /etc/cron.monthly/backupdatabasesmonthly
  181. echo '' >> /etc/cron.monthly/backupdatabasesmonthly
  182. echo 'umask 0077' >> /etc/cron.monthly/backupdatabasesmonthly
  183. chmod 600 /etc/cron.monthly/backupdatabasesmonthly
  184. chmod +x /etc/cron.monthly/backupdatabasesmonthly
  185. fi
  186. }
  187. function repair_databases_script {
  188. if [ -f /etc/cron.hourly/repair ]; then
  189. sed -i "s|/usr/bin/repairdatabase|${PROJECT_NAME}-repair-database|g" /etc/cron.hourly/repair
  190. fi
  191. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  192. return
  193. fi
  194. db_pass=$(${PROJECT_NAME}-pass -u root -p mariadb)
  195. if [[ "$db_pass" == 'Error:'* ]]; then
  196. return
  197. fi
  198. echo '#!/bin/bash' > /etc/cron.hourly/repair
  199. echo '' >> /etc/cron.hourly/repair
  200. chmod 600 /etc/cron.hourly/repair
  201. chmod +x /etc/cron.hourly/repair
  202. mark_completed $FUNCNAME
  203. }
  204. function remove_database {
  205. app_name="$1"
  206. if [ ! -d $INSTALL_DIR ]; then
  207. mkdir $INSTALL_DIR
  208. fi
  209. echo "drop database ${app_name};
  210. quit" > $INSTALL_DIR/batch.sql
  211. chmod 600 $INSTALL_DIR/batch.sql
  212. mysql -u root --password="$MARIADB_PASSWORD" < $INSTALL_DIR/batch.sql
  213. shred -zu $INSTALL_DIR/batch.sql
  214. }
  215. function create_database {
  216. app_name="$1"
  217. app_admin_password="$2"
  218. app_admin_username=$3
  219. if [ ! -d $INSTALL_DIR ]; then
  220. mkdir $INSTALL_DIR
  221. fi
  222. if [ ! $app_admin_username ]; then
  223. app_admin_username=${app_name}admin
  224. fi
  225. echo "create database ${app_name};
  226. CREATE USER '$app_admin_username@localhost' IDENTIFIED BY '${app_admin_password}';
  227. GRANT ALL PRIVILEGES ON ${app_name}.* TO '$app_admin_username@localhost';
  228. quit" > $INSTALL_DIR/batch.sql
  229. chmod 600 $INSTALL_DIR/batch.sql
  230. mysql -u root --password="$MARIADB_PASSWORD" < $INSTALL_DIR/batch.sql
  231. shred -zu $INSTALL_DIR/batch.sql
  232. }
  233. function initialise_database {
  234. database_name=$1
  235. database_file=$2
  236. mysql -u root --password="$MARIADB_PASSWORD" -D $database_name < $database_file
  237. if [ ! "$?" = "0" ]; then
  238. exit 62952
  239. fi
  240. }
  241. function run_query {
  242. database_name=$1
  243. database_query=$2
  244. mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name
  245. }
  246. function run_query_with_output {
  247. database_name=$1
  248. database_query=$2
  249. output=$(mysql -u root --password="$MARIADB_PASSWORD" << EOF
  250. use $database_name;
  251. $database_query
  252. EOF
  253. )
  254. echo "$output"
  255. }
  256. function drop_database {
  257. database_name=$1
  258. get_mariadb_password
  259. mysqladmin -uroot -p"$MARIADB_PASSWORD" -f drop $database_name
  260. }
  261. function database_reinstall {
  262. apt-get -yq purge mariadb*
  263. rm -rf /var/lib/mysql
  264. rm -rf /etc/mysql
  265. apt-get -yq install mariadb-server
  266. }
  267. function install_rethinkdb {
  268. if [ ! -d $INSTALL_DIR ]; then
  269. mkdir -p $INSTALL_DIR
  270. fi
  271. cd $INSTALL_DIR
  272. echo "deb http://download.rethinkdb.com/apt `lsb_release -cs` main" | tee /etc/apt/sources.list.d/rethinkdb.list
  273. wget -qO- https://download.rethinkdb.com/apt/pubkey.gpg | apt-key add -
  274. apt-get update
  275. apt-get -yq install rethinkdb
  276. echo 'runuser=rethinkdb' > /etc/rethinkdb/instances.d/default.conf
  277. echo 'rungroup=rethinkdb' >> /etc/rethinkdb/instances.d/default.conf
  278. echo '# pid-file=/var/run/rethinkdb/rethinkdb.pid' >> /etc/rethinkdb/instances.d/default.conf
  279. echo '# directory=/var/lib/rethinkdb/default' >> /etc/rethinkdb/instances.d/default.conf
  280. echo '# log-file=/var/log/rethinkdb' >> /etc/rethinkdb/instances.d/default.conf
  281. echo 'bind=127.0.0.1' >> /etc/rethinkdb/instances.d/default.conf
  282. echo '# canonical-address=' >> /etc/rethinkdb/instances.d/default.conf
  283. echo '# driver-port=28015' >> /etc/rethinkdb/instances.d/default.conf
  284. echo '# cluster-port=29015' >> /etc/rethinkdb/instances.d/default.conf
  285. echo '# join=example.com:29015' >> /etc/rethinkdb/instances.d/default.conf
  286. echo '# port-offset=0' >> /etc/rethinkdb/instances.d/default.conf
  287. echo '# reql-http-proxy=socks5://example.com:1080' >> /etc/rethinkdb/instances.d/default.conf
  288. echo '# http-port=8091' >> /etc/rethinkdb/instances.d/default.conf
  289. echo '# no-http-admin' >> /etc/rethinkdb/instances.d/default.conf
  290. echo '# cores=2' >> /etc/rethinkdb/instances.d/default.conf
  291. echo '# cache-size=1024' >> /etc/rethinkdb/instances.d/default.conf
  292. echo '# io-threads=64' >> /etc/rethinkdb/instances.d/default.conf
  293. echo '# direct-io' >> /etc/rethinkdb/instances.d/default.conf
  294. echo '# server-name=server1' >> /etc/rethinkdb/instances.d/default.conf
  295. systemctl restart rethinkdb
  296. }
  297. function remove_rethinkdb {
  298. if [ ! -d /etc/rethinkdb ]; then
  299. return
  300. fi
  301. apt-get -yq remove rethinkdb
  302. if [ -d /etc/rethinkdb ]; then
  303. rm -rf /etc/rethinkdb
  304. fi
  305. if [ -f /etc/apt/sources.list.d/rethinkdb.list ]; then
  306. rm /etc/apt/sources.list.d/rethinkdb.list
  307. apt-get update
  308. fi
  309. }
  310. # NOTE: deliberately there is no "exit 0"