freedombone-restore-remote 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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 Affero 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 Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero 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. # special handling of ttrss
  109. if [[ ${2} == "ttrss" ]]; then
  110. if [ -d /etc/share/tt-rss ]; then
  111. rm -rf /etc/share/tt-rss
  112. mv /root/temp${1}/etc/share/tt-rss /etc/share/
  113. if [ ! "$?" = "0" ]; then
  114. exit 639
  115. fi
  116. if [ -d /etc/letsencrypt/live/${2} ]; then
  117. ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
  118. ln -s /etc/letsencrypt/live/${2}/fullchain.pem /etc/ssl/certs/${2}.pem
  119. else
  120. # Ensure that the bundled SSL cert is being used
  121. if [ -f /etc/ssl/certs/${2}.bundle.crt ]; then
  122. sed -i "s|${2}.crt|${2}.bundle.crt|g" /etc/nginx/sites-available/${2}
  123. fi
  124. fi
  125. fi
  126. fi
  127. if [ -d /var/www/${2}/htdocs ]; then
  128. if [ -d /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs ]; then
  129. rm -rf /var/www/${2}/htdocs
  130. mv /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs /var/www/${2}/
  131. if [ ! "$?" = "0" ]; then
  132. exit 683
  133. fi
  134. if [ -d /etc/letsencrypt/live/${2} ]; then
  135. ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
  136. ln -s /etc/letsencrypt/live/${2}/fullchain.pem /etc/ssl/certs/${2}.pem
  137. else
  138. # Ensure that the bundled SSL cert is being used
  139. if [ -f /etc/ssl/certs/${2}.bundle.crt ]; then
  140. sed -i "s|${2}.crt|${2}.bundle.crt|g" /etc/nginx/sites-available/${2}
  141. fi
  142. fi
  143. fi
  144. fi
  145. fi
  146. fi
  147. }
  148. function restore_configuration {
  149. if [[ $RESTORE_APP != 'all' ]]; then
  150. if [[ $RESTORE_APP != 'configuration' ]]; then
  151. return
  152. fi
  153. fi
  154. if [ -d $SERVER_DIRECTORY/backup/config ]; then
  155. echo $"Restoring configuration files"
  156. restore_directory_from_friend /root/tempconfig config
  157. cp -f /root/tempconfig/root/${PROJECT_NAME}.cfg $CONFIG_FILE
  158. if [ ! "$?" = "0" ]; then
  159. unmount_drive
  160. rm -rf /root/tempconfig
  161. exit 5372
  162. fi
  163. if [ -f $CONFIG_FILE ]; then
  164. # install according to the config file
  165. freedombone -c $CONFIG_FILE
  166. fi
  167. cp -f /root/tempconfig/root/${PROJECT_NAME}-completed.txt $COMPLETION_FILE
  168. if [ ! "$?" = "0" ]; then
  169. unmount_drive
  170. rm -rf /root/tempconfig
  171. exit 7252
  172. fi
  173. if [ -f /root/tempconfig${BACKUP_EXTRA_DIRECTORIES} ]; then
  174. cp -f /root/tempconfig${BACKUP_EXTRA_DIRECTORIES} ${BACKUP_EXTRA_DIRECTORIES}
  175. if [ ! "$?" = "0" ]; then
  176. unmount_drive
  177. rm -rf /root/tempconfig
  178. exit 62121
  179. fi
  180. fi
  181. # restore nginx password hashes
  182. if [ -f /root/tempconfig/root/htpasswd ]; then
  183. cp -f /root/tempconfig/root/htpasswd /etc/nginx/.htpasswd
  184. fi
  185. rm -rf /root/tempconfig
  186. fi
  187. }
  188. function restore_mariadb {
  189. if [[ $RESTORE_APP != 'all' ]]; then
  190. if [[ $RESTORE_APP != 'mariadb' ]]; then
  191. return
  192. fi
  193. fi
  194. if [ -d $SERVER_DIRECTORY/backup/mariadb ]; then
  195. echo $"Restoring MariaDB settings"
  196. restore_directory_from_friend /root/tempmariadb mariadb
  197. echo $"Get the MariaDB password from the backup"
  198. if [ ! -f /root/tempmariadb/root/tempmariadb/db ]; then
  199. echo $"MariaDB password file not found"
  200. exit 495
  201. fi
  202. BACKUP_MARIADB_PASSWORD=$(cat /root/tempmariadb/root/tempmariadb/db)
  203. if [[ "$BACKUP_MARIADB_PASSWORD" != "$DATABASE_PASSWORD" ]]; then
  204. echo $"Restore the MariaDB user table"
  205. mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" mysql -o < /root/tempmariadb/root/tempmariadb/mysql.sql)
  206. if [ ! "$?" = "0" ]; then
  207. echo $"Try again using the password obtained from backup"
  208. mysqlsuccess=$(mysql -u root --password="$BACKUP_MARIADB_PASSWORD" mysql -o < /root/tempmariadb/root/tempmariadb/mysql.sql)
  209. fi
  210. if [ ! "$?" = "0" ]; then
  211. echo "$mysqlsuccess"
  212. exit 962
  213. fi
  214. echo $"Restarting database"
  215. service mysql restart
  216. echo $"Change the MariaDB password to the backup version"
  217. DATABASE_PASSWORD=$BACKUP_MARIADB_PASSWORD
  218. fi
  219. shred -zu /root/tempmariadb/root/tempmariadb/db
  220. rm -rf /root/tempmariadb
  221. # Change database password file
  222. echo "$DATABASE_PASSWORD" > /root/dbpass
  223. chmod 600 /root/dbpass
  224. fi
  225. }
  226. function restore_letsencrypt {
  227. if [[ $RESTORE_APP != 'all' ]]; then
  228. if [[ $RESTORE_APP != 'letsencrypt' ]]; then
  229. return
  230. fi
  231. fi
  232. if [ -d $SERVER_DIRECTORY/backup/letsencrypt ]; then
  233. echo $"Restoring Lets Encrypt settings"
  234. restore_directory_from_friend / letsencrypt
  235. fi
  236. }
  237. function restore_tor {
  238. if [[ $RESTORE_APP != 'all' ]]; then
  239. if [[ $RESTORE_APP != 'tor' ]]; then
  240. return
  241. fi
  242. fi
  243. if [ -d $SERVER_DIRECTORY/backup/tor ]; then
  244. echo $"Restoring Tor settings"
  245. restore_directory_from_friend / tor
  246. fi
  247. }
  248. function restore_mutt_settings {
  249. if [[ $RESTORE_APP != 'all' ]]; then
  250. if [[ $RESTORE_APP != 'mutt' ]]; then
  251. return
  252. fi
  253. fi
  254. for d in $SERVER_DIRECTORY/backup/mutt/*/ ; do
  255. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  256. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  257. if [ -d $SERVER_DIRECTORY/backup/mutt/$USERNAME ]; then
  258. if [ ! -d /home/$USERNAME ]; then
  259. ${PROJECT_NAME}-adduser $USERNAME
  260. fi
  261. echo $"Restoring Mutt settings for $USERNAME"
  262. restore_directory_from_friend /root/tempmutt mutt/$USERNAME
  263. if [ -f /root/tempmutt/home/$USERNAME/tempbackup/.muttrc ]; then
  264. cp -f /root/tempmutt/home/$USERNAME/tempbackup/.muttrc /home/$USERNAME/.muttrc
  265. fi
  266. if [ -f /root/tempmutt/home/$USERNAME/tempbackup/Muttrc ]; then
  267. cp -f /root/tempmutt/home/$USERNAME/tempbackup/Muttrc /etc/Muttrc
  268. fi
  269. if [ ! "$?" = "0" ]; then
  270. rm -rf /root/tempmutt
  271. exit 276
  272. fi
  273. rm -rf /root/tempmutt
  274. fi
  275. fi
  276. done
  277. }
  278. function restore_gpg {
  279. if [[ $RESTORE_APP != 'all' ]]; then
  280. if [[ $RESTORE_APP != 'gpg' ]]; then
  281. return
  282. fi
  283. fi
  284. for d in $SERVER_DIRECTORY/backup/gnupg/*/ ; do
  285. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  286. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  287. if [ -d $SERVER_DIRECTORY/backup/gnupg/$USERNAME ]; then
  288. if [ ! -d /home/$USERNAME ]; then
  289. ${PROJECT_NAME}-adduser $USERNAME
  290. fi
  291. echo $"Restoring gnupg settings for $USERNAME"
  292. restore_directory_from_friend /root/tempgnupg gnupg/$USERNAME
  293. cp -r /root/tempgnupg/home/$USERNAME/.gnupg /home/$USERNAME/
  294. if [ ! "$?" = "0" ]; then
  295. rm -rf /root/tempgnupg
  296. exit 276
  297. fi
  298. rm -rf /root/tempgnupg
  299. if [[ "$USERNAME" == "$ADMIN_USERNAME" ]]; then
  300. cp -r /home/$USERNAME/.gnupg /root
  301. if [ ! "$?" = "0" ]; then
  302. exit 283
  303. fi
  304. fi
  305. fi
  306. fi
  307. done
  308. }
  309. function restore_procmail {
  310. if [[ $RESTORE_APP != 'all' ]]; then
  311. if [[ $RESTORE_APP != 'procmail' ]]; then
  312. return
  313. fi
  314. fi
  315. for d in $SERVER_DIRECTORY/backup/procmail/*/ ; do
  316. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  317. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  318. if [ -d $SERVER_DIRECTORY/backup/procmail/$USERNAME ]; then
  319. if [ ! -d /home/$USERNAME ]; then
  320. ${PROJECT_NAME}-adduser $USERNAME
  321. fi
  322. echo $"Restoring procmail settings for $USERNAME"
  323. restore_directory_from_friend /root/tempprocmail procmail/$USERNAME
  324. cp -f /root/tempprocmail/home/$USERNAME/tempbackup/.procmailrc /home/$USERNAME/
  325. if [ ! "$?" = "0" ]; then
  326. rm -rf /root/tempprocmail
  327. exit 276
  328. fi
  329. rm -rf /root/tempprocmail
  330. fi
  331. fi
  332. done
  333. }
  334. function restore_spamassassin {
  335. if [[ $RESTORE_APP != 'all' ]]; then
  336. if [[ $RESTORE_APP != 'spamassassin' ]]; then
  337. return
  338. fi
  339. fi
  340. for d in $SERVER_DIRECTORY/backup/spamassassin/*/ ; do
  341. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  342. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  343. if [ -d $SERVER_DIRECTORY/backup/spamassassin/$USERNAME ]; then
  344. if [ ! -d /home/$USERNAME ]; then
  345. ${PROJECT_NAME}-adduser $USERNAME
  346. fi
  347. echo $"Restoring spamassassin settings for $USERNAME"
  348. restore_directory_from_friend /root/tempspamassassin spamassassin/$USERNAME
  349. cp -rf /root/tempspamassassin/home/$USERNAME/.spamassassin /home/$USERNAME/
  350. if [ ! "$?" = "0" ]; then
  351. rm -rf /root/tempspamassassin
  352. exit 276
  353. fi
  354. rm -rf /root/tempspamassassin
  355. fi
  356. fi
  357. done
  358. }
  359. function restore_admin_readme {
  360. if [[ $RESTORE_APP != 'all' ]]; then
  361. if [[ $RESTORE_APP != 'readme' ]]; then
  362. return
  363. fi
  364. fi
  365. if [ -d $SERVER_DIRECTORY/backup/readme ]; then
  366. echo $"Restoring README"
  367. restore_directory_from_friend /root/tempreadme readme
  368. cp -f /root/tempreadme/home/$ADMIN_USERNAME/tempbackup/README /home/$ADMIN_USERNAME/
  369. if [ ! "$?" = "0" ]; then
  370. rm -rf /root/tempreadme
  371. exit 276
  372. fi
  373. rm -rf /root/tempreadme
  374. fi
  375. }
  376. function restore_ipfs {
  377. if [[ $RESTORE_APP != 'all' ]]; then
  378. if [[ $RESTORE_APP != 'ipfs' ]]; then
  379. return
  380. fi
  381. fi
  382. if [ -d $SERVER_DIRECTORY/backup/ipfs ]; then
  383. echo $"Restoring IPFS"
  384. restore_directory_from_friend /root/tempipfs ipfs
  385. cp -rf /root/tempipfs/home/$ADMIN_USERNAME/.ipfs/* /home/$ADMIN_USERNAME/.ipfs
  386. if [ ! "$?" = "0" ]; then
  387. rm -rf /root/tempipfs
  388. exit 276
  389. fi
  390. rm -rf /root/tempipfs
  391. fi
  392. }
  393. function restore_ssh_keys {
  394. if [[ $RESTORE_APP != 'all' ]]; then
  395. if [[ $RESTORE_APP != 'ssh' ]]; then
  396. return
  397. fi
  398. fi
  399. for d in $SERVER_DIRECTORY/backup/ssh/*/ ; do
  400. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  401. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  402. if [ -d $SERVER_DIRECTORY/backup/ssh/$USERNAME ]; then
  403. if [ ! -d /home/$USERNAME ]; then
  404. ${PROJECT_NAME}-adduser $USERNAME
  405. fi
  406. echo $"Restoring ssh keys for $USERNAME"
  407. restore_directory_from_friend /root/tempssh ssh/$USERNAME
  408. cp -r /root/tempssh/home/$USERNAME/.ssh /home/$USERNAME/
  409. if [ ! "$?" = "0" ]; then
  410. rm -rf /root/tempssh
  411. exit 664
  412. fi
  413. rm -rf /root/tempssh
  414. fi
  415. fi
  416. done
  417. }
  418. function restore_user_config {
  419. if [[ $RESTORE_APP != 'all' ]]; then
  420. if [[ $RESTORE_APP != 'userconfig' ]]; then
  421. return
  422. fi
  423. fi
  424. for d in $SERVER_DIRECTORY/backup/config/*/ ; do
  425. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  426. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  427. if [ -d $SERVER_DIRECTORY/backup/config/$USERNAME ]; then
  428. if [ ! -d /home/$USERNAME ]; then
  429. ${PROJECT_NAME}-adduser $USERNAME
  430. fi
  431. echo $"Restoring config files for $USERNAME"
  432. restore_directory_from_friend /root/tempconfig config/$USERNAME
  433. cp -r /root/tempconfig/home/$USERNAME/.config /home/$USERNAME/
  434. if [ ! "$?" = "0" ]; then
  435. rm -rf /root/tempconfig
  436. exit 664
  437. fi
  438. rm -rf /root/tempconfig
  439. fi
  440. fi
  441. done
  442. }
  443. function restore_user_local {
  444. if [[ $RESTORE_APP != 'all' ]]; then
  445. if [[ $RESTORE_APP != 'userlocal' ]]; then
  446. return
  447. fi
  448. fi
  449. for d in $SERVER_DIRECTORY/backup/local/*/ ; do
  450. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  451. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  452. if [ -d $SERVER_DIRECTORY/backup/local/$USERNAME ]; then
  453. if [ ! -d /home/$USERNAME ]; then
  454. ${PROJECT_NAME}-adduser $USERNAME
  455. fi
  456. echo $"Restoring local files for $USERNAME"
  457. restore_directory_from_friend /root/templocal local/$USERNAME
  458. cp -r /root/templocal/home/$USERNAME/.local /home/$USERNAME/
  459. if [ ! "$?" = "0" ]; then
  460. rm -rf /root/templocal
  461. exit 664
  462. fi
  463. rm -rf /root/templocal
  464. fi
  465. fi
  466. done
  467. }
  468. function restore_certs {
  469. if [[ $RESTORE_APP != 'all' ]]; then
  470. if [[ $RESTORE_APP != 'certs' ]]; then
  471. return
  472. fi
  473. fi
  474. if [ -d $SERVER_DIRECTORY/backup/ssl ]; then
  475. echo $"Restoring certificates"
  476. restore_directory_from_friend /root/tempssl ssl
  477. cp -r /root/tempssl/etc/ssl/* /etc/ssl
  478. if [ ! "$?" = "0" ]; then
  479. exit 276
  480. fi
  481. rm -rf /root/tempssl
  482. # restore ownership
  483. if [ -f /etc/ssl/private/xmpp.key ]; then
  484. chown prosody:prosody /etc/ssl/private/xmpp.key
  485. chown prosody:prosody /etc/ssl/certs/xmpp.*
  486. fi
  487. if [ -d /etc/dovecot ]; then
  488. chown root:dovecot /etc/ssl/private/dovecot.*
  489. chown root:dovecot /etc/ssl/certs/dovecot.*
  490. fi
  491. if [ -f /etc/ssl/private/exim.key ]; then
  492. cp /etc/ssl/private/exim.key /etc/exim4
  493. cp /etc/ssl/certs/exim.crt /etc/exim4
  494. cp /etc/ssl/certs/exim.dhparam /etc/exim4
  495. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  496. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  497. fi
  498. if [ -f /etc/ssl/private/mumble.key ]; then
  499. if [ -d /var/lib/mumble-server ]; then
  500. cp /etc/ssl/certs/mumble.* /var/lib/mumble-server
  501. cp /etc/ssl/private/mumble.key /var/lib/mumble-server
  502. chown -R mumble-server:mumble-server /var/lib/mumble-server
  503. fi
  504. fi
  505. fi
  506. }
  507. function restore_personal_settings {
  508. if [[ $RESTORE_APP != 'all' ]]; then
  509. if [[ $RESTORE_APP != 'personal' ]]; then
  510. return
  511. fi
  512. fi
  513. for d in $SERVER_DIRECTORY/backup/personal/*/ ; do
  514. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  515. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  516. if [ -d $SERVER_DIRECTORY/backup/personal/$USERNAME ]; then
  517. if [ ! -d /home/$USERNAME ]; then
  518. ${PROJECT_NAME}-adduser $USERNAME
  519. fi
  520. echo $"Restoring personal settings for $USERNAME"
  521. restore_directory_from_friend /root/temppersonal personal/$USERNAME
  522. if [ -d /home/$USERNAME/personal ]; then
  523. rm -rf /home/$USERNAME/personal
  524. fi
  525. mv /root/temppersonal/home/$USERNAME/personal /home/$USERNAME
  526. if [ ! "$?" = "0" ]; then
  527. exit 184
  528. fi
  529. rm -rf /root/temppersonal
  530. fi
  531. fi
  532. done
  533. }
  534. function restore_mailing_list {
  535. if [[ $RESTORE_APP != 'all' ]]; then
  536. if [[ $RESTORE_APP != 'mailinglist' ]]; then
  537. return
  538. fi
  539. fi
  540. if [ -d /var/spool/mlmmj ]; then
  541. echo $"Restoring public mailing list"
  542. restore_directory_from_friend /root/tempmailinglist mailinglist
  543. cp -r /root/tempmailinglist/root/spool/mlmmj/* /var/spool/mlmmj
  544. if [ ! "$?" = "0" ]; then
  545. exit 526
  546. fi
  547. rm -rf /root/tempmailinglist
  548. fi
  549. }
  550. function restore_xmpp {
  551. if [[ $RESTORE_APP != 'all' ]]; then
  552. if [[ $RESTORE_APP != 'xmpp' ]]; then
  553. return
  554. fi
  555. fi
  556. if [ -d /var/lib/prosody ]; then
  557. echo $"Restoring XMPP settings"
  558. restore_directory_from_friend /root/tempxmpp xmpp
  559. cp -r /root/tempxmpp/var/lib/prosody/* /var/lib/prosody
  560. if [ ! "$?" = "0" ]; then
  561. exit 725
  562. fi
  563. rm -rf /root/tempxmpp
  564. service prosody restart
  565. chown -R prosody:prosody /var/lib/prosody/*
  566. fi
  567. }
  568. function restore_gnu_social {
  569. if [[ $RESTORE_APP != 'all' ]]; then
  570. if [[ $RESTORE_APP != 'gnusocial' ]]; then
  571. return
  572. fi
  573. fi
  574. if grep -q "GNU Social domain" $COMPLETION_FILE; then
  575. MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
  576. restore_database_from_friend gnusocial ${MICROBLOG_DOMAIN_NAME}
  577. if [ -d /root/tempgnusocial ]; then
  578. rm -rf /root/tempgnusocial
  579. fi
  580. fi
  581. }
  582. function restore_rss_reader {
  583. if [[ $RESTORE_APP != 'all' ]]; then
  584. if [[ $RESTORE_APP != 'ttrss' ]]; then
  585. return
  586. fi
  587. fi
  588. if grep -q "RSS reader domain" $COMPLETION_FILE; then
  589. RSS_READER_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "RSS reader domain" | awk -F ':' '{print $2}')
  590. restore_database_from_friend ttrss ${RSS_READER_DOMAIN_NAME}
  591. if [ -d $SERVER_DIRECTORY/backup/ttrss ]; then
  592. chown -R www-data:www-data /etc/share/tt-rss
  593. fi
  594. if [ -d /root/tempttrss ]; then
  595. rm -rf /root/tempttrss
  596. fi
  597. fi
  598. }
  599. function restore_hubzilla {
  600. if [[ $RESTORE_APP != 'all' ]]; then
  601. if [[ $RESTORE_APP != 'hubzilla' ]]; then
  602. return
  603. fi
  604. fi
  605. if grep -q "Hubzilla domain" $COMPLETION_FILE; then
  606. HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
  607. restore_database_from_friend hubzilla ${HUBZILLA_DOMAIN_NAME}
  608. if [ -d $SERVER_DIRECTORY/backup/hubzilla ]; then
  609. if [ ! -d /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3 ]; then
  610. mkdir -p /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  611. fi
  612. chmod 777 /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  613. chown -R www-data:www-data /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/*
  614. fi
  615. if [ -d /root/temphubzilla ]; then
  616. rm -rf /root/temphubzilla
  617. fi
  618. fi
  619. }
  620. function restore_owncloud {
  621. if [[ $RESTORE_APP != 'all' ]]; then
  622. if [[ $RESTORE_APP != 'owncloud' ]]; then
  623. return
  624. fi
  625. fi
  626. if grep -q "Owncloud domain" $COMPLETION_FILE; then
  627. OWNCLOUD_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Owncloud domain" | awk -F ':' '{print $2}')
  628. restore_database_from_friend owncloud $OWNCLOUD_DOMAIN_NAME
  629. if [ -d $SERVER_DIRECTORY/backup/owncloud ]; then
  630. echo $"Restoring Owncloud installation"
  631. cp -r /root/tempowncloud/var/lib/owncloud/* /var/lib/owncloud/
  632. if [ ! "$?" = "0" ]; then
  633. exit 981
  634. fi
  635. restore_directory_from_friend /root/tempowncloud2 owncloud2
  636. cp -r /root/tempowncloud2/etc/owncloud/* /etc/owncloud/
  637. if [ ! "$?" = "0" ]; then
  638. exit 982
  639. fi
  640. rm -rf /root/tempowncloud
  641. rm -rf /root/tempowncloud2
  642. chown -R www-data:www-data /var/lib/owncloud/data
  643. chown -R www-data:www-data /var/lib/owncloud/backup
  644. chown -R www-data:www-data /var/lib/owncloud/assets
  645. for d in /home/*/ ; do
  646. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  647. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  648. occ files:scan $USERNAME
  649. fi
  650. done
  651. ln -s /usr/share/owncloud /var/www/${OWNCLOUD_DOMAIN_NAME}/htdocs
  652. fi
  653. fi
  654. }
  655. function restore_gogs {
  656. if [[ $RESTORE_APP != 'all' ]]; then
  657. if [[ $RESTORE_APP != 'gogs' ]]; then
  658. return
  659. fi
  660. fi
  661. if grep -q "Gogs domain" $COMPLETION_FILE; then
  662. GIT_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
  663. restore_database_from_friend gogs $GIT_DOMAIN_NAME
  664. if [ -d $SERVER_DIRECTORY/backup/gogs ]; then
  665. if [ ! -d /home/git/go/src/github.com/gogits/gogs/custom ]; then
  666. mkdir -p /home/git/go/src/github.com/gogits/gogs/custom
  667. fi
  668. cp -r /root/tempgogs/home/git/go/src/github.com/gogits/gogs/custom/* /home/git/go/src/github.com/gogits/gogs/custom/
  669. if [ ! "$?" = "0" ]; then
  670. exit 5885
  671. fi
  672. echo $"Restoring Gogs repos"
  673. restore_directory_from_friend /root/tempgogsrepos gogsrepos
  674. cp -r /root/tempgogsrepos/home/git/gogs-repositories/* /home/git/gogs-repositories/
  675. if [ ! "$?" = "0" ]; then
  676. exit 7649
  677. fi
  678. echo $"Restoring Gogs authorized_keys"
  679. restore_directory_from_friend /root/tempgogsssh gogsssh
  680. if [ ! -d /home/git/.ssh ]; then
  681. mkdir /home/git/.ssh
  682. fi
  683. cp -r /root/tempgogsssh/home/git/.ssh/* /home/git/.ssh/
  684. if [ ! "$?" = "0" ]; then
  685. exit 74239
  686. fi
  687. rm -rf /root/tempgogs
  688. rm -rf /root/tempgogsrepos
  689. rm -rf /root/tempgogsssh
  690. chown -R git:git /home/git
  691. fi
  692. fi
  693. }
  694. function restore_wiki {
  695. if [[ $RESTORE_APP != 'all' ]]; then
  696. if [[ $RESTORE_APP != 'wiki' ]]; then
  697. return
  698. fi
  699. fi
  700. if [ -d $SERVER_DIRECTORY/backup/wiki ]; then
  701. WIKI_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Wiki domain" | awk -F ':' '{print $2}')
  702. echo $"Restoring Wiki installation $WIKI_DOMAIN_NAME"
  703. restore_directory_from_friend /root/tempwiki wiki
  704. cp -r /root/tempwiki/var/lib/dokuwiki/* /var/lib/dokuwiki/
  705. if [ ! "$?" = "0" ]; then
  706. exit 868
  707. fi
  708. restore_directory_from_friend /root/tempwiki2 wiki2
  709. cp -r /root/tempwiki2/etc/dokuwiki/* /etc/dokuwiki/
  710. if [ ! "$?" = "0" ]; then
  711. exit 869
  712. fi
  713. rm -rf /root/tempwiki
  714. rm -rf /root/tempwiki2
  715. chown -R www-data:www-data /var/lib/dokuwiki/*
  716. # Ensure that the bundled SSL cert is being used
  717. if [ -f /etc/ssl/certs/${WIKI_DOMAIN_NAME}.bundle.crt ]; then
  718. sed -i "s|${WIKI_DOMAIN_NAME}.crt|${WIKI_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${WIKI_DOMAIN_NAME}
  719. fi
  720. if [ -d /etc/letsencrypt/live/${WIKI_DOMAIN_NAME} ]; then
  721. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${WIKI_DOMAIN_NAME}.key
  722. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${WIKI_DOMAIN_NAME}.pem
  723. fi
  724. fi
  725. }
  726. function restore_blog {
  727. if [[ $RESTORE_APP != 'all' ]]; then
  728. if [[ $RESTORE_APP != 'blog' ]]; then
  729. return
  730. fi
  731. fi
  732. if [ -d $SERVER_DIRECTORY/backup/blog ]; then
  733. FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
  734. echo $"Restoring blog installation $FULLBLOG_DOMAIN_NAME"
  735. mkdir /root/tempblog
  736. restore_directory_from_friend /root/tempblog blog
  737. rm -rf /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs
  738. cp -r /root/tempblog/var/www/${FULLBLOG_DOMAIN_NAME}/htdocs /var/www/${FULLBLOG_DOMAIN_NAME}/
  739. if [ ! "$?" = "0" ]; then
  740. exit 593
  741. fi
  742. rm -rf /root/tempblog
  743. if [ ! -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content ]; then
  744. echo $"No content directory found after restoring blog"
  745. exit 287
  746. fi
  747. # Ensure that the bundled SSL cert is being used
  748. if [ -f /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.bundle.crt ]; then
  749. sed -i "s|${FULLBLOG_DOMAIN_NAME}.crt|${FULLBLOG_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${FULLBLOG_DOMAIN_NAME}
  750. fi
  751. for d in /home/*/ ; do
  752. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  753. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  754. if [ -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post ]; then
  755. mv /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/*.md /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post
  756. fi
  757. done
  758. if [ -d /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME} ]; then
  759. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${FULLBLOG_DOMAIN_NAME}.key
  760. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.pem
  761. fi
  762. fi
  763. }
  764. function restore_cjdns {
  765. if [[ $RESTORE_APP != 'all' ]]; then
  766. if [[ $RESTORE_APP != 'cjdns' ]]; then
  767. return
  768. fi
  769. fi
  770. if [ -d $SERVER_DIRECTORY/backup/cjdns ]; then
  771. echo $"Restoring cjdns installation"
  772. restore_directory_from_friend /root/tempcjdns cjdns
  773. rm -rf /etc/cjdns
  774. cp -r /root/tempcjdns/etc/cjdns /etc/
  775. if [ ! "$?" = "0" ]; then
  776. exit 7438
  777. fi
  778. rm -rf /root/tempcjdns
  779. fi
  780. }
  781. function restore_voip {
  782. if [[ $RESTORE_APP != 'all' ]]; then
  783. if [[ $RESTORE_APP != 'voip' ]]; then
  784. return
  785. fi
  786. fi
  787. if [ -d $SERVER_DIRECTORY/backup/voip ]; then
  788. echo $"Restoring VoIP settings"
  789. restore_directory_from_friend /root/tempvoip voip
  790. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.ini /etc/
  791. if [ ! "$?" = "0" ]; then
  792. rm -rf /root/tempvoip
  793. exit 7823
  794. fi
  795. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/sipwitch.conf /etc/sipwitch.conf
  796. if [ ! "$?" = "0" ]; then
  797. rm -rf /root/tempvoip
  798. exit 7823
  799. fi
  800. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.sqlite /var/lib/mumble-server/
  801. if [ ! "$?" = "0" ]; then
  802. rm -rf /root/tempvoip
  803. exit 276
  804. fi
  805. rm -rf /root/tempvoip
  806. cp /etc/ssl/certs/mumble* /var/lib/mumble-server
  807. cp /etc/ssl/private/mumble* /var/lib/mumble-server
  808. chown -R mumble-server:mumble-server /var/lib/mumble-server
  809. service sipwitch restart
  810. service mumble-server restart
  811. fi
  812. }
  813. function restore_tox {
  814. if [[ $RESTORE_APP != 'all' ]]; then
  815. if [[ $RESTORE_APP != 'tox' ]]; then
  816. return
  817. fi
  818. fi
  819. if [ -d $SERVER_DIRECTORY/backup/tox ]; then
  820. echo $"Restoring Tox node settings"
  821. restore_directory_from_friend / tox
  822. if [ ! "$?" = "0" ]; then
  823. exit 93653
  824. fi
  825. cp /var/lib/tox-bootstrapd/tox-bootstrapd.conf /etc/tox-bootstrapd.conf
  826. systemctl restart tox-bootstrapd.service
  827. if [ ! "$?" = "0" ]; then
  828. systemctl status tox-bootstrapd.service
  829. exit 59369
  830. fi
  831. fi
  832. }
  833. function restore_email {
  834. if [[ $RESTORE_APP != 'all' ]]; then
  835. if [[ $RESTORE_APP != 'email' ]]; then
  836. return
  837. fi
  838. fi
  839. for d in $SERVER_DIRECTORY/backup/mail/*/ ; do
  840. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  841. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  842. if [ -d $SERVER_DIRECTORY/backup/mail/$USERNAME ]; then
  843. if [ ! -d /home/$USERNAME ]; then
  844. ${PROJECT_NAME}-adduser $USERNAME
  845. fi
  846. echo $"Restoring emails for $USERNAME"
  847. restore_directory_from_friend /root/tempmail mail/$USERNAME
  848. if [ ! -d /home/$USERNAME/Maildir ]; then
  849. mkdir /home/$USERNAME/Maildir
  850. fi
  851. tar -xzvf /root/tempmail/root/tempbackupemail/$USERNAME/maildir.tar.gz -C /
  852. if [ ! "$?" = "0" ]; then
  853. exit 927
  854. fi
  855. rm -rf /root/tempmail
  856. fi
  857. fi
  858. done
  859. }
  860. function restore_dlna {
  861. if [[ $RESTORE_APP != 'all' ]]; then
  862. if [[ $RESTORE_APP != 'dlna' ]]; then
  863. return
  864. fi
  865. fi
  866. if [ -d /var/cache/minidlna ]; then
  867. if [ -d $SERVER_DIRECTORY/backup/dlna ]; then
  868. echo $"Restoring DLNA cache"
  869. restore_directory_from_friend /root/tempdlna dlna
  870. cp -r /root/tempdlna/var/cache/minidlna/* /var/cache/minidlna/
  871. if [ ! "$?" = "0" ]; then
  872. exit 982
  873. fi
  874. rm -rf /root/tempdlna
  875. fi
  876. fi
  877. }
  878. # Social key management
  879. # Recover any key fragments and reconstruct the gpg key
  880. ${PROJECT_NAME}-recoverkey -u ${ADMIN_USERNAME} -l $BACKUP_LIST
  881. copy_gpg_keys
  882. restore_configuration
  883. restore_mariadb
  884. restore_letsencrypt
  885. restore_mutt_settings
  886. restore_gpg
  887. restore_procmail
  888. restore_spamassassin
  889. restore_admin_readme
  890. restore_ipfs
  891. restore_ssh_keys
  892. restore_user_config
  893. restore_user_local
  894. restore_certs
  895. restore_personal_settings
  896. restore_mailing_list
  897. restore_xmpp
  898. restore_gnu_social
  899. restore_hubzilla
  900. restore_rss_reader
  901. restore_owncloud
  902. restore_gogs
  903. restore_wiki
  904. restore_blog
  905. restore_cjdns
  906. restore_voip
  907. restore_tox
  908. restore_email
  909. restore_dlna
  910. echo $"*** Remote restore was successful ***"
  911. exit 0