freedombone-restore-remote 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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-2016 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. # restore ownership
  435. if [ -f /etc/ssl/private/xmpp.key ]; then
  436. chown prosody:prosody /etc/ssl/private/xmpp.key
  437. chown prosody:prosody /etc/ssl/certs/xmpp.*
  438. fi
  439. if [ -d /etc/dovecot ]; then
  440. chown root:dovecot /etc/ssl/private/dovecot.*
  441. chown root:dovecot /etc/ssl/certs/dovecot.*
  442. fi
  443. if [ -f /etc/ssl/private/exim.key ]; then
  444. cp /etc/ssl/private/exim.key /etc/exim4
  445. cp /etc/ssl/certs/exim.crt /etc/exim4
  446. cp /etc/ssl/certs/exim.dhparam /etc/exim4
  447. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  448. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  449. fi
  450. if [ -f /etc/ssl/private/mumble.key ]; then
  451. if [ -d /var/lib/mumble-server ]; then
  452. cp /etc/ssl/certs/mumble.* /var/lib/mumble-server
  453. cp /etc/ssl/private/mumble.key /var/lib/mumble-server
  454. chown -R mumble-server:mumble-server /var/lib/mumble-server
  455. fi
  456. fi
  457. fi
  458. }
  459. function restore_personal_settings {
  460. if [[ $RESTORE_APP != 'all' ]]; then
  461. if [[ $RESTORE_APP != 'personal' ]]; then
  462. return
  463. fi
  464. fi
  465. for d in $SERVER_DIRECTORY/backup/personal/*/ ; do
  466. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  467. if [[ $USERNAME != "git" ]]; then
  468. if [ -d $SERVER_DIRECTORY/backup/personal/$USERNAME ]; then
  469. if [ ! -d /home/$USERNAME ]; then
  470. ${PROJECT_NAME}-adduser $USERNAME
  471. fi
  472. echo $"Restoring personal settings for $USERNAME"
  473. restore_directory_from_friend /root/temppersonal personal/$USERNAME
  474. if [ -d /home/$USERNAME/personal ]; then
  475. rm -rf /home/$USERNAME/personal
  476. fi
  477. mv /root/temppersonal/home/$USERNAME/personal /home/$USERNAME
  478. if [ ! "$?" = "0" ]; then
  479. exit 184
  480. fi
  481. rm -rf /root/temppersonal
  482. fi
  483. fi
  484. done
  485. }
  486. function restore_mailing_list {
  487. if [[ $RESTORE_APP != 'all' ]]; then
  488. if [[ $RESTORE_APP != 'mailinglist' ]]; then
  489. return
  490. fi
  491. fi
  492. if [ -d /var/spool/mlmmj ]; then
  493. echo $"Restoring public mailing list"
  494. restore_directory_from_friend /root/tempmailinglist mailinglist
  495. cp -r /root/tempmailinglist/root/spool/mlmmj/* /var/spool/mlmmj
  496. if [ ! "$?" = "0" ]; then
  497. exit 526
  498. fi
  499. rm -rf /root/tempmailinglist
  500. fi
  501. }
  502. function restore_xmpp {
  503. if [[ $RESTORE_APP != 'all' ]]; then
  504. if [[ $RESTORE_APP != 'xmpp' ]]; then
  505. return
  506. fi
  507. fi
  508. if [ -d /var/lib/prosody ]; then
  509. echo $"Restoring XMPP settings"
  510. restore_directory_from_friend /root/tempxmpp xmpp
  511. cp -r /root/tempxmpp/var/lib/prosody/* /var/lib/prosody
  512. if [ ! "$?" = "0" ]; then
  513. exit 725
  514. fi
  515. rm -rf /root/tempxmpp
  516. service prosody restart
  517. chown -R prosody:prosody /var/lib/prosody/*
  518. fi
  519. }
  520. function restore_gnu_social {
  521. if [[ $RESTORE_APP != 'all' ]]; then
  522. if [[ $RESTORE_APP != 'gnusocial' ]]; then
  523. return
  524. fi
  525. fi
  526. if grep -q "GNU Social domain" $COMPLETION_FILE; then
  527. MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
  528. restore_database_from_friend gnusocial ${MICROBLOG_DOMAIN_NAME}
  529. if [ -d /root/tempgnusocial ]; then
  530. rm -rf /root/tempgnusocial
  531. fi
  532. fi
  533. }
  534. function restore_hubzilla {
  535. if [[ $RESTORE_APP != 'all' ]]; then
  536. if [[ $RESTORE_APP != 'hubzilla' ]]; then
  537. return
  538. fi
  539. fi
  540. if grep -q "Hubzilla domain" $COMPLETION_FILE; then
  541. HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
  542. restore_database_from_friend hubzilla ${HUBZILLA_DOMAIN_NAME}
  543. if [ -d $SERVER_DIRECTORY/backup/hubzilla ]; then
  544. if [ ! -d /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3 ]; then
  545. mkdir -p /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  546. fi
  547. chmod 777 /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  548. chown -R www-data:www-data /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/*
  549. fi
  550. if [ -d /root/temphubzilla ]; then
  551. rm -rf /root/temphubzilla
  552. fi
  553. fi
  554. }
  555. function restore_owncloud {
  556. if [[ $RESTORE_APP != 'all' ]]; then
  557. if [[ $RESTORE_APP != 'owncloud' ]]; then
  558. return
  559. fi
  560. fi
  561. if grep -q "Owncloud domain" $COMPLETION_FILE; then
  562. OWNCLOUD_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Owncloud domain" | awk -F ':' '{print $2}')
  563. restore_database_from_friend owncloud $OWNCLOUD_DOMAIN_NAME
  564. if [ -d $SERVER_DIRECTORY/backup/owncloud ]; then
  565. echo $"Restoring Owncloud installation"
  566. cp -r /root/tempowncloud/var/lib/owncloud/* /var/lib/owncloud/
  567. if [ ! "$?" = "0" ]; then
  568. exit 981
  569. fi
  570. restore_directory_from_friend /root/tempowncloud2 owncloud2
  571. cp -r /root/tempowncloud2/etc/owncloud/* /etc/owncloud/
  572. if [ ! "$?" = "0" ]; then
  573. exit 982
  574. fi
  575. rm -rf /root/tempowncloud
  576. rm -rf /root/tempowncloud2
  577. chown -R www-data:www-data /var/lib/owncloud/data
  578. chown -R www-data:www-data /var/lib/owncloud/backup
  579. chown -R www-data:www-data /var/lib/owncloud/assets
  580. for d in /home/*/ ; do
  581. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  582. if [[ $USERNAME != "git" ]]; then
  583. occ files:scan $USERNAME
  584. fi
  585. done
  586. ln -s /usr/share/owncloud /var/www/${OWNCLOUD_DOMAIN_NAME}/htdocs
  587. fi
  588. fi
  589. }
  590. function restore_gogs {
  591. if [[ $RESTORE_APP != 'all' ]]; then
  592. if [[ $RESTORE_APP != 'gogs' ]]; then
  593. return
  594. fi
  595. fi
  596. if grep -q "Gogs domain" $COMPLETION_FILE; then
  597. GIT_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
  598. restore_database_from_friend gogs $GIT_DOMAIN_NAME
  599. if [ -d $SERVER_DIRECTORY/backup/gogs ]; then
  600. if [ ! -d /home/git/go/src/github.com/gogits/gogs/custom ]; then
  601. mkdir -p /home/git/go/src/github.com/gogits/gogs/custom
  602. fi
  603. cp -r /root/tempgogs/home/git/go/src/github.com/gogits/gogs/custom/* /home/git/go/src/github.com/gogits/gogs/custom/
  604. if [ ! "$?" = "0" ]; then
  605. exit 5885
  606. fi
  607. echo $"Restoring Gogs repos"
  608. restore_directory_from_friend /root/tempgogsrepos gogsrepos
  609. cp -r /root/tempgogsrepos/home/git/gogs-repositories/* /home/git/gogs-repositories/
  610. if [ ! "$?" = "0" ]; then
  611. exit 7649
  612. fi
  613. echo $"Restoring Gogs authorized_keys"
  614. restore_directory_from_friend /root/tempgogsssh gogsssh
  615. if [ ! -d /home/git/.ssh ]; then
  616. mkdir /home/git/.ssh
  617. fi
  618. cp -r /root/tempgogsssh/home/git/.ssh/* /home/git/.ssh/
  619. if [ ! "$?" = "0" ]; then
  620. exit 74239
  621. fi
  622. rm -rf /root/tempgogs
  623. rm -rf /root/tempgogsrepos
  624. rm -rf /root/tempgogsssh
  625. chown -R git:git /home/git
  626. fi
  627. fi
  628. }
  629. function restore_wiki {
  630. if [[ $RESTORE_APP != 'all' ]]; then
  631. if [[ $RESTORE_APP != 'wiki' ]]; then
  632. return
  633. fi
  634. fi
  635. if [ -d $SERVER_DIRECTORY/backup/wiki ]; then
  636. WIKI_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Wiki domain" | awk -F ':' '{print $2}')
  637. echo $"Restoring Wiki installation $WIKI_DOMAIN_NAME"
  638. restore_directory_from_friend /root/tempwiki wiki
  639. cp -r /root/tempwiki/var/lib/dokuwiki/* /var/lib/dokuwiki/
  640. if [ ! "$?" = "0" ]; then
  641. exit 868
  642. fi
  643. restore_directory_from_friend /root/tempwiki2 wiki2
  644. cp -r /root/tempwiki2/etc/dokuwiki/* /etc/dokuwiki/
  645. if [ ! "$?" = "0" ]; then
  646. exit 869
  647. fi
  648. rm -rf /root/tempwiki
  649. rm -rf /root/tempwiki2
  650. chown -R www-data:www-data /var/lib/dokuwiki/*
  651. # Ensure that the bundled SSL cert is being used
  652. if [ -f /etc/ssl/certs/${WIKI_DOMAIN_NAME}.bundle.crt ]; then
  653. sed -i "s|${WIKI_DOMAIN_NAME}.crt|${WIKI_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${WIKI_DOMAIN_NAME}
  654. fi
  655. if [ -d /etc/letsencrypt/live/${WIKI_DOMAIN_NAME} ]; then
  656. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${WIKI_DOMAIN_NAME}.key
  657. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${WIKI_DOMAIN_NAME}.pem
  658. fi
  659. fi
  660. }
  661. function restore_blog {
  662. if [[ $RESTORE_APP != 'all' ]]; then
  663. if [[ $RESTORE_APP != 'blog' ]]; then
  664. return
  665. fi
  666. fi
  667. if [ -d $SERVER_DIRECTORY/backup/blog ]; then
  668. FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
  669. echo $"Restoring blog installation $FULLBLOG_DOMAIN_NAME"
  670. mkdir /root/tempblog
  671. restore_directory_from_friend /root/tempblog blog
  672. rm -rf /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs
  673. cp -r /root/tempblog/var/www/${FULLBLOG_DOMAIN_NAME}/htdocs /var/www/${FULLBLOG_DOMAIN_NAME}/
  674. if [ ! "$?" = "0" ]; then
  675. exit 593
  676. fi
  677. rm -rf /root/tempblog
  678. if [ ! -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content ]; then
  679. echo $"No content directory found after restoring blog"
  680. exit 287
  681. fi
  682. # Ensure that the bundled SSL cert is being used
  683. if [ -f /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.bundle.crt ]; then
  684. sed -i "s|${FULLBLOG_DOMAIN_NAME}.crt|${FULLBLOG_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${FULLBLOG_DOMAIN_NAME}
  685. fi
  686. for d in /home/*/ ; do
  687. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  688. if [[ $USERNAME != "git" ]]; then
  689. if [ -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post ]; then
  690. mv /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/*.md /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post
  691. fi
  692. done
  693. if [ -d /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME} ]; then
  694. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${FULLBLOG_DOMAIN_NAME}.key
  695. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.pem
  696. fi
  697. fi
  698. }
  699. function restore_cjdns {
  700. if [[ $RESTORE_APP != 'all' ]]; then
  701. if [[ $RESTORE_APP != 'cjdns' ]]; then
  702. return
  703. fi
  704. fi
  705. if [ -d $SERVER_DIRECTORY/backup/cjdns ]; then
  706. echo $"Restoring cjdns installation"
  707. restore_directory_from_friend /root/tempcjdns cjdns
  708. rm -rf /etc/cjdns
  709. cp -r /root/tempcjdns/etc/cjdns /etc/
  710. if [ ! "$?" = "0" ]; then
  711. exit 7438
  712. fi
  713. rm -rf /root/tempcjdns
  714. fi
  715. }
  716. function restore_voip {
  717. if [[ $RESTORE_APP != 'all' ]]; then
  718. if [[ $RESTORE_APP != 'voip' ]]; then
  719. return
  720. fi
  721. fi
  722. if [ -d $SERVER_DIRECTORY/backup/voip ]; then
  723. echo $"Restoring VoIP settings"
  724. restore_directory_from_friend /root/tempvoip voip
  725. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.ini /etc/
  726. if [ ! "$?" = "0" ]; then
  727. rm -rf /root/tempvoip
  728. exit 7823
  729. fi
  730. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/sipwitch.conf /etc/sipwitch.conf
  731. if [ ! "$?" = "0" ]; then
  732. rm -rf /root/tempvoip
  733. exit 7823
  734. fi
  735. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.sqlite /var/lib/mumble-server/
  736. if [ ! "$?" = "0" ]; then
  737. rm -rf /root/tempvoip
  738. exit 276
  739. fi
  740. rm -rf /root/tempvoip
  741. cp /etc/ssl/certs/mumble* /var/lib/mumble-server
  742. cp /etc/ssl/private/mumble* /var/lib/mumble-server
  743. chown -R mumble-server:mumble-server /var/lib/mumble-server
  744. service sipwitch restart
  745. service mumble-server restart
  746. fi
  747. }
  748. function restore_tox {
  749. if [[ $RESTORE_APP != 'all' ]]; then
  750. if [[ $RESTORE_APP != 'tox' ]]; then
  751. return
  752. fi
  753. fi
  754. if [ -d $SERVER_DIRECTORY/backup/tox ]; then
  755. echo $"Restoring Tox node settings"
  756. restore_directory_from_friend / tox
  757. if [ ! "$?" = "0" ]; then
  758. exit 93653
  759. fi
  760. cp /var/lib/tox-bootstrapd/tox-bootstrapd.conf /etc/tox-bootstrapd.conf
  761. systemctl restart tox-bootstrapd.service
  762. if [ ! "$?" = "0" ]; then
  763. systemctl status tox-bootstrapd.service
  764. exit 59369
  765. fi
  766. fi
  767. }
  768. function restore_email {
  769. if [[ $RESTORE_APP != 'all' ]]; then
  770. if [[ $RESTORE_APP != 'email' ]]; then
  771. return
  772. fi
  773. fi
  774. for d in $SERVER_DIRECTORY/backup/mail/*/ ; do
  775. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  776. if [[ $USERNAME != "git" ]]; then
  777. if [ -d $SERVER_DIRECTORY/backup/mail/$USERNAME ]; then
  778. if [ ! -d /home/$USERNAME ]; then
  779. ${PROJECT_NAME}-adduser $USERNAME
  780. fi
  781. echo $"Restoring emails for $USERNAME"
  782. restore_directory_from_friend /root/tempmail mail/$USERNAME
  783. if [ ! -d /home/$USERNAME/Maildir ]; then
  784. mkdir /home/$USERNAME/Maildir
  785. fi
  786. tar -xzvf /root/tempmail/root/tempbackupemail/$USERNAME/maildir.tar.gz -C /
  787. if [ ! "$?" = "0" ]; then
  788. exit 927
  789. fi
  790. rm -rf /root/tempmail
  791. fi
  792. fi
  793. done
  794. }
  795. function restore_dlna {
  796. if [[ $RESTORE_APP != 'all' ]]; then
  797. if [[ $RESTORE_APP != 'dlna' ]]; then
  798. return
  799. fi
  800. fi
  801. if [ -d /var/cache/minidlna ]; then
  802. if [ -d $SERVER_DIRECTORY/backup/dlna ]; then
  803. echo $"Restoring DLNA cache"
  804. restore_directory_from_friend /root/tempdlna dlna
  805. cp -r /root/tempdlna/var/cache/minidlna/* /var/cache/minidlna/
  806. if [ ! "$?" = "0" ]; then
  807. exit 982
  808. fi
  809. rm -rf /root/tempdlna
  810. fi
  811. fi
  812. }
  813. # Social key management
  814. # Recover any key fragments and reconstruct the gpg key
  815. ${PROJECT_NAME}-recoverkey -u ${ADMIN_USERNAME} -l $BACKUP_LIST
  816. copy_gpg_keys
  817. restore_configuration
  818. restore_mariadb
  819. restore_letsencrypt
  820. restore_mutt_settings
  821. restore_gpg
  822. restore_procmail
  823. restore_spamassassin
  824. restore_admin_readme
  825. restore_ipfs
  826. restore_ssh_keys
  827. restore_user_config
  828. restore_certs
  829. restore_personal_settings
  830. restore_mailing_list
  831. restore_xmpp
  832. restore_gnu_social
  833. restore_hubzilla
  834. restore_owncloud
  835. restore_gogs
  836. restore_wiki
  837. restore_blog
  838. restore_cjdns
  839. restore_voip
  840. restore_tox
  841. restore_email
  842. restore_dlna
  843. echo $"*** Remote restore was successful ***"
  844. exit 0