freedombone-utils-database 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Database functions
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. # default MariaDB password
  29. MARIADB_PASSWORD=
  30. # Used to indicate whether the backup contains MariaDB databases or not
  31. BACKUP_INCLUDES_DATABASES="no"
  32. function store_original_mariadb_password {
  33. if [ ! -f /root/.mariadboriginal ]; then
  34. echo $'Storing original mariadb password'
  35. ORIGINAL_MARIADB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
  36. # We can store this in plaintext because it will soon be of historical interest only
  37. echo -n "$ORIGINAL_MARIADB_PASSWORD" > /root/.mariadboriginal
  38. fi
  39. }
  40. function keep_database_running {
  41. if [ ! "$(daemon_is_running mariadb)" ]; then
  42. systemctl start mariadb
  43. fi
  44. }
  45. function remove_backup_database_local {
  46. database_name=$1
  47. sed -i "/# Backup the ${database_name} database/,/# End of ${database_name} database backup/d" /usr/bin/backupdatabases
  48. sed -i "/# Backup ${database_name}/,/# End of backup for ${database_name}/d" /etc/cron.weekly/backupdatabasesweekly
  49. sed -i "/# Backup ${database_name}/,/# End of backup for ${database_name}/d" /etc/cron.monthly/backupdatabasesmonthly
  50. sed -i "/${database_name}/d" /etc/cron.hourly/repair
  51. }
  52. function backup_database_local {
  53. # Makes local backups of databases which can then be automatically rolled
  54. # back if corruption is detected
  55. database_name=$1
  56. backup_databases_script=/usr/bin/backupdatabases
  57. if ! grep -q "# Check database daemon" /usr/bin/backupdatabases; then
  58. { echo '';
  59. echo '# Check database daemon is running';
  60. echo "if [ ! \$(systemctl is-active mariadb >/dev/null 2>&1 && echo Running) ]; then";
  61. echo ' systemctl start mariadb';
  62. echo 'fi';
  63. echo ''; } >> /usr/bin/backupdatabases
  64. fi
  65. if ! grep -q "# Backup the ${database_name} database" $backup_databases_script; then
  66. { echo "# Backup the ${database_name} database";
  67. echo "TEMPFILE=/root/${database_name}.sql";
  68. echo "DAILYFILE=/var/backups/${database_name}_daily.sql";
  69. echo "mysqldump --password=\"\$MYSQL_PASSWORD\" ${database_name} > \$TEMPFILE";
  70. echo "FILESIZE=\$(stat -c%s \$TEMPFILE)";
  71. echo "if [ \"\$FILESIZE\" -eq \"0\" ]; then";
  72. echo " if [ -f \$DAILYFILE ]; then";
  73. echo " cp \$DAILYFILE \$TEMPFILE";
  74. echo '';
  75. echo ' # try to restore yesterdays database';
  76. echo " mysql -u root --password=\"\$MYSQL_PASSWORD\" ${database_name} -o < \$DAILYFILE";
  77. echo '';
  78. echo ' # Send a warning email';
  79. echo " echo \"Unable to create a backup of the ${database_name} database. Attempted to restore from yesterdays backup\" | mail -s \"${database_name} backup\" \$EMAIL";
  80. echo ' else';
  81. echo ' # Send a warning email';
  82. echo " echo \"Unable to create a backup of the ${database_name} database.\" | mail -s \"${database_name} backup\" \$EMAIL";
  83. echo ' fi';
  84. echo 'else';
  85. echo " chmod 600 \$TEMPFILE";
  86. echo " mv \$TEMPFILE \$DAILYFILE";
  87. echo '';
  88. echo ' # Make the backup readable only by root';
  89. echo " chmod 600 \$DAILYFILE";
  90. echo 'fi';
  91. echo "# End of ${database_name} database backup"; } >> $backup_databases_script
  92. fi
  93. weekly_backup_script=/etc/cron.weekly/backupdatabasesweekly
  94. if ! grep -q "Backup ${database_name}" ${weekly_backup_script}; then
  95. { echo "# Backup ${database_name}";
  96. echo "if [ -f /var/backups/${database_name}_weekly.sql ]; then";
  97. echo " cp -f /var/backups/${database_name}_weekly.sql /var/backups/${database_name}_2weekly.sql";
  98. echo 'fi';
  99. echo "if [ -f /var/backups/${database_name}_daily.sql ]; then";
  100. echo " cp -f /var/backups/${database_name}_daily.sql /var/backups/${database_name}_weekly.sql";
  101. echo 'fi';
  102. echo "# End of backup for ${database_name}"; } >> ${weekly_backup_script}
  103. fi
  104. monthly_backup_script=/etc/cron.monthly/backupdatabasesmonthly
  105. if ! grep -q "Backup ${database_name}" ${monthly_backup_script}; then
  106. { echo "# Backup ${database_name}";
  107. echo "if [ -f /var/backups/${database_name}_monthly.sql ]; then";
  108. echo " cp -f /var/backups/${database_name}_monthly.sql /var/backups/${database_name}_2monthly.sql";
  109. echo 'fi';
  110. echo "if [ -f /var/backups/${database_name}_weekly.sql ]; then";
  111. echo " cp -f /var/backups/${database_name}_weekly.sql /var/backups/${database_name}_monthly.sql";
  112. echo 'fi';
  113. echo "# End of backup for ${database_name}"; } >> ${monthly_backup_script}
  114. fi
  115. if ! grep -q "${database_name}" /etc/cron.hourly/repair; then
  116. echo "${PROJECT_NAME}-repair-database ${database_name}" >> /etc/cron.hourly/repair
  117. # remove legacy stuff
  118. sed -i 's|/usr/bin/repairdatabase redmatrix||g' /etc/cron.hourly/repair
  119. fi
  120. }
  121. function get_mariadb_password {
  122. # migrate from database password file to using the password store
  123. DATABASE_PASSWORD_FILE=/root/dbpass
  124. if [ -f $DATABASE_PASSWORD_FILE ]; then
  125. MARIADB_PASSWORD=$(cat $DATABASE_PASSWORD_FILE)
  126. "${PROJECT_NAME}-pass" -u root -a mariadb -p "$MARIADB_PASSWORD"
  127. stored_password=$("${PROJECT_NAME}-pass" -u root -a mariadb)
  128. if [[ "$stored_password" == "$MARIADB_PASSWORD" ]]; then
  129. rm $DATABASE_PASSWORD_FILE
  130. echo $'MariaDB password moved into password store'
  131. return
  132. fi
  133. fi
  134. MARIADB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
  135. if [[ "$MARIADB_PASSWORD" == *'failed'* ]]; then
  136. echo $'Could not obtain mariadb password'
  137. exit 835225
  138. fi
  139. }
  140. function mariadb_kill_stone_dead {
  141. systemctl stop mariadb
  142. kill_pid=$(pgrep mysqld_safe | head -n 1)
  143. kill -9 "$kill_pid"
  144. kill_pid=$(pgrep mysqld | head -n 1)
  145. kill -9 "$kill_pid"
  146. kill_pid=$(pgrep mysqld | head -n 1)
  147. kill -9 "$kill_pid"
  148. }
  149. function mariadb_fix_authentication {
  150. # See http://www.pontikis.net/blog/debian-9-stretch-rc3-web-server-setup-php7-mariadb
  151. # https://mariadb.com/kb/en/mariadb/unix_socket-authentication-plugin
  152. remove_watchdog_daemon mariadb
  153. mariadb_kill_stone_dead
  154. mysqld_safe --skip-grant-tables &
  155. sleep 5
  156. mysql -u root --password="$MARIADB_PASSWORD" << EOF
  157. use mysql;
  158. update mysql.user set plugin = '' where User='root';
  159. UPDATE user SET Password=PASSWORD('$MARIADB_PASSWORD') where USER='root';
  160. flush privileges;
  161. EOF
  162. mariadb_kill_stone_dead
  163. sed -i 's| --skip-grant-tables||g' /lib/systemd/system/mariadb.service
  164. systemctl daemon-reload
  165. systemctl start mariadb
  166. add_watchdog_daemon mariadb
  167. }
  168. function mariadb_create_root_user {
  169. run_query mysql "CREATE USER 'root@localhost' IDENTIFIED BY '${MARIADB_PASSWORD}'; flush privileges;"
  170. run_query mysql "update mysql.user set plugin = '' where User='root@localhost'; flush privileges;"
  171. run_query mysql "GRANT ALL PRIVILEGES ON * TO 'root@localhost'; flush privileges;"
  172. }
  173. function install_mariadb {
  174. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  175. return
  176. fi
  177. apt-get -yq install software-properties-common debconf-utils
  178. apt-get -yq update
  179. remove_watchdog_daemon mariadb
  180. function_check get_mariadb_password
  181. get_mariadb_password
  182. if [ ! "$MARIADB_PASSWORD" ]; then
  183. if [ -f "$IMAGE_PASSWORD_FILE" ]; then
  184. passfile="$(cat "$IMAGE_PASSWORD_FILE")"
  185. MARIADB_PASSWORD="$(printf "%s" "$passfile")"
  186. else
  187. MARIADB_PASSWORD=$(create_password "${MINIMUM_PASSWORD_LENGTH}")
  188. fi
  189. fi
  190. "${PROJECT_NAME}-pass" -u root -a mariadb -p "$MARIADB_PASSWORD"
  191. debconf-set-selections <<< "mariadb-server mariadb-server/root_password password $MARIADB_PASSWORD"
  192. debconf-set-selections <<< "mariadb-server mariadb-server/root_password_again password $MARIADB_PASSWORD"
  193. apt-get -yq install mariadb-server mariadb-client
  194. apt-get -yq remove --purge apache2-bin*
  195. if [ -d /etc/apache2 ]; then
  196. rm -rf /etc/apache2
  197. echo $'Removed Apache installation after MariaDB install'
  198. fi
  199. if [ ! -d /etc/mysql ]; then
  200. echo $"ERROR: mariadb-server does not appear to have installed. $CHECK_MESSAGE"
  201. exit 54
  202. fi
  203. if [ ! -f /usr/bin/mysql ]; then
  204. echo $"ERROR: mariadb-server does not appear to have installed. $CHECK_MESSAGE"
  205. exit 34672
  206. fi
  207. sed -i 's|ExecStart=/usr/sbin/mysqld|ExecStart=/usr/sbin/mysqld --skip-grant-tables|g' /lib/systemd/system/mariadb.service
  208. systemctl daemon-reload
  209. systemctl restart mariadb
  210. mariadb_fix_authentication
  211. mariadb_create_root_user
  212. mark_completed "${FUNCNAME[0]}"
  213. }
  214. function backup_databases_script_header {
  215. if [ ! -f /usr/bin/backupdatabases ]; then
  216. # daily
  217. { echo '#!/bin/sh';
  218. echo '';
  219. echo "EMAIL='$MY_EMAIL_ADDRESS'";
  220. echo '';
  221. echo "MYSQL_PASSWORD=\$(${PROJECT_NAME}-pass -u root -a mariadb)";
  222. echo 'umask 0077';
  223. echo '';
  224. echo '# exit if we are backing up to friends servers';
  225. echo "if [ -f $FRIENDS_SERVERS_LIST ]; then";
  226. echo ' exit 1';
  227. echo 'fi'; } > /usr/bin/backupdatabases
  228. chmod 600 /usr/bin/backupdatabases
  229. chmod +x /usr/bin/backupdatabases
  230. { echo '#!/bin/sh';
  231. echo '/usr/bin/backupdatabases'; } > /etc/cron.daily/backupdatabasesdaily
  232. chmod 600 /etc/cron.daily/backupdatabasesdaily
  233. chmod +x /etc/cron.daily/backupdatabasesdaily
  234. # weekly
  235. { echo '#!/bin/sh';
  236. echo '';
  237. echo 'umask 0077'; } > /etc/cron.weekly/backupdatabasesweekly
  238. chmod 600 /etc/cron.weekly/backupdatabasesweekly
  239. chmod +x /etc/cron.weekly/backupdatabasesweekly
  240. # monthly
  241. { echo '#!/bin/sh';
  242. echo '';
  243. echo 'umask 0077'; } > /etc/cron.monthly/backupdatabasesmonthly
  244. chmod 600 /etc/cron.monthly/backupdatabasesmonthly
  245. chmod +x /etc/cron.monthly/backupdatabasesmonthly
  246. fi
  247. }
  248. function repair_databases_script {
  249. if [ -f /etc/cron.hourly/repair ]; then
  250. sed -i "s|/usr/bin/repairdatabase|${PROJECT_NAME}-repair-database|g" /etc/cron.hourly/repair
  251. fi
  252. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  253. return
  254. fi
  255. db_pass=$("${PROJECT_NAME}-pass" -u root -p mariadb)
  256. if [[ "$db_pass" == 'Error:'* ]]; then
  257. return
  258. fi
  259. echo '#!/bin/bash' > /etc/cron.hourly/repair
  260. echo '' >> /etc/cron.hourly/repair
  261. chmod 600 /etc/cron.hourly/repair
  262. chmod +x /etc/cron.hourly/repair
  263. mark_completed "${FUNCNAME[0]}"
  264. }
  265. function remove_database {
  266. app_name="$1"
  267. if [ ! -d "$INSTALL_DIR" ]; then
  268. mkdir "$INSTALL_DIR"
  269. fi
  270. echo "drop database ${app_name};
  271. quit" > "$INSTALL_DIR/batch.sql"
  272. chmod 600 "$INSTALL_DIR/batch.sql"
  273. keep_database_running
  274. mysql -u root --password="$MARIADB_PASSWORD" < "$INSTALL_DIR/batch.sql"
  275. rm "$INSTALL_DIR/batch.sql"
  276. }
  277. function initialise_database {
  278. database_name=$1
  279. database_file=$2
  280. keep_database_running
  281. mysql -u root --password="$MARIADB_PASSWORD" -D "$database_name" < "$database_file"
  282. # shellcheck disable=SC2181
  283. if [ ! "$?" = "0" ]; then
  284. exit 62952
  285. fi
  286. }
  287. function run_query {
  288. database_name=$1
  289. database_query=$2
  290. keep_database_running
  291. mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" "$database_name"
  292. }
  293. function run_query_root {
  294. database_name=$1
  295. database_query=$2
  296. keep_database_running
  297. mysql -e "$database_query" "$database_name"
  298. }
  299. function create_database {
  300. app_name="$1"
  301. app_admin_password="$2"
  302. app_admin_username=$3
  303. if [ ! -d "$INSTALL_DIR" ]; then
  304. mkdir "$INSTALL_DIR"
  305. fi
  306. if [ ! "$app_admin_username" ]; then
  307. app_admin_username="${app_name}admin"
  308. fi
  309. echo "create database ${app_name};
  310. CREATE USER '$app_admin_username@localhost' IDENTIFIED BY '${app_admin_password}';
  311. update mysql.user set plugin = '' where User='$app_admin_username@localhost';
  312. GRANT ALL PRIVILEGES ON ${app_name}.* TO '$app_admin_username@localhost';
  313. flush privileges;
  314. quit" > "$INSTALL_DIR/batch.sql"
  315. chmod 600 "$INSTALL_DIR/batch.sql"
  316. keep_database_running
  317. mysql -u root --password="$MARIADB_PASSWORD" < "$INSTALL_DIR/batch.sql"
  318. rm "$INSTALL_DIR/batch.sql"
  319. }
  320. function run_query_with_output {
  321. database_name=$1
  322. database_query=$2
  323. keep_database_running
  324. output=$(mysql -u root --password="$MARIADB_PASSWORD" << EOF
  325. use $database_name;
  326. $database_query
  327. EOF
  328. )
  329. echo "$output"
  330. }
  331. function drop_database {
  332. database_name="$1"
  333. get_mariadb_password
  334. echo "drop database ${app_name};
  335. flush privileges;
  336. quit" > "$INSTALL_DIR/batch.sql"
  337. chmod 600 "$INSTALL_DIR/batch.sql"
  338. keep_database_running
  339. mysql -u root --password="$MARIADB_PASSWORD" < "$INSTALL_DIR/batch.sql"
  340. rm "$INSTALL_DIR/batch.sql"
  341. }
  342. function database_reinstall {
  343. apt-get -yq purge mariadb*
  344. rm -rf /var/lib/mysql
  345. rm -rf /etc/mysql
  346. sed -i '/mariadb/d' "${HOME}/${PROJECT_NAME}-completed.txt"
  347. install_mariadb
  348. }
  349. function install_rethinkdb {
  350. if [[ "$(arch)" == "arm"* ]]; then
  351. echo $'rethinkdb does not currently support ARM debian packages'
  352. echo $"See http://download.rethinkdb.com/apt/dists/${DEBIAN_VERSION}/main"
  353. exit 723723452
  354. fi
  355. if [ ! -d "$INSTALL_DIR" ]; then
  356. mkdir -p "$INSTALL_DIR"
  357. fi
  358. cd "$INSTALL_DIR" || exit 6384684
  359. echo "deb http://download.rethinkdb.com/apt $DEBIAN_VERSION main" | tee /etc/apt/sources.list.d/rethinkdb.list
  360. wget -qO- https://download.rethinkdb.com/apt/pubkey.gpg | apt-key add -
  361. apt-get update
  362. apt-get -yq install rethinkdb
  363. { echo 'runuser=rethinkdb';
  364. echo 'rungroup=rethinkdb';
  365. echo '# pid-file=/var/run/rethinkdb/rethinkdb.pid';
  366. echo '# directory=/var/lib/rethinkdb/default';
  367. echo '# log-file=/var/log/rethinkdb';
  368. echo 'bind=127.0.0.1';
  369. echo '# canonical-address=';
  370. echo '# driver-port=28015';
  371. echo '# cluster-port=29015';
  372. echo '# join=example.com:29015';
  373. echo '# port-offset=0';
  374. echo '# reql-http-proxy=socks5://example.com:1080';
  375. echo '# http-port=8091';
  376. echo '# no-http-admin';
  377. echo '# cores=2';
  378. echo '# cache-size=1024';
  379. echo '# io-threads=64';
  380. echo '# direct-io';
  381. echo '# server-name=server1'; } > /etc/rethinkdb/instances.d/default.conf
  382. systemctl restart rethinkdb
  383. }
  384. function remove_rethinkdb {
  385. if [ ! -d /etc/rethinkdb ]; then
  386. return
  387. fi
  388. apt-get -yq remove rethinkdb
  389. if [ -d /etc/rethinkdb ]; then
  390. rm -rf /etc/rethinkdb
  391. fi
  392. if [ -f /etc/apt/sources.list.d/rethinkdb.list ]; then
  393. rm /etc/apt/sources.list.d/rethinkdb.list
  394. apt-get update
  395. fi
  396. }
  397. # NOTE: deliberately there is no "exit 0"