freedombone-utils-database 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 keep_database_running {
  35. if [ ! $(daemon_is_running mariadb) ]; then
  36. systemctl start mariadb
  37. fi
  38. }
  39. function remove_backup_database_local {
  40. database_name=$1
  41. sed -i "/# Backup the ${database_name} database/,/# End of ${database_name} database backup/d" /usr/bin/backupdatabases
  42. sed -i "/# Backup ${database_name}/,/# End of backup for ${database_name}/d" /etc/cron.weekly/backupdatabasesweekly
  43. sed -i "/# Backup ${database_name}/,/# End of backup for ${database_name}/d" /etc/cron.monthly/backupdatabasesmonthly
  44. sed -i "/${database_name}/d" /etc/cron.hourly/repair
  45. }
  46. function backup_database_local {
  47. # Makes local backups of databases which can then be automatically rolled
  48. # back if corruption is detected
  49. database_name=$1
  50. backup_databases_script=/usr/bin/backupdatabases
  51. if ! grep -q "# Check database daemon" /usr/bin/backupdatabases; then
  52. echo '' >> /usr/bin/backupdatabases
  53. echo '# Check database daemon is running' >> /usr/bin/backupdatabases
  54. echo 'if [ ! $(systemctl is-active mariadb >/dev/null 2>&1 && echo Running) ]; then' >> /usr/bin/backupdatabases
  55. echo ' systemctl start mariadb' >> /usr/bin/backupdatabases
  56. echo 'fi' >> /usr/bin/backupdatabases
  57. echo '' >> /usr/bin/backupdatabases
  58. fi
  59. if ! grep -q "# Backup the ${database_name} database" $backup_databases_script; then
  60. echo "# Backup the ${database_name} database" >> $backup_databases_script
  61. echo "TEMPFILE=/root/${database_name}.sql" >> $backup_databases_script
  62. echo 'DAILYFILE=/var/backups/${database_name}_daily.sql' >> $backup_databases_script
  63. echo "mysqldump --password=\"\$MYSQL_PASSWORD\" ${database_name} > \$TEMPFILE" >> $backup_databases_script
  64. echo 'FILESIZE=$(stat -c%s $TEMPFILE)' >> $backup_databases_script
  65. echo 'if [ "$FILESIZE" -eq "0" ]; then' >> $backup_databases_script
  66. echo ' if [ -f $DAILYFILE ]; then' >> $backup_databases_script
  67. echo ' cp $DAILYFILE $TEMPFILE' >> $backup_databases_script
  68. echo '' >> $backup_databases_script
  69. echo ' # try to restore yesterdays database' >> $backup_databases_script
  70. echo " mysql -u root --password=\"\$MYSQL_PASSWORD\" ${database_name} -o < \$DAILYFILE" >> $backup_databases_script
  71. echo '' >> $backup_databases_script
  72. echo ' # Send a warning email' >> $backup_databases_script
  73. 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
  74. echo ' else' >> $backup_databases_script
  75. echo ' # Send a warning email' >> $backup_databases_script
  76. echo " echo \"Unable to create a backup of the ${database_name} database.\" | mail -s \"${database_name} backup\" \$EMAIL" >> $backup_databases_script
  77. echo ' fi' >> $backup_databases_script
  78. echo 'else' >> $backup_databases_script
  79. echo ' chmod 600 $TEMPFILE' >> $backup_databases_script
  80. echo ' mv $TEMPFILE $DAILYFILE' >> $backup_databases_script
  81. echo '' >> $backup_databases_script
  82. echo ' # Make the backup readable only by root' >> $backup_databases_script
  83. echo ' chmod 600 $DAILYFILE' >> $backup_databases_script
  84. echo 'fi' >> $backup_databases_script
  85. echo "# End of ${database_name} database backup" >> $backup_databases_script
  86. fi
  87. weekly_backup_script=/etc/cron.weekly/backupdatabasesweekly
  88. if ! grep -q "Backup ${database_name}" ${weekly_backup_script}; then
  89. echo "# Backup ${database_name}" >> ${weekly_backup_script}
  90. echo "if [ -f /var/backups/${database_name}_weekly.sql ]; then" >> ${weekly_backup_script}
  91. echo " cp -f /var/backups/${database_name}_weekly.sql /var/backups/${database_name}_2weekly.sql" >> ${weekly_backup_script}
  92. echo 'fi' >> ${weekly_backup_script}
  93. echo "if [ -f /var/backups/${database_name}_daily.sql ]; then" >> ${weekly_backup_script}
  94. echo " cp -f /var/backups/${database_name}_daily.sql /var/backups/${database_name}_weekly.sql" >> ${weekly_backup_script}
  95. echo 'fi' >> ${weekly_backup_script}
  96. echo "# End of backup for ${database_name}" >> ${weekly_backup_script}
  97. fi
  98. monthly_backup_script=/etc/cron.monthly/backupdatabasesmonthly
  99. if ! grep -q "Backup ${database_name}" ${monthly_backup_script}; then
  100. echo "# Backup ${database_name}" >> ${monthly_backup_script}
  101. echo "if [ -f /var/backups/${database_name}_monthly.sql ]; then" >> ${monthly_backup_script}
  102. echo " cp -f /var/backups/${database_name}_monthly.sql /var/backups/${database_name}_2monthly.sql" >> ${monthly_backup_script}
  103. echo 'fi' >> ${monthly_backup_script}
  104. echo "if [ -f /var/backups/${database_name}_weekly.sql ]; then" >> ${monthly_backup_script}
  105. echo " cp -f /var/backups/${database_name}_weekly.sql /var/backups/${database_name}_monthly.sql" >> ${monthly_backup_script}
  106. echo 'fi' >> ${monthly_backup_script}
  107. echo "# End of backup for ${database_name}" >> ${monthly_backup_script}
  108. fi
  109. if ! grep -q "${database_name}" /etc/cron.hourly/repair; then
  110. echo "${PROJECT_NAME}-repair-database ${database_name}" >> /etc/cron.hourly/repair
  111. # remove legacy stuff
  112. sed -i 's|/usr/bin/repairdatabase redmatrix||g' /etc/cron.hourly/repair
  113. fi
  114. }
  115. function get_mariadb_password {
  116. # migrate from database password file to using the password store
  117. DATABASE_PASSWORD_FILE=/root/dbpass
  118. if [ -f $DATABASE_PASSWORD_FILE ]; then
  119. MARIADB_PASSWORD=$(cat $DATABASE_PASSWORD_FILE)
  120. ${PROJECT_NAME}-pass -u root -a mariadb -p "$MARIADB_PASSWORD"
  121. stored_password=$(${PROJECT_NAME}-pass -u root -a mariadb)
  122. if [[ "$stored_password" == "$MARIADB_PASSWORD" ]]; then
  123. shred -zu $DATABASE_PASSWORD_FILE
  124. echo $'MariaDB password moved into password store'
  125. return
  126. fi
  127. fi
  128. MARIADB_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mariadb)
  129. }
  130. function install_mariadb {
  131. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  132. return
  133. fi
  134. apt-get -yq install software-properties-common debconf-utils
  135. apt-get -yq update
  136. function_check get_mariadb_password
  137. get_mariadb_password
  138. if [ ! $MARIADB_PASSWORD ]; then
  139. if [ -f $IMAGE_PASSWORD_FILE ]; then
  140. MARIADB_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  141. else
  142. MARIADB_PASSWORD="$(openssl rand -base64 32 | cut -c1-${MINIMUM_PASSWORD_LENGTH})"
  143. fi
  144. fi
  145. ${PROJECT_NAME}-pass -u root -a mariadb -p "$MARIADB_PASSWORD"
  146. debconf-set-selections <<< "mariadb-server mariadb-server/root_password password $MARIADB_PASSWORD"
  147. debconf-set-selections <<< "mariadb-server mariadb-server/root_password_again password $MARIADB_PASSWORD"
  148. apt-get -yq install mariadb-server mariadb-client
  149. apt-get -yq remove --purge apache2-bin*
  150. if [ -d /etc/apache2 ]; then
  151. rm -rf /etc/apache2
  152. echo $'Removed Apache installation after MariaDB install'
  153. fi
  154. if [ ! -d /etc/mysql ]; then
  155. echo $"ERROR: mariadb-server does not appear to have installed. $CHECK_MESSAGE"
  156. exit 54
  157. fi
  158. if [ ! -f /usr/bin/mysql ]; then
  159. echo $"ERROR: mariadb-server does not appear to have installed. $CHECK_MESSAGE"
  160. exit 34672
  161. fi
  162. sed -i 's|ExecStart=/usr/sbin/mysqld|ExecStart=/usr/sbin/mysqld --skip-grant-tables|g' /lib/systemd/system/mariadb.service
  163. systemctl daemon-reload
  164. systemctl restart mariadb
  165. # See http://www.pontikis.net/blog/debian-9-stretch-rc3-web-server-setup-php7-mariadb
  166. # https://mariadb.com/kb/en/mariadb/unix_socket-authentication-plugin
  167. function_check run_query
  168. run_query_root mysql "update mysql.user set plugin = '' where User='root'; flush privileges;"
  169. run_query_root mysql "UPDATE user SET Password=PASSWORD('$MARIADB_PASSWORD') where USER='root'; flush privileges;"
  170. sed -i 's| --skip-grant-tables||g' /lib/systemd/system/mariadb.service
  171. systemctl daemon-reload
  172. systemctl restart mariadb
  173. # mariadb daemon seems to occasionally crash
  174. # This is a crude attempt to keep it running
  175. add_watchdog_daemon mariadb
  176. run_query mysql "CREATE USER 'root@localhost' IDENTIFIED BY '${MARIADB_PASSWORD}'; flush privileges;"
  177. run_query mysql "update mysql.user set plugin = '' where User='root@localhost'; flush privileges;"
  178. run_query mysql "GRANT ALL PRIVILEGES ON * TO 'root@localhost'; flush privileges;"
  179. mark_completed $FUNCNAME
  180. }
  181. function backup_databases_script_header {
  182. if [ ! -f /usr/bin/backupdatabases ]; then
  183. # daily
  184. echo '#!/bin/sh' > /usr/bin/backupdatabases
  185. echo '' >> /usr/bin/backupdatabases
  186. echo "EMAIL='$MY_EMAIL_ADDRESS'" >> /usr/bin/backupdatabases
  187. echo '' >> /usr/bin/backupdatabases
  188. echo "MYSQL_PASSWORD=\$(${PROJECT_NAME}-pass -u root -a mariadb)" >> /usr/bin/backupdatabases
  189. echo 'umask 0077' >> /usr/bin/backupdatabases
  190. echo '' >> /usr/bin/backupdatabases
  191. echo '# exit if we are backing up to friends servers' >> /usr/bin/backupdatabases
  192. echo "if [ -f $FRIENDS_SERVERS_LIST ]; then" >> /usr/bin/backupdatabases
  193. echo ' exit 1' >> /usr/bin/backupdatabases
  194. echo 'fi' >> /usr/bin/backupdatabases
  195. chmod 600 /usr/bin/backupdatabases
  196. chmod +x /usr/bin/backupdatabases
  197. echo '#!/bin/sh' > /etc/cron.daily/backupdatabasesdaily
  198. echo '/usr/bin/backupdatabases' >> /etc/cron.daily/backupdatabasesdaily
  199. chmod 600 /etc/cron.daily/backupdatabasesdaily
  200. chmod +x /etc/cron.daily/backupdatabasesdaily
  201. # weekly
  202. echo '#!/bin/sh' > /etc/cron.weekly/backupdatabasesweekly
  203. echo '' >> /etc/cron.weekly/backupdatabasesweekly
  204. echo 'umask 0077' >> /etc/cron.weekly/backupdatabasesweekly
  205. chmod 600 /etc/cron.weekly/backupdatabasesweekly
  206. chmod +x /etc/cron.weekly/backupdatabasesweekly
  207. # monthly
  208. echo '#!/bin/sh' > /etc/cron.monthly/backupdatabasesmonthly
  209. echo '' >> /etc/cron.monthly/backupdatabasesmonthly
  210. echo 'umask 0077' >> /etc/cron.monthly/backupdatabasesmonthly
  211. chmod 600 /etc/cron.monthly/backupdatabasesmonthly
  212. chmod +x /etc/cron.monthly/backupdatabasesmonthly
  213. fi
  214. }
  215. function repair_databases_script {
  216. if [ -f /etc/cron.hourly/repair ]; then
  217. sed -i "s|/usr/bin/repairdatabase|${PROJECT_NAME}-repair-database|g" /etc/cron.hourly/repair
  218. fi
  219. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  220. return
  221. fi
  222. db_pass=$(${PROJECT_NAME}-pass -u root -p mariadb)
  223. if [[ "$db_pass" == 'Error:'* ]]; then
  224. return
  225. fi
  226. echo '#!/bin/bash' > /etc/cron.hourly/repair
  227. echo '' >> /etc/cron.hourly/repair
  228. chmod 600 /etc/cron.hourly/repair
  229. chmod +x /etc/cron.hourly/repair
  230. mark_completed $FUNCNAME
  231. }
  232. function remove_database {
  233. app_name="$1"
  234. if [ ! -d $INSTALL_DIR ]; then
  235. mkdir $INSTALL_DIR
  236. fi
  237. echo "drop database ${app_name};
  238. quit" > $INSTALL_DIR/batch.sql
  239. chmod 600 $INSTALL_DIR/batch.sql
  240. keep_database_running
  241. mysql -u root --password="$MARIADB_PASSWORD" < $INSTALL_DIR/batch.sql
  242. shred -zu $INSTALL_DIR/batch.sql
  243. }
  244. function initialise_database {
  245. database_name=$1
  246. database_file=$2
  247. keep_database_running
  248. mysql -u root --password="$MARIADB_PASSWORD" -D $database_name < $database_file
  249. if [ ! "$?" = "0" ]; then
  250. exit 62952
  251. fi
  252. }
  253. function run_query {
  254. database_name=$1
  255. database_query=$2
  256. keep_database_running
  257. mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name
  258. }
  259. function run_query_root {
  260. database_name=$1
  261. database_query=$2
  262. keep_database_running
  263. mysql -e "$database_query" $database_name
  264. }
  265. function create_database {
  266. app_name="$1"
  267. app_admin_password="$2"
  268. app_admin_username=$3
  269. if [ ! -d $INSTALL_DIR ]; then
  270. mkdir $INSTALL_DIR
  271. fi
  272. if [ ! $app_admin_username ]; then
  273. app_admin_username=${app_name}admin
  274. fi
  275. echo "create database ${app_name};
  276. CREATE USER '$app_admin_username@localhost' IDENTIFIED BY '${app_admin_password}';
  277. update mysql.user set plugin = '' where User='$app_admin_username@localhost';
  278. GRANT ALL PRIVILEGES ON ${app_name}.* TO '$app_admin_username@localhost';
  279. flush privileges;
  280. quit" > $INSTALL_DIR/batch.sql
  281. chmod 600 $INSTALL_DIR/batch.sql
  282. keep_database_running
  283. mysql -u root --password="$MARIADB_PASSWORD" < $INSTALL_DIR/batch.sql
  284. shred -zu $INSTALL_DIR/batch.sql
  285. }
  286. function run_query_with_output {
  287. database_name=$1
  288. database_query=$2
  289. keep_database_running
  290. output=$(mysql -u root --password="$MARIADB_PASSWORD" << EOF
  291. use $database_name;
  292. $database_query
  293. EOF
  294. )
  295. echo "$output"
  296. }
  297. function drop_database {
  298. database_name="$1"
  299. get_mariadb_password
  300. echo "drop database ${app_name};
  301. flush privileges;
  302. quit" > $INSTALL_DIR/batch.sql
  303. chmod 600 $INSTALL_DIR/batch.sql
  304. keep_database_running
  305. mysql -u root --password="$MARIADB_PASSWORD" < $INSTALL_DIR/batch.sql
  306. shred -zu $INSTALL_DIR/batch.sql
  307. }
  308. function database_reinstall {
  309. apt-get -yq purge mariadb*
  310. rm -rf /var/lib/mysql
  311. rm -rf /etc/mysql
  312. sed -i '/mariadb/d' ~/${PROJECT_NAME}-completed.txt
  313. install_mariadb
  314. }
  315. function install_rethinkdb {
  316. if [[ "$(arch)" == "arm"* ]]; then
  317. echo $'rethinkdb does not currently support ARM debian packages'
  318. echo $"See http://download.rethinkdb.com/apt/dists/${DEBIAN_VERSION}/main"
  319. exit 723723452
  320. fi
  321. if [ ! -d $INSTALL_DIR ]; then
  322. mkdir -p $INSTALL_DIR
  323. fi
  324. cd $INSTALL_DIR
  325. echo "deb http://download.rethinkdb.com/apt $DEBIAN_VERSION main" | tee /etc/apt/sources.list.d/rethinkdb.list
  326. wget -qO- https://download.rethinkdb.com/apt/pubkey.gpg | apt-key add -
  327. apt-get update
  328. apt-get -yq install rethinkdb
  329. echo 'runuser=rethinkdb' > /etc/rethinkdb/instances.d/default.conf
  330. echo 'rungroup=rethinkdb' >> /etc/rethinkdb/instances.d/default.conf
  331. echo '# pid-file=/var/run/rethinkdb/rethinkdb.pid' >> /etc/rethinkdb/instances.d/default.conf
  332. echo '# directory=/var/lib/rethinkdb/default' >> /etc/rethinkdb/instances.d/default.conf
  333. echo '# log-file=/var/log/rethinkdb' >> /etc/rethinkdb/instances.d/default.conf
  334. echo 'bind=127.0.0.1' >> /etc/rethinkdb/instances.d/default.conf
  335. echo '# canonical-address=' >> /etc/rethinkdb/instances.d/default.conf
  336. echo '# driver-port=28015' >> /etc/rethinkdb/instances.d/default.conf
  337. echo '# cluster-port=29015' >> /etc/rethinkdb/instances.d/default.conf
  338. echo '# join=example.com:29015' >> /etc/rethinkdb/instances.d/default.conf
  339. echo '# port-offset=0' >> /etc/rethinkdb/instances.d/default.conf
  340. echo '# reql-http-proxy=socks5://example.com:1080' >> /etc/rethinkdb/instances.d/default.conf
  341. echo '# http-port=8091' >> /etc/rethinkdb/instances.d/default.conf
  342. echo '# no-http-admin' >> /etc/rethinkdb/instances.d/default.conf
  343. echo '# cores=2' >> /etc/rethinkdb/instances.d/default.conf
  344. echo '# cache-size=1024' >> /etc/rethinkdb/instances.d/default.conf
  345. echo '# io-threads=64' >> /etc/rethinkdb/instances.d/default.conf
  346. echo '# direct-io' >> /etc/rethinkdb/instances.d/default.conf
  347. echo '# server-name=server1' >> /etc/rethinkdb/instances.d/default.conf
  348. systemctl restart rethinkdb
  349. }
  350. function remove_rethinkdb {
  351. if [ ! -d /etc/rethinkdb ]; then
  352. return
  353. fi
  354. apt-get -yq remove rethinkdb
  355. if [ -d /etc/rethinkdb ]; then
  356. rm -rf /etc/rethinkdb
  357. fi
  358. if [ -f /etc/apt/sources.list.d/rethinkdb.list ]; then
  359. rm /etc/apt/sources.list.d/rethinkdb.list
  360. apt-get update
  361. fi
  362. }
  363. # NOTE: deliberately there is no "exit 0"