freedombone-restore-remote 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Restore from a given remote server
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  31. CONFIG_FILE=$HOME/${PROJECT_NAME}.cfg
  32. BACKUP_EXTRA_DIRECTORIES=/root/backup-extra-dirs.csv
  33. export TEXTDOMAIN=${PROJECT_NAME}-restore-remote
  34. export TEXTDOMAINDIR="/usr/share/locale"
  35. SERVER_NAME=$1
  36. # whether to restore everything or just a specific application
  37. RESTORE_APP='all'
  38. if [ ${2} ]; then
  39. RESTORE_APP=${2}
  40. fi
  41. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | -nawk -F ':' '{print $2}')
  42. ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME}
  43. # Temporary location for data to be backed up to other servers
  44. SERVER_DIRECTORY=/root/remoterestore
  45. BACKUP_LIST=/home/${ADMIN_USERNAME}/backup.list
  46. if [ ! $SERVER_NAME ]; then
  47. echo $'restorefromfriend [server]'
  48. exit 1
  49. fi
  50. if [ ! -f $BACKUP_LIST ]; then
  51. echo $"No friends list found at $BACKUP_LIST"
  52. exit 2
  53. fi
  54. if ! grep -q "$SERVER_NAME" $BACKUP_LIST; then
  55. echo $"Server not found within the friends list"
  56. exit 3
  57. fi
  58. REMOTE_SERVER=$(grep -i "$SERVER_NAME" $BACKUP_LIST | awk -F ' ' '{print $1}')
  59. REMOTE_SSH_PORT=$(grep -i "$SERVER_NAME" $BACKUP_LIST | awk -F ' ' '{print $2}')
  60. REMOTE_DIRECTORY=$(grep -i "$SERVER_NAME" $BACKUP_LIST | awk -F ' ' '{print $3}')
  61. REMOTE_PASSWORD=$(grep -i "$SERVER_NAME" $BACKUP_LIST | awk -F ' ' '{print $4}')
  62. REMOTE_SERVER=$REMOTE_SERVER:$REMOTE_DIRECTORY
  63. NOW=$(date +"%Y-%m-%d %H:%M:%S")
  64. echo "$NOW Starting restore from $REMOTE_SERVER" >> /var/log/remotebackups.log
  65. rsync -ratlzv --rsh="/usr/bin/sshpass -p $REMOTE_PASSWORD ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" $REMOTE_SERVER/backup $SERVER_DIRECTORY
  66. if [ ! "$?" = "0" ]; then
  67. echo "$NOW Restore from $REMOTE_SERVER failed" >> /var/log/remotebackups.log
  68. # Send a warning email
  69. echo "Restore from $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} restore from friend" $ADMIN_EMAIL_ADDRESS
  70. exit 790
  71. else
  72. echo "$NOW Restored encrypted data from $REMOTE_SERVER" >> /var/log/remotebackups.log
  73. fi
  74. # MariaDB password
  75. DATABASE_PASSWORD=$(cat /root/dbpass)
  76. function restore_directory_from_friend {
  77. if [ ! -d ${1} ]; then
  78. mkdir ${1}
  79. fi
  80. obnam restore -r $SERVER_DIRECTORY/backup/${2} --to ${1}
  81. }
  82. function copy_gpg_keys {
  83. echo $"Copying GPG keys from admin user to root"
  84. cp -r /home/$ADMIN_USERNAME/.gnupg /root
  85. }
  86. function restore_database_from_friend {
  87. DATABASE_PASSWORD=
  88. RESTORE_SUBDIR="root"
  89. if [ -d $SERVER_DIRECTORY/backup/${1} ]; then
  90. echo $"Restoring ${1} database"
  91. restore_directory_from_friend /root/temp${1}data ${1}data
  92. if [ ! -f /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql ]; then
  93. echo $"Unable to restore ${1} database"
  94. rm -rf /root/temp${1}data
  95. exit 503
  96. fi
  97. mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${1} -o < /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
  98. if [ ! "$?" = "0" ]; then
  99. echo "$mysqlsuccess"
  100. exit 964
  101. fi
  102. shred -zu /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/*
  103. rm -rf /root/temp${1}data
  104. echo $"Restoring ${1} installation"
  105. restore_directory_from_friend /root/temp${1} ${1}
  106. RESTORE_SUBDIR="var"
  107. if [ ${1} ]; then
  108. if [ -d /var/www/${2}/htdocs ]; then
  109. if [ -d /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs ]; then
  110. rm -rf /var/www/${2}/htdocs
  111. mv /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs /var/www/${2}/
  112. if [ ! "$?" = "0" ]; then
  113. exit 683
  114. fi
  115. if [ -d /etc/letsencrypt/live/${2} ]; then
  116. ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
  117. ln -s /etc/letsencrypt/live/${2}/fullchain.pem /etc/ssl/certs/${2}.pem
  118. else
  119. # Ensure that the bundled SSL cert is being used
  120. if [ -f /etc/ssl/certs/${2}.bundle.crt ]; then
  121. sed -i "s|${2}.crt|${2}.bundle.crt|g" /etc/nginx/sites-available/${2}
  122. fi
  123. fi
  124. fi
  125. fi
  126. fi
  127. fi
  128. }
  129. function restore_configuration {
  130. if [[ $RESTORE_APP != 'all' ]]; then
  131. if [[ $RESTORE_APP != 'configuration' ]]; then
  132. return
  133. fi
  134. fi
  135. if [ -d $SERVER_DIRECTORY/backup/config ]; then
  136. echo $"Restoring configuration files"
  137. restore_directory_from_friend /root/tempconfig config
  138. cp -f /root/tempconfig/root/${PROJECT_NAME}.cfg $CONFIG_FILE
  139. if [ ! "$?" = "0" ]; then
  140. unmount_drive
  141. rm -rf /root/tempconfig
  142. exit 5372
  143. fi
  144. if [ -f $CONFIG_FILE ]; then
  145. # install according to the config file
  146. freedombone -c $CONFIG_FILE
  147. fi
  148. cp -f /root/tempconfig/root/${PROJECT_NAME}-completed.txt $COMPLETION_FILE
  149. if [ ! "$?" = "0" ]; then
  150. unmount_drive
  151. rm -rf /root/tempconfig
  152. exit 7252
  153. fi
  154. if [ -f /root/tempconfig${BACKUP_EXTRA_DIRECTORIES} ]; then
  155. cp -f /root/tempconfig${BACKUP_EXTRA_DIRECTORIES} ${BACKUP_EXTRA_DIRECTORIES}
  156. if [ ! "$?" = "0" ]; then
  157. unmount_drive
  158. rm -rf /root/tempconfig
  159. exit 62121
  160. fi
  161. fi
  162. rm -rf /root/tempconfig
  163. fi
  164. }
  165. function restore_mariadb {
  166. if [[ $RESTORE_APP != 'all' ]]; then
  167. if [[ $RESTORE_APP != 'mariadb' ]]; then
  168. return
  169. fi
  170. fi
  171. if [ -d $SERVER_DIRECTORY/backup/mariadb ]; then
  172. echo $"Restoring MariaDB settings"
  173. restore_directory_from_friend /root/tempmariadb mariadb
  174. echo $"Get the MariaDB password from the backup"
  175. if [ ! -f /root/tempmariadb/root/tempmariadb/db ]; then
  176. echo $"MariaDB password file not found"
  177. exit 495
  178. fi
  179. BACKUP_MARIADB_PASSWORD=$(cat /root/tempmariadb/root/tempmariadb/db)
  180. if [[ "$BACKUP_MARIADB_PASSWORD" != "$DATABASE_PASSWORD" ]]; then
  181. echo $"Restore the MariaDB user table"
  182. mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" mysql -o < /root/tempmariadb/root/tempmariadb/mysql.sql)
  183. if [ ! "$?" = "0" ]; then
  184. echo $"Try again using the password obtained from backup"
  185. mysqlsuccess=$(mysql -u root --password="$BACKUP_MARIADB_PASSWORD" mysql -o < /root/tempmariadb/root/tempmariadb/mysql.sql)
  186. fi
  187. if [ ! "$?" = "0" ]; then
  188. echo "$mysqlsuccess"
  189. exit 962
  190. fi
  191. echo $"Restarting database"
  192. service mysql restart
  193. echo $"Change the MariaDB password to the backup version"
  194. DATABASE_PASSWORD=$BACKUP_MARIADB_PASSWORD
  195. fi
  196. shred -zu /root/tempmariadb/root/tempmariadb/db
  197. rm -rf /root/tempmariadb
  198. # Change database password file
  199. echo "$DATABASE_PASSWORD" > /root/dbpass
  200. chmod 600 /root/dbpass
  201. fi
  202. }
  203. function restore_letsencrypt {
  204. if [[ $RESTORE_APP != 'all' ]]; then
  205. if [[ $RESTORE_APP != 'letsencrypt' ]]; then
  206. return
  207. fi
  208. fi
  209. if [ -d $SERVER_DIRECTORY/backup/letsencrypt ]; then
  210. echo $"Restoring Lets Encrypt settings"
  211. restore_directory_from_friend / letsencrypt
  212. fi
  213. }
  214. function restore_tor {
  215. if [[ $RESTORE_APP != 'all' ]]; then
  216. if [[ $RESTORE_APP != 'tor' ]]; then
  217. return
  218. fi
  219. fi
  220. if [ -d $SERVER_DIRECTORY/backup/tor ]; then
  221. echo $"Restoring Tor settings"
  222. restore_directory_from_friend / tor
  223. fi
  224. }
  225. function restore_mutt_settings {
  226. if [[ $RESTORE_APP != 'all' ]]; then
  227. if [[ $RESTORE_APP != 'mutt' ]]; then
  228. return
  229. fi
  230. fi
  231. for d in $SERVER_DIRECTORY/backup/mutt/*/ ; do
  232. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  233. if [[ $USERNAME != "git" ]]; then
  234. if [ -d $SERVER_DIRECTORY/backup/mutt/$USERNAME ]; then
  235. if [ ! -d /home/$USERNAME ]; then
  236. ${PROJECT_NAME}-adduser $USERNAME
  237. fi
  238. echo $"Restoring Mutt settings for $USERNAME"
  239. restore_directory_from_friend /root/tempmutt mutt/$USERNAME
  240. if [ -f /root/tempmutt/home/$USERNAME/tempbackup/.muttrc ]; then
  241. cp -f /root/tempmutt/home/$USERNAME/tempbackup/.muttrc /home/$USERNAME/.muttrc
  242. fi
  243. if [ -f /root/tempmutt/home/$USERNAME/tempbackup/Muttrc ]; then
  244. cp -f /root/tempmutt/home/$USERNAME/tempbackup/Muttrc /etc/Muttrc
  245. fi
  246. if [ ! "$?" = "0" ]; then
  247. rm -rf /root/tempmutt
  248. exit 276
  249. fi
  250. rm -rf /root/tempmutt
  251. fi
  252. fi
  253. done
  254. }
  255. function restore_gpg {
  256. if [[ $RESTORE_APP != 'all' ]]; then
  257. if [[ $RESTORE_APP != 'gpg' ]]; then
  258. return
  259. fi
  260. fi
  261. for d in $SERVER_DIRECTORY/backup/gnupg/*/ ; do
  262. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  263. if [[ $USERNAME != "git" ]]; then
  264. if [ -d $SERVER_DIRECTORY/backup/gnupg/$USERNAME ]; then
  265. if [ ! -d /home/$USERNAME ]; then
  266. ${PROJECT_NAME}-adduser $USERNAME
  267. fi
  268. echo $"Restoring gnupg settings for $USERNAME"
  269. restore_directory_from_friend /root/tempgnupg gnupg/$USERNAME
  270. cp -r /root/tempgnupg/home/$USERNAME/.gnupg /home/$USERNAME/
  271. if [ ! "$?" = "0" ]; then
  272. rm -rf /root/tempgnupg
  273. exit 276
  274. fi
  275. rm -rf /root/tempgnupg
  276. if [[ "$USERNAME" == "$ADMIN_USERNAME" ]]; then
  277. cp -r /home/$USERNAME/.gnupg /root
  278. if [ ! "$?" = "0" ]; then
  279. exit 283
  280. fi
  281. fi
  282. fi
  283. fi
  284. done
  285. }
  286. function restore_procmail {
  287. if [[ $RESTORE_APP != 'all' ]]; then
  288. if [[ $RESTORE_APP != 'procmail' ]]; then
  289. return
  290. fi
  291. fi
  292. for d in $SERVER_DIRECTORY/backup/procmail/*/ ; do
  293. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  294. if [[ $USERNAME != "git" ]]; then
  295. if [ -d $SERVER_DIRECTORY/backup/procmail/$USERNAME ]; then
  296. if [ ! -d /home/$USERNAME ]; then
  297. ${PROJECT_NAME}-adduser $USERNAME
  298. fi
  299. echo $"Restoring procmail settings for $USERNAME"
  300. restore_directory_from_friend /root/tempprocmail procmail/$USERNAME
  301. cp -f /root/tempprocmail/home/$USERNAME/tempbackup/.procmailrc /home/$USERNAME/
  302. if [ ! "$?" = "0" ]; then
  303. rm -rf /root/tempprocmail
  304. exit 276
  305. fi
  306. rm -rf /root/tempprocmail
  307. fi
  308. fi
  309. done
  310. }
  311. function restore_spamassassin {
  312. if [[ $RESTORE_APP != 'all' ]]; then
  313. if [[ $RESTORE_APP != 'spamassassin' ]]; then
  314. return
  315. fi
  316. fi
  317. for d in $SERVER_DIRECTORY/backup/spamassassin/*/ ; do
  318. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  319. if [[ $USERNAME != "git" ]]; then
  320. if [ -d $SERVER_DIRECTORY/backup/spamassassin/$USERNAME ]; then
  321. if [ ! -d /home/$USERNAME ]; then
  322. ${PROJECT_NAME}-adduser $USERNAME
  323. fi
  324. echo $"Restoring spamassassin settings for $USERNAME"
  325. restore_directory_from_friend /root/tempspamassassin spamassassin/$USERNAME
  326. cp -rf /root/tempspamassassin/home/$USERNAME/.spamassassin /home/$USERNAME/
  327. if [ ! "$?" = "0" ]; then
  328. rm -rf /root/tempspamassassin
  329. exit 276
  330. fi
  331. rm -rf /root/tempspamassassin
  332. fi
  333. fi
  334. done
  335. }
  336. function restore_admin_readme {
  337. if [[ $RESTORE_APP != 'all' ]]; then
  338. if [[ $RESTORE_APP != 'readme' ]]; then
  339. return
  340. fi
  341. fi
  342. if [ -d $SERVER_DIRECTORY/backup/readme ]; then
  343. echo $"Restoring README"
  344. restore_directory_from_friend /root/tempreadme readme
  345. cp -f /root/tempreadme/home/$ADMIN_USERNAME/tempbackup/README /home/$ADMIN_USERNAME/
  346. if [ ! "$?" = "0" ]; then
  347. rm -rf /root/tempreadme
  348. exit 276
  349. fi
  350. rm -rf /root/tempreadme
  351. fi
  352. }
  353. function restore_ipfs {
  354. if [[ $RESTORE_APP != 'all' ]]; then
  355. if [[ $RESTORE_APP != 'ipfs' ]]; then
  356. return
  357. fi
  358. fi
  359. if [ -d $SERVER_DIRECTORY/backup/ipfs ]; then
  360. echo $"Restoring IPFS"
  361. restore_directory_from_friend /root/tempipfs ipfs
  362. cp -rf /root/tempipfs/home/$ADMIN_USERNAME/.ipfs/* /home/$ADMIN_USERNAME/.ipfs
  363. if [ ! "$?" = "0" ]; then
  364. rm -rf /root/tempipfs
  365. exit 276
  366. fi
  367. rm -rf /root/tempipfs
  368. fi
  369. }
  370. function restore_ssh_keys {
  371. if [[ $RESTORE_APP != 'all' ]]; then
  372. if [[ $RESTORE_APP != 'ssh' ]]; then
  373. return
  374. fi
  375. fi
  376. for d in $SERVER_DIRECTORY/backup/ssh/*/ ; do
  377. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  378. if [[ $USERNAME != "git" ]]; then
  379. if [ -d $SERVER_DIRECTORY/backup/ssh/$USERNAME ]; then
  380. if [ ! -d /home/$USERNAME ]; then
  381. ${PROJECT_NAME}-adduser $USERNAME
  382. fi
  383. echo $"Restoring ssh keys for $USERNAME"
  384. restore_directory_from_friend /root/tempssh ssh/$USERNAME
  385. cp -r /root/tempssh/home/$USERNAME/.ssh /home/$USERNAME/
  386. if [ ! "$?" = "0" ]; then
  387. rm -rf /root/tempssh
  388. exit 664
  389. fi
  390. rm -rf /root/tempssh
  391. fi
  392. fi
  393. done
  394. }
  395. function restore_user_config {
  396. if [[ $RESTORE_APP != 'all' ]]; then
  397. if [[ $RESTORE_APP != 'userconfig' ]]; then
  398. return
  399. fi
  400. fi
  401. for d in $SERVER_DIRECTORY/backup/config/*/ ; do
  402. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  403. if [[ $USERNAME != "git" ]]; then
  404. if [ -d $SERVER_DIRECTORY/backup/config/$USERNAME ]; then
  405. if [ ! -d /home/$USERNAME ]; then
  406. ${PROJECT_NAME}-adduser $USERNAME
  407. fi
  408. echo $"Restoring config files for $USERNAME"
  409. restore_directory_from_friend /root/tempconfig config/$USERNAME
  410. cp -r /root/tempconfig/home/$USERNAME/.config /home/$USERNAME/
  411. if [ ! "$?" = "0" ]; then
  412. rm -rf /root/tempconfig
  413. exit 664
  414. fi
  415. rm -rf /root/tempconfig
  416. fi
  417. fi
  418. done
  419. }
  420. function restore_certs {
  421. if [[ $RESTORE_APP != 'all' ]]; then
  422. if [[ $RESTORE_APP != 'certs' ]]; then
  423. return
  424. fi
  425. fi
  426. if [ -d $SERVER_DIRECTORY/backup/ssl ]; then
  427. echo $"Restoring certificates"
  428. restore_directory_from_friend /root/tempssl ssl
  429. cp -r /root/tempssl/etc/ssl/* /etc/ssl
  430. if [ ! "$?" = "0" ]; then
  431. exit 276
  432. fi
  433. rm -rf /root/tempssl
  434. fi
  435. }
  436. function restore_personal_settings {
  437. if [[ $RESTORE_APP != 'all' ]]; then
  438. if [[ $RESTORE_APP != 'personal' ]]; then
  439. return
  440. fi
  441. fi
  442. for d in $SERVER_DIRECTORY/backup/personal/*/ ; do
  443. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  444. if [[ $USERNAME != "git" ]]; then
  445. if [ -d $SERVER_DIRECTORY/backup/personal/$USERNAME ]; then
  446. if [ ! -d /home/$USERNAME ]; then
  447. ${PROJECT_NAME}-adduser $USERNAME
  448. fi
  449. echo $"Restoring personal settings for $USERNAME"
  450. restore_directory_from_friend /root/temppersonal personal/$USERNAME
  451. if [ -d /home/$USERNAME/personal ]; then
  452. rm -rf /home/$USERNAME/personal
  453. fi
  454. mv /root/temppersonal/home/$USERNAME/personal /home/$USERNAME
  455. if [ ! "$?" = "0" ]; then
  456. exit 184
  457. fi
  458. rm -rf /root/temppersonal
  459. fi
  460. fi
  461. done
  462. }
  463. function restore_mailing_list {
  464. if [[ $RESTORE_APP != 'all' ]]; then
  465. if [[ $RESTORE_APP != 'mailinglist' ]]; then
  466. return
  467. fi
  468. fi
  469. if [ -d /var/spool/mlmmj ]; then
  470. echo $"Restoring public mailing list"
  471. restore_directory_from_friend /root/tempmailinglist mailinglist
  472. cp -r /root/tempmailinglist/root/spool/mlmmj/* /var/spool/mlmmj
  473. if [ ! "$?" = "0" ]; then
  474. exit 526
  475. fi
  476. rm -rf /root/tempmailinglist
  477. fi
  478. }
  479. function restore_xmpp {
  480. if [[ $RESTORE_APP != 'all' ]]; then
  481. if [[ $RESTORE_APP != 'xmpp' ]]; then
  482. return
  483. fi
  484. fi
  485. if [ -d /var/lib/prosody ]; then
  486. echo $"Restoring XMPP settings"
  487. restore_directory_from_friend /root/tempxmpp xmpp
  488. cp -r /root/tempxmpp/var/lib/prosody/* /var/lib/prosody
  489. if [ ! "$?" = "0" ]; then
  490. exit 725
  491. fi
  492. rm -rf /root/tempxmpp
  493. service prosody restart
  494. chown -R prosody:prosody /var/lib/prosody/*
  495. fi
  496. }
  497. function restore_gnu_social {
  498. if [[ $RESTORE_APP != 'all' ]]; then
  499. if [[ $RESTORE_APP != 'gnusocial' ]]; then
  500. return
  501. fi
  502. fi
  503. if grep -q "GNU Social domain" $COMPLETION_FILE; then
  504. MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
  505. restore_database_from_friend gnusocial ${MICROBLOG_DOMAIN_NAME}
  506. if [ -d /root/tempgnusocial ]; then
  507. rm -rf /root/tempgnusocial
  508. fi
  509. fi
  510. }
  511. function restore_hubzilla {
  512. if [[ $RESTORE_APP != 'all' ]]; then
  513. if [[ $RESTORE_APP != 'hubzilla' ]]; then
  514. return
  515. fi
  516. fi
  517. if grep -q "Hubzilla domain" $COMPLETION_FILE; then
  518. HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
  519. restore_database_from_friend hubzilla ${HUBZILLA_DOMAIN_NAME}
  520. if [ -d $SERVER_DIRECTORY/backup/hubzilla ]; then
  521. if [ ! -d /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3 ]; then
  522. mkdir -p /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  523. fi
  524. chmod 777 /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  525. chown -R www-data:www-data /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/*
  526. fi
  527. if [ -d /root/temphubzilla ]; then
  528. rm -rf /root/temphubzilla
  529. fi
  530. fi
  531. }
  532. function restore_owncloud {
  533. if [[ $RESTORE_APP != 'all' ]]; then
  534. if [[ $RESTORE_APP != 'owncloud' ]]; then
  535. return
  536. fi
  537. fi
  538. if grep -q "Owncloud domain" $COMPLETION_FILE; then
  539. OWNCLOUD_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Owncloud domain" | awk -F ':' '{print $2}')
  540. restore_database_from_friend owncloud $OWNCLOUD_DOMAIN_NAME
  541. if [ -d $SERVER_DIRECTORY/backup/owncloud ]; then
  542. echo $"Restoring Owncloud installation"
  543. cp -r /root/tempowncloud/var/lib/owncloud/* /var/lib/owncloud/
  544. if [ ! "$?" = "0" ]; then
  545. exit 981
  546. fi
  547. restore_directory_from_friend /root/tempowncloud2 owncloud2
  548. cp -r /root/tempowncloud2/etc/owncloud/* /etc/owncloud/
  549. if [ ! "$?" = "0" ]; then
  550. exit 982
  551. fi
  552. rm -rf /root/tempowncloud
  553. rm -rf /root/tempowncloud2
  554. chown -R www-data:www-data /var/lib/owncloud/data
  555. chown -R www-data:www-data /var/lib/owncloud/backup
  556. chown -R www-data:www-data /var/lib/owncloud/assets
  557. for d in /home/*/ ; do
  558. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  559. if [[ $USERNAME != "git" ]]; then
  560. occ files:scan $USERNAME
  561. fi
  562. done
  563. ln -s /usr/share/owncloud /var/www/${OWNCLOUD_DOMAIN_NAME}/htdocs
  564. fi
  565. fi
  566. }
  567. function restore_gogs {
  568. if [[ $RESTORE_APP != 'all' ]]; then
  569. if [[ $RESTORE_APP != 'gogs' ]]; then
  570. return
  571. fi
  572. fi
  573. if grep -q "Gogs domain" $COMPLETION_FILE; then
  574. GIT_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
  575. restore_database_from_friend gogs $GIT_DOMAIN_NAME
  576. if [ -d $SERVER_DIRECTORY/backup/gogs ]; then
  577. if [ ! -d /home/git/go/src/github.com/gogits/gogs/custom ]; then
  578. mkdir -p /home/git/go/src/github.com/gogits/gogs/custom
  579. fi
  580. cp -r /root/tempgogs/home/git/go/src/github.com/gogits/gogs/custom/* /home/git/go/src/github.com/gogits/gogs/custom/
  581. if [ ! "$?" = "0" ]; then
  582. exit 5885
  583. fi
  584. echo $"Restoring Gogs repos"
  585. restore_directory_from_friend /root/tempgogsrepos gogsrepos
  586. cp -r /root/tempgogsrepos/home/git/gogs-repositories/* /home/git/gogs-repositories/
  587. if [ ! "$?" = "0" ]; then
  588. exit 7649
  589. fi
  590. echo $"Restoring Gogs authorized_keys"
  591. restore_directory_from_friend /root/tempgogsssh gogsssh
  592. if [ ! -d /home/git/.ssh ]; then
  593. mkdir /home/git/.ssh
  594. fi
  595. cp -r /root/tempgogsssh/home/git/.ssh/* /home/git/.ssh/
  596. if [ ! "$?" = "0" ]; then
  597. exit 74239
  598. fi
  599. rm -rf /root/tempgogs
  600. rm -rf /root/tempgogsrepos
  601. rm -rf /root/tempgogsssh
  602. chown -R git:git /home/git
  603. fi
  604. fi
  605. }
  606. function restore_wiki {
  607. if [[ $RESTORE_APP != 'all' ]]; then
  608. if [[ $RESTORE_APP != 'wiki' ]]; then
  609. return
  610. fi
  611. fi
  612. if [ -d $SERVER_DIRECTORY/backup/wiki ]; then
  613. WIKI_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Wiki domain" | awk -F ':' '{print $2}')
  614. echo $"Restoring Wiki installation $WIKI_DOMAIN_NAME"
  615. restore_directory_from_friend /root/tempwiki wiki
  616. cp -r /root/tempwiki/var/lib/dokuwiki/* /var/lib/dokuwiki/
  617. if [ ! "$?" = "0" ]; then
  618. exit 868
  619. fi
  620. restore_directory_from_friend /root/tempwiki2 wiki2
  621. cp -r /root/tempwiki2/etc/dokuwiki/* /etc/dokuwiki/
  622. if [ ! "$?" = "0" ]; then
  623. exit 869
  624. fi
  625. rm -rf /root/tempwiki
  626. rm -rf /root/tempwiki2
  627. chown -R www-data:www-data /var/lib/dokuwiki/*
  628. # Ensure that the bundled SSL cert is being used
  629. if [ -f /etc/ssl/certs/${WIKI_DOMAIN_NAME}.bundle.crt ]; then
  630. sed -i "s|${WIKI_DOMAIN_NAME}.crt|${WIKI_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${WIKI_DOMAIN_NAME}
  631. fi
  632. if [ -d /etc/letsencrypt/live/${WIKI_DOMAIN_NAME} ]; then
  633. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${WIKI_DOMAIN_NAME}.key
  634. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${WIKI_DOMAIN_NAME}.pem
  635. fi
  636. fi
  637. }
  638. function restore_blog {
  639. if [[ $RESTORE_APP != 'all' ]]; then
  640. if [[ $RESTORE_APP != 'blog' ]]; then
  641. return
  642. fi
  643. fi
  644. if [ -d $SERVER_DIRECTORY/backup/blog ]; then
  645. FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
  646. echo $"Restoring blog installation $FULLBLOG_DOMAIN_NAME"
  647. mkdir /root/tempblog
  648. restore_directory_from_friend /root/tempblog blog
  649. rm -rf /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs
  650. cp -r /root/tempblog/var/www/${FULLBLOG_DOMAIN_NAME}/htdocs /var/www/${FULLBLOG_DOMAIN_NAME}/
  651. if [ ! "$?" = "0" ]; then
  652. exit 593
  653. fi
  654. rm -rf /root/tempblog
  655. if [ ! -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content ]; then
  656. echo $"No content directory found after restoring blog"
  657. exit 287
  658. fi
  659. # Ensure that the bundled SSL cert is being used
  660. if [ -f /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.bundle.crt ]; then
  661. sed -i "s|${FULLBLOG_DOMAIN_NAME}.crt|${FULLBLOG_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${FULLBLOG_DOMAIN_NAME}
  662. fi
  663. for d in /home/*/ ; do
  664. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  665. if [[ $USERNAME != "git" ]]; then
  666. if [ -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post ]; then
  667. mv /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/*.md /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post
  668. fi
  669. done
  670. if [ -d /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME} ]; then
  671. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${FULLBLOG_DOMAIN_NAME}.key
  672. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.pem
  673. fi
  674. fi
  675. }
  676. function restore_cjdns {
  677. if [[ $RESTORE_APP != 'all' ]]; then
  678. if [[ $RESTORE_APP != 'cjdns' ]]; then
  679. return
  680. fi
  681. fi
  682. if [ -d $SERVER_DIRECTORY/backup/cjdns ]; then
  683. echo $"Restoring cjdns installation"
  684. restore_directory_from_friend /root/tempcjdns cjdns
  685. rm -rf /etc/cjdns
  686. cp -r /root/tempcjdns/etc/cjdns /etc/
  687. if [ ! "$?" = "0" ]; then
  688. exit 7438
  689. fi
  690. rm -rf /root/tempcjdns
  691. fi
  692. }
  693. function restore_voip {
  694. if [[ $RESTORE_APP != 'all' ]]; then
  695. if [[ $RESTORE_APP != 'voip' ]]; then
  696. return
  697. fi
  698. fi
  699. if [ -d $SERVER_DIRECTORY/backup/voip ]; then
  700. echo $"Restoring VoIP settings"
  701. restore_directory_from_friend /root/tempvoip voip
  702. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.ini /etc/
  703. if [ ! "$?" = "0" ]; then
  704. rm -rf /root/tempvoip
  705. exit 7823
  706. fi
  707. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/sipwitch.conf /etc/sipwitch.conf
  708. if [ ! "$?" = "0" ]; then
  709. rm -rf /root/tempvoip
  710. exit 7823
  711. fi
  712. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.sqlite /var/lib/mumble-server/
  713. if [ ! "$?" = "0" ]; then
  714. rm -rf /root/tempvoip
  715. exit 276
  716. fi
  717. rm -rf /root/tempvoip
  718. cp /etc/ssl/certs/mumble* /var/lib/mumble-server
  719. cp /etc/ssl/private/mumble* /var/lib/mumble-server
  720. chown -R mumble-server:mumble-server /var/lib/mumble-server
  721. service sipwitch restart
  722. service mumble-server restart
  723. fi
  724. }
  725. function restore_tox {
  726. if [[ $RESTORE_APP != 'all' ]]; then
  727. if [[ $RESTORE_APP != 'tox' ]]; then
  728. return
  729. fi
  730. fi
  731. if [ -d $SERVER_DIRECTORY/backup/tox ]; then
  732. echo $"Restoring Tox node settings"
  733. restore_directory_from_friend / tox
  734. if [ ! "$?" = "0" ]; then
  735. exit 93653
  736. fi
  737. cp /var/lib/tox-bootstrapd/tox-bootstrapd.conf /etc/tox-bootstrapd.conf
  738. systemctl restart tox-bootstrapd.service
  739. if [ ! "$?" = "0" ]; then
  740. systemctl status tox-bootstrapd.service
  741. exit 59369
  742. fi
  743. fi
  744. }
  745. function restore_email {
  746. if [[ $RESTORE_APP != 'all' ]]; then
  747. if [[ $RESTORE_APP != 'email' ]]; then
  748. return
  749. fi
  750. fi
  751. for d in $SERVER_DIRECTORY/backup/mail/*/ ; do
  752. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  753. if [[ $USERNAME != "git" ]]; then
  754. if [ -d $SERVER_DIRECTORY/backup/mail/$USERNAME ]; then
  755. if [ ! -d /home/$USERNAME ]; then
  756. ${PROJECT_NAME}-adduser $USERNAME
  757. fi
  758. echo $"Restoring emails for $USERNAME"
  759. restore_directory_from_friend /root/tempmail mail/$USERNAME
  760. if [ ! -d /home/$USERNAME/Maildir ]; then
  761. mkdir /home/$USERNAME/Maildir
  762. fi
  763. tar -xzvf /root/tempmail/root/tempbackupemail/$USERNAME/maildir.tar.gz -C /
  764. if [ ! "$?" = "0" ]; then
  765. exit 927
  766. fi
  767. rm -rf /root/tempmail
  768. fi
  769. fi
  770. done
  771. }
  772. function restore_dlna {
  773. if [[ $RESTORE_APP != 'all' ]]; then
  774. if [[ $RESTORE_APP != 'dlna' ]]; then
  775. return
  776. fi
  777. fi
  778. if [ -d /var/cache/minidlna ]; then
  779. if [ -d $SERVER_DIRECTORY/backup/dlna ]; then
  780. echo $"Restoring DLNA cache"
  781. restore_directory_from_friend /root/tempdlna dlna
  782. cp -r /root/tempdlna/var/cache/minidlna/* /var/cache/minidlna/
  783. if [ ! "$?" = "0" ]; then
  784. exit 982
  785. fi
  786. rm -rf /root/tempdlna
  787. fi
  788. fi
  789. }
  790. # Social key management
  791. # Recover any key fragments and reconstruct the gpg key
  792. ${PROJECT_NAME}-recoverkey -u ${ADMIN_USERNAME} -l $BACKUP_LIST
  793. copy_gpg_keys
  794. restore_configuration
  795. restore_mariadb
  796. restore_letsencrypt
  797. restore_mutt_settings
  798. restore_gpg
  799. restore_procmail
  800. restore_spamassassin
  801. restore_admin_readme
  802. restore_ipfs
  803. restore_ssh_keys
  804. restore_user_config
  805. restore_certs
  806. restore_personal_settings
  807. restore_mailing_list
  808. restore_xmpp
  809. restore_gnu_social
  810. restore_hubzilla
  811. restore_owncloud
  812. restore_gogs
  813. restore_wiki
  814. restore_blog
  815. restore_cjdns
  816. restore_voip
  817. restore_tox
  818. restore_email
  819. restore_dlna
  820. echo $"*** Remote restore was successful ***"
  821. exit 0