freedombone-utils-database 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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-2017 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 store_original_mariadb_password {
  35. if [ ! -f /root/.mariadboriginal ]; then
  36. echo $'Storing original mariadb password'
  37. ORIGINAL_MARIADB_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mariadb)
  38. # We can store this in plaintext because it will soon be of historical interest only
  39. echo -n "$ORIGINAL_MARIADB_PASSWORD" > /root/.mariadboriginal
  40. fi
  41. }
  42. function keep_database_running {
  43. if [ ! $(daemon_is_running mariadb) ]; then
  44. systemctl start mariadb
  45. fi
  46. }
  47. function remove_backup_database_local {
  48. database_name=$1
  49. sed -i "/# Backup the ${database_name} database/,/# End of ${database_name} database backup/d" /usr/bin/backupdatabases
  50. sed -i "/# Backup ${database_name}/,/# End of backup for ${database_name}/d" /etc/cron.weekly/backupdatabasesweekly
  51. sed -i "/# Backup ${database_name}/,/# End of backup for ${database_name}/d" /etc/cron.monthly/backupdatabasesmonthly
  52. sed -i "/${database_name}/d" /etc/cron.hourly/repair
  53. }
  54. function backup_database_local {
  55. # Makes local backups of databases which can then be automatically rolled
  56. # back if corruption is detected
  57. database_name=$1
  58. backup_databases_script=/usr/bin/backupdatabases
  59. if ! grep -q "# Check database daemon" /usr/bin/backupdatabases; then
  60. echo '' >> /usr/bin/backupdatabases
  61. echo '# Check database daemon is running' >> /usr/bin/backupdatabases
  62. echo 'if [ ! $(systemctl is-active mariadb >/dev/null 2>&1 && echo Running) ]; then' >> /usr/bin/backupdatabases
  63. echo ' systemctl start mariadb' >> /usr/bin/backupdatabases
  64. echo 'fi' >> /usr/bin/backupdatabases
  65. echo '' >> /usr/bin/backupdatabases
  66. fi
  67. if ! grep -q "# Backup the ${database_name} database" $backup_databases_script; then
  68. echo "# Backup the ${database_name} database" >> $backup_databases_script
  69. echo "TEMPFILE=/root/${database_name}.sql" >> $backup_databases_script
  70. echo "DAILYFILE=/var/backups/${database_name}_daily.sql" >> $backup_databases_script
  71. echo "mysqldump --password=\"\$MYSQL_PASSWORD\" ${database_name} > \$TEMPFILE" >> $backup_databases_script
  72. echo 'FILESIZE=$(stat -c%s $TEMPFILE)' >> $backup_databases_script
  73. echo 'if [ "$FILESIZE" -eq "0" ]; then' >> $backup_databases_script
  74. echo ' if [ -f $DAILYFILE ]; then' >> $backup_databases_script
  75. echo ' cp $DAILYFILE $TEMPFILE' >> $backup_databases_script
  76. echo '' >> $backup_databases_script
  77. echo ' # try to restore yesterdays database' >> $backup_databases_script
  78. echo " mysql -u root --password=\"\$MYSQL_PASSWORD\" ${database_name} -o < \$DAILYFILE" >> $backup_databases_script
  79. echo '' >> $backup_databases_script
  80. echo ' # Send a warning email' >> $backup_databases_script
  81. 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
  82. echo ' else' >> $backup_databases_script
  83. echo ' # Send a warning email' >> $backup_databases_script
  84. echo " echo \"Unable to create a backup of the ${database_name} database.\" | mail -s \"${database_name} backup\" \$EMAIL" >> $backup_databases_script
  85. echo ' fi' >> $backup_databases_script
  86. echo 'else' >> $backup_databases_script
  87. echo ' chmod 600 $TEMPFILE' >> $backup_databases_script
  88. echo ' mv $TEMPFILE $DAILYFILE' >> $backup_databases_script
  89. echo '' >> $backup_databases_script
  90. echo ' # Make the backup readable only by root' >> $backup_databases_script
  91. echo ' chmod 600 $DAILYFILE' >> $backup_databases_script
  92. echo 'fi' >> $backup_databases_script
  93. echo "# End of ${database_name} database backup" >> $backup_databases_script
  94. fi
  95. weekly_backup_script=/etc/cron.weekly/backupdatabasesweekly
  96. if ! grep -q "Backup ${database_name}" ${weekly_backup_script}; then
  97. echo "# Backup ${database_name}" >> ${weekly_backup_script}
  98. echo "if [ -f /var/backups/${database_name}_weekly.sql ]; then" >> ${weekly_backup_script}
  99. echo " cp -f /var/backups/${database_name}_weekly.sql /var/backups/${database_name}_2weekly.sql" >> ${weekly_backup_script}
  100. echo 'fi' >> ${weekly_backup_script}
  101. echo "if [ -f /var/backups/${database_name}_daily.sql ]; then" >> ${weekly_backup_script}
  102. echo " cp -f /var/backups/${database_name}_daily.sql /var/backups/${database_name}_weekly.sql" >> ${weekly_backup_script}
  103. echo 'fi' >> ${weekly_backup_script}
  104. echo "# End of backup for ${database_name}" >> ${weekly_backup_script}
  105. fi
  106. monthly_backup_script=/etc/cron.monthly/backupdatabasesmonthly
  107. if ! grep -q "Backup ${database_name}" ${monthly_backup_script}; then
  108. echo "# Backup ${database_name}" >> ${monthly_backup_script}
  109. echo "if [ -f /var/backups/${database_name}_monthly.sql ]; then" >> ${monthly_backup_script}
  110. echo " cp -f /var/backups/${database_name}_monthly.sql /var/backups/${database_name}_2monthly.sql" >> ${monthly_backup_script}
  111. echo 'fi' >> ${monthly_backup_script}
  112. echo "if [ -f /var/backups/${database_name}_weekly.sql ]; then" >> ${monthly_backup_script}
  113. echo " cp -f /var/backups/${database_name}_weekly.sql /var/backups/${database_name}_monthly.sql" >> ${monthly_backup_script}
  114. echo 'fi' >> ${monthly_backup_script}
  115. echo "# End of backup for ${database_name}" >> ${monthly_backup_script}
  116. fi
  117. if ! grep -q "${database_name}" /etc/cron.hourly/repair; then
  118. echo "${PROJECT_NAME}-repair-database ${database_name}" >> /etc/cron.hourly/repair
  119. # remove legacy stuff
  120. sed -i 's|/usr/bin/repairdatabase redmatrix||g' /etc/cron.hourly/repair
  121. fi
  122. }
  123. function get_mariadb_password {
  124. # migrate from database password file to using the password store
  125. DATABASE_PASSWORD_FILE=/root/dbpass
  126. if [ -f $DATABASE_PASSWORD_FILE ]; then
  127. MARIADB_PASSWORD=$(cat $DATABASE_PASSWORD_FILE)
  128. ${PROJECT_NAME}-pass -u root -a mariadb -p "$MARIADB_PASSWORD"
  129. stored_password=$(${PROJECT_NAME}-pass -u root -a mariadb)
  130. if [[ "$stored_password" == "$MARIADB_PASSWORD" ]]; then
  131. shred -zu $DATABASE_PASSWORD_FILE
  132. echo $'MariaDB password moved into password store'
  133. return
  134. fi
  135. fi
  136. MARIADB_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mariadb)
  137. if [[ "$MARIADB_PASSWORD" == *'failed'* ]]; then
  138. echo $'Could not obtain mariadb password'
  139. exit 835225
  140. fi
  141. }
  142. function mariadb_kill_stone_dead {
  143. systemctl stop mariadb
  144. kill_pid=$(ps aux | grep mysqld_safe | awk -F ' ' '{print $2}' | head -n 1)
  145. kill -9 $kill_pid
  146. kill_pid=$(ps aux | grep mysqld | awk -F ' ' '{print $2}' | head -n 1)
  147. kill -9 $kill_pid
  148. kill_pid=$(ps aux | grep mysqld | awk -F ' ' '{print $2}' | head -n 1)
  149. kill -9 $kill_pid
  150. }
  151. function mariadb_fix_authentication {
  152. # See http://www.pontikis.net/blog/debian-9-stretch-rc3-web-server-setup-php7-mariadb
  153. # https://mariadb.com/kb/en/mariadb/unix_socket-authentication-plugin
  154. remove_watchdog_daemon mariadb
  155. mariadb_kill_stone_dead
  156. mysqld_safe --skip-grant-tables &
  157. sleep 5
  158. mysql -u root --password="$MARIADB_PASSWORD" << EOF
  159. use mysql;
  160. update mysql.user set plugin = '' where User='root';
  161. UPDATE user SET Password=PASSWORD('$MARIADB_PASSWORD') where USER='root';
  162. flush privileges;
  163. EOF
  164. mariadb_kill_stone_dead
  165. sed -i 's| --skip-grant-tables||g' /lib/systemd/system/mariadb.service
  166. systemctl daemon-reload
  167. systemctl start mariadb
  168. add_watchdog_daemon mariadb
  169. }
  170. function mariadb_create_root_user {
  171. run_query mysql "CREATE USER 'root@localhost' IDENTIFIED BY '${MARIADB_PASSWORD}'; flush privileges;"
  172. run_query mysql "update mysql.user set plugin = '' where User='root@localhost'; flush privileges;"
  173. run_query mysql "GRANT ALL PRIVILEGES ON * TO 'root@localhost'; flush privileges;"
  174. }
  175. function install_mariadb {
  176. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  177. return
  178. fi
  179. apt-get -yq install software-properties-common debconf-utils
  180. apt-get -yq update
  181. remove_watchdog_daemon mariadb
  182. function_check get_mariadb_password
  183. get_mariadb_password
  184. if [ ! $MARIADB_PASSWORD ]; then
  185. if [ -f $IMAGE_PASSWORD_FILE ]; then
  186. MARIADB_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  187. else
  188. MARIADB_PASSWORD="$(openssl rand -base64 32 | cut -c1-${MINIMUM_PASSWORD_LENGTH})"
  189. fi
  190. fi
  191. ${PROJECT_NAME}-pass -u root -a mariadb -p "$MARIADB_PASSWORD"
  192. debconf-set-selections <<< "mariadb-server mariadb-server/root_password password $MARIADB_PASSWORD"
  193. debconf-set-selections <<< "mariadb-server mariadb-server/root_password_again password $MARIADB_PASSWORD"
  194. apt-get -yq install mariadb-server mariadb-client
  195. apt-get -yq remove --purge apache2-bin*
  196. if [ -d /etc/apache2 ]; then
  197. rm -rf /etc/apache2
  198. echo $'Removed Apache installation after MariaDB install'
  199. fi
  200. if [ ! -d /etc/mysql ]; then
  201. echo $"ERROR: mariadb-server does not appear to have installed. $CHECK_MESSAGE"
  202. exit 54
  203. fi
  204. if [ ! -f /usr/bin/mysql ]; then
  205. echo $"ERROR: mariadb-server does not appear to have installed. $CHECK_MESSAGE"
  206. exit 34672
  207. fi
  208. sed -i 's|ExecStart=/usr/sbin/mysqld|ExecStart=/usr/sbin/mysqld --skip-grant-tables|g' /lib/systemd/system/mariadb.service
  209. systemctl daemon-reload
  210. systemctl restart mariadb
  211. mariadb_fix_authentication
  212. mariadb_create_root_user
  213. mark_completed $FUNCNAME
  214. }
  215. function backup_databases_script_header {
  216. if [ ! -f /usr/bin/backupdatabases ]; then
  217. # daily
  218. echo '#!/bin/sh' > /usr/bin/backupdatabases
  219. echo '' >> /usr/bin/backupdatabases
  220. echo "EMAIL='$MY_EMAIL_ADDRESS'" >> /usr/bin/backupdatabases
  221. echo '' >> /usr/bin/backupdatabases
  222. echo "MYSQL_PASSWORD=\$(${PROJECT_NAME}-pass -u root -a mariadb)" >> /usr/bin/backupdatabases
  223. echo 'umask 0077' >> /usr/bin/backupdatabases
  224. echo '' >> /usr/bin/backupdatabases
  225. echo '# exit if we are backing up to friends servers' >> /usr/bin/backupdatabases
  226. echo "if [ -f $FRIENDS_SERVERS_LIST ]; then" >> /usr/bin/backupdatabases
  227. echo ' exit 1' >> /usr/bin/backupdatabases
  228. echo 'fi' >> /usr/bin/backupdatabases
  229. chmod 600 /usr/bin/backupdatabases
  230. chmod +x /usr/bin/backupdatabases
  231. echo '#!/bin/sh' > /etc/cron.daily/backupdatabasesdaily
  232. echo '/usr/bin/backupdatabases' >> /etc/cron.daily/backupdatabasesdaily
  233. chmod 600 /etc/cron.daily/backupdatabasesdaily
  234. chmod +x /etc/cron.daily/backupdatabasesdaily
  235. # weekly
  236. echo '#!/bin/sh' > /etc/cron.weekly/backupdatabasesweekly
  237. echo '' >> /etc/cron.weekly/backupdatabasesweekly
  238. echo 'umask 0077' >> /etc/cron.weekly/backupdatabasesweekly
  239. chmod 600 /etc/cron.weekly/backupdatabasesweekly
  240. chmod +x /etc/cron.weekly/backupdatabasesweekly
  241. # monthly
  242. echo '#!/bin/sh' > /etc/cron.monthly/backupdatabasesmonthly
  243. echo '' >> /etc/cron.monthly/backupdatabasesmonthly
  244. echo 'umask 0077' >> /etc/cron.monthly/backupdatabasesmonthly
  245. chmod 600 /etc/cron.monthly/backupdatabasesmonthly
  246. chmod +x /etc/cron.monthly/backupdatabasesmonthly
  247. fi
  248. }
  249. function repair_databases_script {
  250. if [ -f /etc/cron.hourly/repair ]; then
  251. sed -i "s|/usr/bin/repairdatabase|${PROJECT_NAME}-repair-database|g" /etc/cron.hourly/repair
  252. fi
  253. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  254. return
  255. fi
  256. db_pass=$(${PROJECT_NAME}-pass -u root -p mariadb)
  257. if [[ "$db_pass" == 'Error:'* ]]; then
  258. return
  259. fi
  260. echo '#!/bin/bash' > /etc/cron.hourly/repair
  261. echo '' >> /etc/cron.hourly/repair
  262. chmod 600 /etc/cron.hourly/repair
  263. chmod +x /etc/cron.hourly/repair
  264. mark_completed $FUNCNAME
  265. }
  266. function remove_database {
  267. app_name="$1"
  268. if [ ! -d $INSTALL_DIR ]; then
  269. mkdir $INSTALL_DIR
  270. fi
  271. echo "drop database ${app_name};
  272. quit" > $INSTALL_DIR/batch.sql
  273. chmod 600 $INSTALL_DIR/batch.sql
  274. keep_database_running
  275. mysql -u root --password="$MARIADB_PASSWORD" < $INSTALL_DIR/batch.sql
  276. shred -zu $INSTALL_DIR/batch.sql
  277. }
  278. function initialise_database {
  279. database_name=$1
  280. database_file=$2
  281. keep_database_running
  282. mysql -u root --password="$MARIADB_PASSWORD" -D $database_name < $database_file
  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. shred -zu $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. shred -zu $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' ~/${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
  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' > /etc/rethinkdb/instances.d/default.conf
  364. echo 'rungroup=rethinkdb' >> /etc/rethinkdb/instances.d/default.conf
  365. echo '# pid-file=/var/run/rethinkdb/rethinkdb.pid' >> /etc/rethinkdb/instances.d/default.conf
  366. echo '# directory=/var/lib/rethinkdb/default' >> /etc/rethinkdb/instances.d/default.conf
  367. echo '# log-file=/var/log/rethinkdb' >> /etc/rethinkdb/instances.d/default.conf
  368. echo 'bind=127.0.0.1' >> /etc/rethinkdb/instances.d/default.conf
  369. echo '# canonical-address=' >> /etc/rethinkdb/instances.d/default.conf
  370. echo '# driver-port=28015' >> /etc/rethinkdb/instances.d/default.conf
  371. echo '# cluster-port=29015' >> /etc/rethinkdb/instances.d/default.conf
  372. echo '# join=example.com:29015' >> /etc/rethinkdb/instances.d/default.conf
  373. echo '# port-offset=0' >> /etc/rethinkdb/instances.d/default.conf
  374. echo '# reql-http-proxy=socks5://example.com:1080' >> /etc/rethinkdb/instances.d/default.conf
  375. echo '# http-port=8091' >> /etc/rethinkdb/instances.d/default.conf
  376. echo '# no-http-admin' >> /etc/rethinkdb/instances.d/default.conf
  377. echo '# cores=2' >> /etc/rethinkdb/instances.d/default.conf
  378. echo '# cache-size=1024' >> /etc/rethinkdb/instances.d/default.conf
  379. echo '# io-threads=64' >> /etc/rethinkdb/instances.d/default.conf
  380. echo '# direct-io' >> /etc/rethinkdb/instances.d/default.conf
  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"