freedombone-restore-local 36KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Restore from local storage - typically a USB drive
  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. BACKUP_EXTRA_DIRECTORIES=/root/backup-extra-dirs.csv
  32. # whether to restore everything or just a specific application
  33. RESTORE_APP='all'
  34. export TEXTDOMAIN=${PROJECT_NAME}-restore-local
  35. export TEXTDOMAINDIR="/usr/share/locale"
  36. USB_DRIVE=/dev/sdb1
  37. USB_MOUNT=/mnt/usb
  38. # get default USB from config file
  39. CONFIG_FILE=$HOME/${PROJECT_NAME}.cfg
  40. if [ -f $CONFIG_FILE ]; then
  41. if grep -q "USB_DRIVE=" $CONFIG_FILE; then
  42. USB_DRIVE=$(cat $CONFIG_FILE | grep "USB_DRIVE=" | awk -F '=' '{print $2}')
  43. fi
  44. fi
  45. # get the version of Go being used
  46. if [ -f /usr/local/bin/${PROJECT_NAME} ]; then
  47. GO_VERSION=$(cat /usr/local/bin/${PROJECT_NAME} | grep 'GO_VERSION=' | head -n 1 | awk -F '=' '{print $2}')
  48. GVM_HOME=$(cat /usr/local/bin/${PROJECT_NAME} | grep 'GVM_HOME=' | head -n 1 | awk -F '=' '{print $2}')
  49. else
  50. GO_VERSION=$(cat /usr/bin/${PROJECT_NAME} | grep 'GO_VERSION=' | head -n 1 | awk -F '=' '{print $2}')
  51. GVM_HOME=$(cat /usr/bin/${PROJECT_NAME} | grep 'GVM_HOME=' | head -n 1 | awk -F '=' '{print $2}')
  52. fi
  53. ADMIN_USERNAME=
  54. ADMIN_NAME=
  55. # MariaDB password
  56. DATABASE_PASSWORD=$(cat /root/dbpass)
  57. MICROBLOG_DOMAIN_NAME=
  58. HUBZILLA_DOMAIN_NAME=
  59. MEDIAGOBLIN_DOMAIN_NAME=
  60. GIT_DOMAIN_NAME=
  61. WIKI_DOMAIN_NAME=
  62. FULLBLOG_DOMAIN_NAME=
  63. function mount_drive {
  64. if [ $1 ]; then
  65. USB_DRIVE=/dev/${1}1
  66. fi
  67. if [ $2 ]; then
  68. RESTORE_APP=$2
  69. fi
  70. # get the admin user
  71. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  72. if [ $3 ]; then
  73. ADMIN_USERNAME=$3
  74. fi
  75. ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
  76. # check that the backup destination is available
  77. if [ ! -b $USB_DRIVE ]; then
  78. echo $"Please attach a USB drive"
  79. exit 1
  80. fi
  81. # unmount if already mounted
  82. umount -f $USB_MOUNT
  83. if [ ! -d $USB_MOUNT ]; then
  84. mkdir $USB_MOUNT
  85. fi
  86. if [ -f /dev/mapper/encrypted_usb ]; then
  87. rm -rf /dev/mapper/encrypted_usb
  88. fi
  89. cryptsetup luksClose encrypted_usb
  90. # mount the encrypted backup drive
  91. cryptsetup luksOpen $USB_DRIVE encrypted_usb
  92. if [ "$?" = "0" ]; then
  93. USB_DRIVE=/dev/mapper/encrypted_usb
  94. fi
  95. mount $USB_DRIVE $USB_MOUNT
  96. if [ ! "$?" = "0" ]; then
  97. echo $"There was a problem mounting the USB drive to $USB_MOUNT"
  98. rm -rf $USB_MOUNT
  99. exit 2
  100. fi
  101. }
  102. function unmount_drive {
  103. sync
  104. umount $USB_MOUNT
  105. if [ ! "$?" = "0" ]; then
  106. echo $"Unable to unmount the drive. This means that the backup did not work"
  107. rm -rf $USB_MOUNT
  108. exit 9
  109. fi
  110. rm -rf $USB_MOUNT
  111. echo $"Setting permissions"
  112. for d in /home/*/ ; do
  113. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  114. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  115. chown -R $USERNAME:$USERNAME /home/$USERNAME
  116. fi
  117. done
  118. if [[ $USB_DRIVE == /dev/mapper/encrypted_usb ]]; then
  119. echo $"Unmount encrypted USB"
  120. cryptsetup luksClose encrypted_usb
  121. fi
  122. if [ -f /dev/mapper/encrypted_usb ]; then
  123. rm -rf /dev/mapper/encrypted_usb
  124. fi
  125. }
  126. function check_backup_exists {
  127. if [ ! -d $USB_MOUNT/backup ]; then
  128. echo $"No backup directory found on the USB drive."
  129. unmount_drive
  130. exit 2
  131. fi
  132. }
  133. function check_admin_user {
  134. echo $"Checking that admin user exists"
  135. if [ ! -d /home/$ADMIN_USERNAME ]; then
  136. echo $"Username $ADMIN_USERNAME not found. Reinstall ${PROJECT_NAME} with this username."
  137. unmount_drive
  138. exit 295
  139. fi
  140. }
  141. function copy_gpg_keys {
  142. echo $"Copying GPG keys from admin user to root"
  143. cp -r /home/$ADMIN_USERNAME/.gnupg /root
  144. }
  145. function restore_directory_from_usb {
  146. if [ ! -d ${1} ]; then
  147. mkdir ${1}
  148. fi
  149. obnam restore -r $USB_MOUNT/backup/${2} --to ${1}
  150. }
  151. function restore_database {
  152. RESTORE_SUBDIR="root"
  153. if [ -d $USB_MOUNT/backup/${1} ]; then
  154. echo $"Restoring ${1} database"
  155. restore_directory_from_usb "/root/temp${1}data" "${1}data"
  156. if [ ! -f /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql ]; then
  157. echo $"Unable to restore ${1} database"
  158. rm -rf /root/temp${1}data
  159. unmount_drive
  160. exit 503
  161. fi
  162. mysqlsuccess=$(mysql -u root --password=$DATABASE_PASSWORD ${1} -o < /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
  163. if [ ! "$?" = "0" ]; then
  164. echo "$mysqlsuccess"
  165. unmount_drive
  166. exit 964
  167. fi
  168. shred -zu /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/*
  169. rm -rf /root/temp${1}data
  170. echo $"Restoring ${1} installation"
  171. if [ ! -d /root/temp${1} ]; then
  172. mkdir /root/temp${1}
  173. fi
  174. restore_directory_from_usb "/root/temp${1}" "${1}"
  175. RESTORE_SUBDIR="var"
  176. if [ ${2} ]; then
  177. # special handline of ttrss
  178. if [[ ${2} == "ttrss" ]]; then
  179. if [ -d /etc/share/tt-rss ]; then
  180. if [ -d /root/temp${1}/etc/share/tt-rss ]; then
  181. rm -rf /etc/share/tt-rss
  182. mv /root/temp${1}/etc/share/tt-rss /etc/share/
  183. if [ ! "$?" = "0" ]; then
  184. unmount_drive
  185. exit 528
  186. fi
  187. if [ -d /etc/letsencrypt/live/${2} ]; then
  188. ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
  189. ln -s /etc/letsencrypt/live/${2}/fullchain.pem /etc/ssl/certs/${2}.pem
  190. else
  191. # Ensure that the bundled SSL cert is being used
  192. if [ -f /etc/ssl/certs/${2}.bundle.crt ]; then
  193. sed -i "s|${2}.crt|${2}.bundle.crt|g" /etc/nginx/sites-available/${2}
  194. fi
  195. fi
  196. fi
  197. fi
  198. fi
  199. if [ -d /var/www/${2}/htdocs ]; then
  200. if [ -d /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs ]; then
  201. rm -rf /var/www/${2}/htdocs
  202. mv /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs /var/www/${2}/
  203. if [ ! "$?" = "0" ]; then
  204. unmount_drive
  205. exit 683
  206. fi
  207. if [ -d /etc/letsencrypt/live/${2} ]; then
  208. ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
  209. ln -s /etc/letsencrypt/live/${2}/fullchain.pem /etc/ssl/certs/${2}.pem
  210. else
  211. # Ensure that the bundled SSL cert is being used
  212. if [ -f /etc/ssl/certs/${2}.bundle.crt ]; then
  213. sed -i "s|${2}.crt|${2}.bundle.crt|g" /etc/nginx/sites-available/${2}
  214. fi
  215. fi
  216. fi
  217. fi
  218. fi
  219. fi
  220. }
  221. function update_domains {
  222. if grep -q "RSS reader domain" $COMPLETION_FILE; then
  223. RSS_READER_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "RSS reader domain" | awk -F ':' '{print $2}')
  224. fi
  225. if grep -q "GNU Social domain" $COMPLETION_FILE; then
  226. MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
  227. fi
  228. if grep -q "Hubzilla domain" $COMPLETION_FILE; then
  229. HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
  230. fi
  231. if grep -q "Mediagoblin domain" $COMPLETION_FILE; then
  232. MEDIAGOBLIN_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Mediagoblin domain" | awk -F ':' '{print $2}')
  233. fi
  234. if grep -q "Gogs domain" $COMPLETION_FILE; then
  235. GIT_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
  236. fi
  237. if [ -d $USB_MOUNT/backup/wiki ]; then
  238. WIKI_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Wiki domain" | awk -F ':' '{print $2}')
  239. fi
  240. if [ -d $USB_MOUNT/backup/blog ]; then
  241. FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
  242. fi
  243. }
  244. function restore_configuration {
  245. if [[ $RESTORE_APP != 'all' ]]; then
  246. if [[ $RESTORE_APP != 'configuration' ]]; then
  247. return
  248. fi
  249. fi
  250. # this restores *.cfg and COMPLETION_FILE
  251. if [ -d $USB_MOUNT/backup/config ]; then
  252. echo $"Restoring configuration files"
  253. restore_directory_from_usb /root/tempconfig config
  254. cp -f /root/tempconfig/root/${PROJECT_NAME}.cfg $CONFIG_FILE
  255. if [ ! "$?" = "0" ]; then
  256. unmount_drive
  257. rm -rf /root/tempconfig
  258. exit 5294
  259. fi
  260. if [ -f $CONFIG_FILE ]; then
  261. # install according to the config file
  262. freedombone -c $CONFIG_FILE
  263. fi
  264. cp -f /root/tempconfig/root/${PROJECT_NAME}-completed.txt $COMPLETION_FILE
  265. if [ ! "$?" = "0" ]; then
  266. unmount_drive
  267. rm -rf /root/tempconfig
  268. exit 6382
  269. fi
  270. if [ -f /root/tempconfig${BACKUP_EXTRA_DIRECTORIES} ]; then
  271. cp -f /root/tempconfig${BACKUP_EXTRA_DIRECTORIES} ${BACKUP_EXTRA_DIRECTORIES}
  272. if [ ! "$?" = "0" ]; then
  273. unmount_drive
  274. rm -rf /root/tempconfig
  275. exit 62121
  276. fi
  277. fi
  278. # restore nginx password hashes
  279. if [ -f /root/tempconfig/root/htpasswd ]; then
  280. cp -f /root/tempconfig/root/htpasswd /etc/nginx/.htpasswd
  281. fi
  282. rm -rf /root/tempconfig
  283. fi
  284. }
  285. function same_admin_user {
  286. PREV_ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  287. if [[ "$PREV_ADMIN_USERNAME" != "$ADMIN_USERNAME" ]]; then
  288. echo $"The admin username has changed from $PREV_ADMIN_USERNAME to $ADMIN_USERNAME. To restore you will first need to install a new ${PROJECT_NAME} system with an initial admin user named $PREV_ADMIN_USERNAME"
  289. unmount_drive
  290. exit 73265
  291. fi
  292. }
  293. function restore_mariadb {
  294. if [[ $RESTORE_APP != 'all' ]]; then
  295. if [[ $RESTORE_APP != 'mariadb' ]]; then
  296. return
  297. fi
  298. fi
  299. if [ -d $USB_MOUNT/backup/mariadb ]; then
  300. echo $"Restoring mysql settings"
  301. restore_directory_from_usb /root/tempmariadb mariadb
  302. echo $"Get the MariaDB password from the backup"
  303. if [ ! -f /root/tempmariadb/root/tempmariadb/db ]; then
  304. echo $"MariaDB password file not found"
  305. exit 495
  306. fi
  307. BACKUP_MARIADB_PASSWORD=$(cat /root/tempmariadb/root/tempmariadb/db)
  308. if [[ $BACKUP_MARIADB_PASSWORD != $DATABASE_PASSWORD ]]; then
  309. echo $"Restore the MariaDB user table"
  310. mysqlsuccess=$(mysql -u root --password=$DATABASE_PASSWORD mysql -o < /root/tempmariadb/root/tempmariadb/mysql.sql)
  311. if [ ! "$?" = "0" ]; then
  312. echo $"Try again using the password obtained from backup"
  313. mysqlsuccess=$(mysql -u root --password=$BACKUP_MARIADB_PASSWORD mysql -o < /root/tempmariadb/root/tempmariadb/mysql.sql)
  314. fi
  315. if [ ! "$?" = "0" ]; then
  316. echo "$mysqlsuccess"
  317. unmount_drive
  318. exit 962
  319. fi
  320. echo $"Restarting database"
  321. service mysql restart
  322. echo $"Change the MariaDB password to the backup version"
  323. DATABASE_PASSWORD=$BACKUP_MARIADB_PASSWORD
  324. fi
  325. shred -zu /root/tempmariadb/root/tempmariadb/db
  326. rm -rf /root/tempmariadb
  327. # Change database password file
  328. echo "$DATABASE_PASSWORD" > /root/dbpass
  329. chmod 600 /root/dbpass
  330. fi
  331. }
  332. function restore_letsencrypt {
  333. if [[ $RESTORE_APP != 'all' ]]; then
  334. if [[ $RESTORE_APP != 'letsencrypt' ]]; then
  335. return
  336. fi
  337. fi
  338. if [ -d $USB_MOUNT/backup/letsencrypt ]; then
  339. echo $"Restoring Lets Encrypt settings"
  340. restore_directory_from_usb / letsencrypt
  341. fi
  342. }
  343. function restore_tor {
  344. if [[ $RESTORE_APP != 'all' ]]; then
  345. if [[ $RESTORE_APP != 'tor' ]]; then
  346. return
  347. fi
  348. fi
  349. if [ -d $USB_MOUNT/backup/tor ]; then
  350. echo $"Restoring Tor settings"
  351. restore_directory_from_usb / tor
  352. fi
  353. }
  354. function restore_mutt_settings {
  355. if [[ $RESTORE_APP != 'all' ]]; then
  356. if [[ $RESTORE_APP != 'mutt' ]]; then
  357. return
  358. fi
  359. fi
  360. if [ -d $USB_MOUNT/backup/mutt ]; then
  361. for d in $USB_MOUNT/backup/mutt/*/ ; do
  362. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  363. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  364. if [ ! -d /home/$USERNAME ]; then
  365. ${PROJECT_NAME}-adduser $USERNAME
  366. fi
  367. echo $"Restoring Mutt settings for $USERNAME"
  368. restore_directory_from_usb /root/tempmutt mutt/$USERNAME
  369. if [ -f /root/tempmutt/home/$USERNAME/tempbackup/.muttrc ]; then
  370. cp -f /root/tempmutt/home/$USERNAME/tempbackup/.muttrc /home/$USERNAME/.muttrc
  371. fi
  372. if [ -f /root/tempmutt/home/$USERNAME/tempbackup/Muttrc ]; then
  373. cp -f /root/tempmutt/home/$USERNAME/tempbackup/Muttrc /etc/Muttrc
  374. fi
  375. if [ ! "$?" = "0" ]; then
  376. rm -rf /root/tempmutt
  377. unmount_drive
  378. exit 276
  379. fi
  380. rm -rf /root/tempmutt
  381. fi
  382. done
  383. fi
  384. }
  385. function restore_gpg {
  386. if [[ $RESTORE_APP != 'all' ]]; then
  387. if [[ $RESTORE_APP != 'gpg' ]]; then
  388. return
  389. fi
  390. fi
  391. if [ -d $USB_MOUNT/backup/gnupg ]; then
  392. for d in $USB_MOUNT/backup/gnupg/*/ ; do
  393. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  394. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  395. if [ ! -d /home/$USERNAME ]; then
  396. ${PROJECT_NAME}-adduser $USERNAME
  397. fi
  398. echo $"Restoring gnupg settings for $USERNAME"
  399. restore_directory_from_usb /root/tempgnupg gnupg/$USERNAME
  400. cp -r /root/tempgnupg/home/$USERNAME/.gnupg /home/$USERNAME/
  401. if [ ! "$?" = "0" ]; then
  402. rm -rf /root/tempgnupg
  403. unmount_drive
  404. exit 276
  405. fi
  406. rm -rf /root/tempgnupg
  407. if [[ "$USERNAME" == "$ADMIN_USERNAME" ]]; then
  408. cp -r /home/$USERNAME/.gnupg /root
  409. if [ ! "$?" = "0" ]; then
  410. unmount_drive
  411. exit 283
  412. fi
  413. fi
  414. fi
  415. done
  416. fi
  417. }
  418. function restore_procmail {
  419. if [[ $RESTORE_APP != 'all' ]]; then
  420. if [[ $RESTORE_APP != 'procmail' ]]; then
  421. return
  422. fi
  423. fi
  424. if [ -d $USB_MOUNT/backup/procmail ]; then
  425. for d in $USB_MOUNT/backup/procmail/*/ ; do
  426. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  427. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  428. if [ ! -d /home/$USERNAME ]; then
  429. ${PROJECT_NAME}-adduser $USERNAME
  430. fi
  431. echo $"Restoring procmail settings for $USERNAME"
  432. restore_directory_from_usb /root/tempprocmail procmail/$USERNAME
  433. cp -f /root/tempprocmail/home/$USERNAME/tempbackup/.procmailrc /home/$USERNAME/
  434. if [ ! "$?" = "0" ]; then
  435. rm -rf /root/tempprocmail
  436. unmount_drive
  437. exit 276
  438. fi
  439. rm -rf /root/tempprocmail
  440. fi
  441. done
  442. fi
  443. }
  444. function restore_spamassassin {
  445. if [[ $RESTORE_APP != 'all' ]]; then
  446. if [[ $RESTORE_APP != 'spamassassin' ]]; then
  447. return
  448. fi
  449. fi
  450. if [ -d $USB_MOUNT/backup/spamassassin ]; then
  451. for d in $USB_MOUNT/backup/spamassassin/*/ ; do
  452. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  453. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  454. if [ -d $USB_MOUNT/backup/spamassassin/$USERNAME ]; then
  455. if [ ! -d /home/$USERNAME ]; then
  456. ${PROJECT_NAME}-adduser $USERNAME
  457. fi
  458. echo $"Restoring spamassassin settings for $USERNAME"
  459. restore_directory_from_usb /root/tempspamassassin spamassassin/$USERNAME
  460. cp -rf /root/tempspamassassin/home/$USERNAME/.spamassassin /home/$USERNAME/
  461. if [ ! "$?" = "0" ]; then
  462. rm -rf /root/tempspamassassin
  463. unmount_drive
  464. exit 276
  465. fi
  466. rm -rf /root/tempspamassassin
  467. fi
  468. fi
  469. done
  470. fi
  471. }
  472. function restore_admin_readme {
  473. if [[ $RESTORE_APP != 'all' ]]; then
  474. if [[ $RESTORE_APP != 'readme' ]]; then
  475. return
  476. fi
  477. fi
  478. if [ -d $USB_MOUNT/backup/readme ]; then
  479. echo $"Restoring admin user README"
  480. # Make a backup of the original README file
  481. # incase old passwords need to be used
  482. if [ -f /home/$ADMIN_USERNAME/README ]; then
  483. if [ ! -f /home/$ADMIN_USERNAME/README_original ]; then
  484. cp /home/$ADMIN_USERNAME/README /home/$ADMIN_USERNAME/README_original
  485. fi
  486. fi
  487. restore_directory_from_usb /root/tempreadme readme
  488. cp -f /root/tempreadme/home/$ADMIN_USERNAME/tempbackup/README /home/$ADMIN_USERNAME/
  489. if [ ! "$?" = "0" ]; then
  490. rm -rf /root/tempreadme
  491. unmount_drive
  492. exit 276
  493. fi
  494. rm -rf /root/tempreadme
  495. fi
  496. }
  497. function restore_ipfs {
  498. if [[ $RESTORE_APP != 'all' ]]; then
  499. if [[ $RESTORE_APP != 'ipfs' ]]; then
  500. return
  501. fi
  502. fi
  503. if [ -d $USB_MOUNT/backup/ipfs ]; then
  504. echo $"Restoring IPFS"
  505. restore_directory_from_usb /root/tempipfs ipfs
  506. cp -rf /root/tempipfs/home/$ADMIN_USERNAME/.ipfs/* /home/$ADMIN_USERNAME/.ipfs
  507. if [ ! "$?" = "0" ]; then
  508. rm -rf /root/tempipfs
  509. unmount_drive
  510. exit 276
  511. fi
  512. rm -rf /root/tempipfs
  513. fi
  514. }
  515. function restore_user_ssh_keys {
  516. if [[ $RESTORE_APP != 'all' ]]; then
  517. if [[ $RESTORE_APP != 'ssh' ]]; then
  518. return
  519. fi
  520. fi
  521. if [ -d $USB_MOUNT/backup/ssh ]; then
  522. for d in $USB_MOUNT/backup/ssh/*/ ; do
  523. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  524. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  525. if [ ! -d /home/$USERNAME ]; then
  526. ${PROJECT_NAME}-adduser $USERNAME
  527. fi
  528. echo $"Restoring ssh keys for $USERNAME"
  529. restore_directory_from_usb /root/tempssh ssh/$USERNAME
  530. cp -r /root/tempssh/home/$USERNAME/.ssh /home/$USERNAME/
  531. if [ ! "$?" = "0" ]; then
  532. rm -rf /root/tempssh
  533. unmount_drive
  534. exit 664
  535. fi
  536. rm -rf /root/tempssh
  537. fi
  538. done
  539. fi
  540. }
  541. function restore_user_config {
  542. if [[ $RESTORE_APP != 'all' ]]; then
  543. if [[ $RESTORE_APP != 'userconfig' ]]; then
  544. return
  545. fi
  546. fi
  547. if [ -d $USB_MOUNT/backup/config ]; then
  548. for d in $USB_MOUNT/backup/config/*/ ; do
  549. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  550. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  551. if [ ! -d /home/$USERNAME ]; then
  552. ${PROJECT_NAME}-adduser $USERNAME
  553. fi
  554. echo $"Restoring config files for $USERNAME"
  555. restore_directory_from_usb /root/tempconfig config/$USERNAME
  556. cp -r /root/tempconfig/home/$USERNAME/.config /home/$USERNAME/
  557. if [ ! "$?" = "0" ]; then
  558. rm -rf /root/tempconfig
  559. unmount_drive
  560. exit 664
  561. fi
  562. rm -rf /root/tempconfig
  563. fi
  564. done
  565. fi
  566. }
  567. function gpg_pubkey_from_email {
  568. key_owner_username=$1
  569. key_email_address=$2
  570. key_id=
  571. if [[ $key_owner_username != "root" ]]; then
  572. key_id=$(su -c "gpg --list-keys $key_email_address | grep 'pub '" - $key_owner_username | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  573. else
  574. key_id=$(gpg --list-keys $key_email_address | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  575. fi
  576. echo $key_id
  577. }
  578. function restore_user_monkeysphere {
  579. if [[ $RESTORE_APP != 'all' ]]; then
  580. if [[ $RESTORE_APP != 'usermonkeysphere' ]]; then
  581. return
  582. fi
  583. fi
  584. if [ -d $USB_MOUNT/backup/monkeysphere ]; then
  585. for d in $USB_MOUNT/backup/monkeysphere/*/ ; do
  586. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  587. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  588. if [ ! -d /home/$USERNAME ]; then
  589. ${PROJECT_NAME}-adduser $USERNAME
  590. fi
  591. echo $"Restoring monkeysphere ids for $USERNAME"
  592. restore_directory_from_usb /root/tempmonkeysphere monkeysphere/$USERNAME
  593. cp -r /root/tempmonkeysphere/home/$USERNAME/.monkeysphere /home/$USERNAME/
  594. if [ ! "$?" = "0" ]; then
  595. rm -rf /root/tempmonkeysphere
  596. unmount_drive
  597. exit 664
  598. fi
  599. rm -rf /root/tempmonkeysphere
  600. fi
  601. done
  602. # The admin user is the identity certifier
  603. MY_EMAIL_ADDRESS="${ADMIN_USERNAME}@${HOSTNAME}"
  604. if grep -q "MY_EMAIL_ADDRESS" $CONFIG_FILE; then
  605. MY_EMAIL_ADDRESS=$(grep "MY_EMAIL_ADDRESS" $CONFIG_FILE | awk -F '=' '{print $2}')
  606. fi
  607. MY_GPG_PUBLIC_KEY_ID=$(gpg_pubkey_from_email "$ADMIN_USERNAME" "$MY_EMAIL_ADDRESS")
  608. fpr=$(gpg --with-colons --fingerprint $MY_GPG_PUBLIC_KEY_ID | grep fpr | head -n 1 | awk -F ':' '{print $10}')
  609. monkeysphere-authentication add-identity-certifier $fpr
  610. monkeysphere-authentication update-users
  611. fi
  612. }
  613. function restore_user_fin {
  614. if [[ $RESTORE_APP != 'all' ]]; then
  615. if [[ $RESTORE_APP != 'userfin' ]]; then
  616. return
  617. fi
  618. fi
  619. if [ -d $USB_MOUNT/backup/fin ]; then
  620. for d in $USB_MOUNT/backup/fin/*/ ; do
  621. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  622. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  623. if [ ! -d /home/$USERNAME ]; then
  624. ${PROJECT_NAME}-adduser $USERNAME
  625. fi
  626. echo $"Restoring fin files for $USERNAME"
  627. restore_directory_from_usb /root/tempfin fin/$USERNAME
  628. cp -r /root/tempfin/home/$USERNAME/.fin /home/$USERNAME/
  629. if [ ! "$?" = "0" ]; then
  630. rm -rf /root/tempfin
  631. unmount_drive
  632. exit 664
  633. fi
  634. rm -rf /root/tempfin
  635. fi
  636. done
  637. fi
  638. }
  639. function restore_user_local {
  640. if [[ $RESTORE_APP != 'all' ]]; then
  641. if [[ $RESTORE_APP != 'userlocal' ]]; then
  642. return
  643. fi
  644. fi
  645. if [ -d $USB_MOUNT/backup/local ]; then
  646. for d in $USB_MOUNT/backup/local/*/ ; do
  647. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  648. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  649. if [ ! -d /home/$USERNAME ]; then
  650. ${PROJECT_NAME}-adduser $USERNAME
  651. fi
  652. echo $"Restoring local files for $USERNAME"
  653. restore_directory_from_usb /root/templocal local/$USERNAME
  654. cp -r /root/templocal/home/$USERNAME/.local /home/$USERNAME/
  655. if [ ! "$?" = "0" ]; then
  656. rm -rf /root/templocal
  657. unmount_drive
  658. exit 664
  659. fi
  660. rm -rf /root/templocal
  661. fi
  662. done
  663. fi
  664. }
  665. function restore_certs {
  666. if [[ $RESTORE_APP != 'all' ]]; then
  667. if [[ $RESTORE_APP != 'certs' ]]; then
  668. return
  669. fi
  670. fi
  671. if [ -d $USB_MOUNT/backup/ssl ]; then
  672. echo $"Restoring certificates"
  673. mkdir /root/tempssl
  674. restore_directory_from_usb /root/tempssl ssl
  675. cp -r /root/tempssl/etc/ssl/* /etc/ssl
  676. if [ ! "$?" = "0" ]; then
  677. unmount_drive
  678. exit 276
  679. fi
  680. rm -rf /root/tempssl
  681. # restore ownership
  682. if [ -f /etc/ssl/private/xmpp.key ]; then
  683. chown prosody:prosody /etc/ssl/private/xmpp.key
  684. chown prosody:prosody /etc/ssl/certs/xmpp.*
  685. fi
  686. if [ -d /etc/dovecot ]; then
  687. chown root:dovecot /etc/ssl/private/dovecot.*
  688. chown root:dovecot /etc/ssl/certs/dovecot.*
  689. fi
  690. if [ -f /etc/ssl/private/exim.key ]; then
  691. cp /etc/ssl/private/exim.key /etc/exim4
  692. cp /etc/ssl/certs/exim.crt /etc/exim4
  693. cp /etc/ssl/certs/exim.dhparam /etc/exim4
  694. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  695. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  696. fi
  697. if [ -f /etc/ssl/private/mumble.key ]; then
  698. if [ -d /var/lib/mumble-server ]; then
  699. cp /etc/ssl/certs/mumble.* /var/lib/mumble-server
  700. cp /etc/ssl/private/mumble.key /var/lib/mumble-server
  701. chown -R mumble-server:mumble-server /var/lib/mumble-server
  702. fi
  703. fi
  704. fi
  705. }
  706. function restore_personal_settings {
  707. if [[ $RESTORE_APP != 'all' ]]; then
  708. if [[ $RESTORE_APP != 'personal' ]]; then
  709. return
  710. fi
  711. fi
  712. if [ -d $USB_MOUNT/backup/personal ]; then
  713. for d in $USB_MOUNT/backup/personal/*/ ; do
  714. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  715. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  716. if [ -d $USB_MOUNT/backup/personal/$USERNAME ]; then
  717. if [ ! -d /home/$USERNAME ]; then
  718. ${PROJECT_NAME}-adduser $USERNAME
  719. fi
  720. echo $"Restoring personal settings for $USERNAME"
  721. restore_directory_from_usb /root/temppersonal personal/$USERNAME
  722. if [ -d /home/$USERNAME/personal ]; then
  723. rm -rf /home/$USERNAME/personal
  724. fi
  725. mv /root/temppersonal/home/$USERNAME/personal /home/$USERNAME
  726. if [ ! "$?" = "0" ]; then
  727. unmount_drive
  728. exit 184
  729. fi
  730. rm -rf /root/temppersonal
  731. fi
  732. fi
  733. done
  734. fi
  735. }
  736. function restore_mailing_list {
  737. if [[ $RESTORE_APP != 'all' ]]; then
  738. if [[ $RESTORE_APP != 'mailinglist' ]]; then
  739. return
  740. fi
  741. fi
  742. if [ -d /var/spool/mlmmj ]; then
  743. echo $"Restoring public mailing list"
  744. restore_directory_from_usb /root/tempmailinglist mailinglist
  745. cp -r /root/tempmailinglist/root/spool/mlmmj/* /var/spool/mlmmj
  746. if [ ! "$?" = "0" ]; then
  747. unmount_drive
  748. exit 526
  749. fi
  750. rm -rf /root/tempmailinglist
  751. fi
  752. }
  753. function restore_xmpp {
  754. if [[ $RESTORE_APP != 'all' ]]; then
  755. if [[ $RESTORE_APP != 'xmpp' ]]; then
  756. return
  757. fi
  758. fi
  759. if [ -d /var/lib/prosody ]; then
  760. echo $"Restoring XMPP settings"
  761. restore_directory_from_usb /root/tempxmpp xmpp
  762. cp -r /root/tempxmpp/var/lib/prosody/* /var/lib/prosody
  763. if [ ! "$?" = "0" ]; then
  764. unmount_drive
  765. exit 725
  766. fi
  767. rm -rf /root/tempxmpp
  768. service prosody restart
  769. chown -R prosody:prosody /var/lib/prosody/*
  770. fi
  771. }
  772. function restore_gnu_social {
  773. if [[ $RESTORE_APP != 'all' ]]; then
  774. if [[ $RESTORE_APP != 'gnusocial' ]]; then
  775. return
  776. fi
  777. fi
  778. if [ $MICROBLOG_DOMAIN_NAME ]; then
  779. restore_database gnusocial ${MICROBLOG_DOMAIN_NAME}
  780. if [ -d /root/tempgnusocial ]; then
  781. rm -rf /root/tempgnusocial
  782. fi
  783. fi
  784. }
  785. function restore_rss_reader {
  786. if [[ $RESTORE_APP != 'all' ]]; then
  787. if [[ $RESTORE_APP != 'ttrss' ]]; then
  788. return
  789. fi
  790. fi
  791. if [ $RSS_READER_DOMAIN_NAME ]; then
  792. restore_database ttrss ${RSS_READER_DOMAIN_NAME}
  793. if [ -d $USB_MOUNT/backup/ttrss ]; then
  794. chown -R www-data:www-data /etc/share/tt-rss
  795. if [ -d /root/tempttrss ]; then
  796. rm -rf /root/tempttrss
  797. fi
  798. fi
  799. fi
  800. }
  801. function restore_hubzilla {
  802. if [[ $RESTORE_APP != 'all' ]]; then
  803. if [[ $RESTORE_APP != 'hubzilla' ]]; then
  804. return
  805. fi
  806. fi
  807. if [ $HUBZILLA_DOMAIN_NAME ]; then
  808. restore_database hubzilla ${HUBZILLA_DOMAIN_NAME}
  809. if [ -d $USB_MOUNT/backup/hubzilla ]; then
  810. if [ ! -d /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3 ]; then
  811. mkdir -p /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  812. fi
  813. chmod 777 /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  814. chown -R www-data:www-data /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/*
  815. if [ -d /root/temphubzilla ]; then
  816. rm -rf /root/temphubzilla
  817. fi
  818. fi
  819. fi
  820. }
  821. function restore_syncthing {
  822. if [[ $RESTORE_APP != 'all' ]]; then
  823. if [[ $RESTORE_APP != 'syncthing' ]]; then
  824. return
  825. fi
  826. fi
  827. if [ -f /etc/systemd/system/syncthing.service ]; then
  828. systemctl stop syncthing
  829. systemctl stop cron
  830. fi
  831. if [ -d $USB_MOUNT/backup/syncthingconfig ]; then
  832. echo $"Restoring syncthing configuration"
  833. restore_directory_from_usb /root/tempsyncthingconfig syncthingconfig
  834. cp -r /root/tempsyncthingconfig/* /
  835. if [ ! "$?" = "0" ]; then
  836. unmount_drive
  837. systemctl start syncthing
  838. systemctl start cron
  839. exit 6833
  840. fi
  841. rm -rf /root/tempsyncthingconfig
  842. fi
  843. if [ -d $USB_MOUNT/backup/syncthingshared ]; then
  844. echo $"Restoring syncthing shared files"
  845. restore_directory_from_usb /root/tempsyncthingshared syncthingshared
  846. cp -r /root/tempsyncthingshared/* /
  847. if [ ! "$?" = "0" ]; then
  848. unmount_drive
  849. systemctl start syncthing
  850. systemctl start cron
  851. exit 37904
  852. fi
  853. rm -rf /root/tempsyncthingshared
  854. fi
  855. if [ -d $USB_MOUNT/backup/syncthing ]; then
  856. for d in $USB_MOUNT/backup/syncthing/*/ ; do
  857. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  858. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  859. if [ ! -d /home/$USERNAME ]; then
  860. ${PROJECT_NAME}-adduser $USERNAME
  861. fi
  862. echo $"Restoring syncthing files for $USERNAME"
  863. restore_directory_from_usb /root/tempsyncthing syncthing/$USERNAME
  864. cp -r /root/tempsyncthing/home/$USERNAME/Sync /home/$USERNAME/
  865. if [ ! "$?" = "0" ]; then
  866. rm -rf /root/tempsyncthing
  867. unmount_drive
  868. systemctl start syncthing
  869. systemctl start cron
  870. exit 68438
  871. fi
  872. rm -rf /root/tempsyncthing
  873. # restore device IDs from config settings
  874. if [ -f /home/$USERNAME/.config/syncthing/.syncthing-server-id ]; then
  875. cp /home/$USERNAME/.config/syncthing/.syncthing-server-id /home/$USERNAME/.syncthing-server-id
  876. chown $USERNAME:$USERNAME /home/$USERNAME/.syncthing-server-id
  877. fi
  878. if [ -f /home/$USERNAME/.config/syncthing/.syncthingids ]; then
  879. cp /home/$USERNAME/.config/syncthing/.syncthingids /home/$USERNAME/.syncthingids
  880. chown $USERNAME:$USERNAME /home/$USERNAME/.syncthingids
  881. fi
  882. fi
  883. done
  884. fi
  885. if [ -f /etc/systemd/system/syncthing.service ]; then
  886. systemctl start syncthing
  887. systemctl start cron
  888. fi
  889. }
  890. function restore_mediagoblin {
  891. if [[ $RESTORE_APP != 'all' ]]; then
  892. if [[ $RESTORE_APP != 'mediagoblin' ]]; then
  893. return
  894. fi
  895. fi
  896. if [ ! $MEDIAGOBLIN_DOMAIN_NAME ]; then
  897. return
  898. fi
  899. if [ -d $USB_MOUNT/backup/mediagoblin ]; then
  900. restore_directory_from_usb /root/tempmediagoblin mediagoblin
  901. cp -r /root/tempmediagoblin/* /
  902. if [ ! "$?" = "0" ]; then
  903. unmount_drive
  904. exit 67843
  905. fi
  906. rm -rf /root/tempmediagoblin
  907. chown -hR mediagoblin:www-data /var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs
  908. fi
  909. }
  910. function restore_gogs {
  911. export GVM_ROOT=$GVM_HOME
  912. if [ -d $GVM_ROOT/bin ]; then
  913. cd $GVM_ROOT/bin
  914. [[ -s "$GVM_ROOT/scripts/gvm" ]] && source "$GVM_ROOT/scripts/gvm"
  915. gvm use go${GO_VERSION} --default
  916. systemctl set-environment GOPATH=$GOPATH
  917. fi
  918. if [[ $RESTORE_APP != 'all' ]]; then
  919. if [[ $RESTORE_APP != 'gogs' ]]; then
  920. return
  921. fi
  922. fi
  923. if [ ${#GIT_DOMAIN_NAME} -gt 2 ]; then
  924. restore_database gogs ${GIT_DOMAIN_NAME}
  925. if [ -d $USB_MOUNT/backup/gogs ]; then
  926. echo $"Restoring Gogs settings"
  927. if [ ! -d $GOPATH/src/github.com/gogits/gogs/custom ]; then
  928. mkdir -p $GOPATH/src/github.com/gogits/gogs/custom
  929. fi
  930. cp -r /root/tempgogs/$GOPATH/src/github.com/gogits/gogs/custom/* $GOPATH/src/github.com/gogits/gogs/custom
  931. if [ ! "$?" = "0" ]; then
  932. unmount_drive
  933. exit 981
  934. fi
  935. echo $"Restoring Gogs repos"
  936. restore_directory_from_usb /root/tempgogsrepos gogsrepos
  937. cp -r /root/tempgogsrepos/home/git/gogs-repositories/* /home/git/gogs-repositories/
  938. if [ ! "$?" = "0" ]; then
  939. unmount_drive
  940. exit 67574
  941. fi
  942. echo $"Restoring Gogs authorized_keys"
  943. restore_directory_from_usb /root/tempgogsssh gogsssh
  944. if [ ! -d /home/git/.ssh ]; then
  945. mkdir /home/git/.ssh
  946. fi
  947. cp -r /root/tempgogsssh/home/git/.ssh/* /home/git/.ssh/
  948. if [ ! "$?" = "0" ]; then
  949. unmount_drive
  950. exit 8463
  951. fi
  952. rm -rf /root/tempgogs
  953. rm -rf /root/tempgogsrepos
  954. rm -rf /root/tempgogsssh
  955. chown -R git:git /home/git
  956. fi
  957. fi
  958. }
  959. function restore_wiki {
  960. if [[ $RESTORE_APP != 'all' ]]; then
  961. if [[ $RESTORE_APP != 'wiki' ]]; then
  962. return
  963. fi
  964. fi
  965. if [ $WIKI_DOMAIN_NAME ]; then
  966. echo $"Restoring Wiki installation ${WIKI_DOMAIN_NAME}"
  967. restore_directory_from_usb /root/tempwiki wiki
  968. cp -r /root/tempwiki/var/lib/dokuwiki/* /var/lib/dokuwiki/
  969. if [ ! "$?" = "0" ]; then
  970. unmount_drive
  971. exit 868
  972. fi
  973. restore_directory_from_usb /root/tempwiki2 wiki2
  974. cp -r /root/tempwiki2/etc/dokuwiki/* /etc/dokuwiki/
  975. if [ ! "$?" = "0" ]; then
  976. unmount_drive
  977. exit 869
  978. fi
  979. rm -rf /root/tempwiki
  980. rm -rf /root/tempwiki2
  981. chown -R www-data:www-data /var/lib/dokuwiki/*
  982. # Ensure that the bundled SSL cert is being used
  983. if [ -f /etc/ssl/certs/${WIKI_DOMAIN_NAME}.bundle.crt ]; then
  984. sed -i "s|${WIKI_DOMAIN_NAME}.crt|${WIKI_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${WIKI_DOMAIN_NAME}
  985. fi
  986. if [ -d /etc/letsencrypt/live/${WIKI_DOMAIN_NAME} ]; then
  987. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${WIKI_DOMAIN_NAME}.key
  988. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${WIKI_DOMAIN_NAME}.pem
  989. fi
  990. fi
  991. }
  992. function restore_blog {
  993. if [[ $RESTORE_APP != 'all' ]]; then
  994. if [[ $RESTORE_APP != 'blog' ]]; then
  995. return
  996. fi
  997. fi
  998. if [ $FULLBLOG_DOMAIN_NAME ]; then
  999. echo $"Restoring blog installation"
  1000. restore_directory_from_usb /root/tempblog blog
  1001. rm -rf /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs
  1002. cp -r /root/tempblog/var/www/${FULLBLOG_DOMAIN_NAME}/htdocs /var/www/${FULLBLOG_DOMAIN_NAME}/
  1003. if [ ! "$?" = "0" ]; then
  1004. unmount_drive
  1005. exit 593
  1006. fi
  1007. rm -rf /root/tempblog
  1008. if [ ! -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content ]; then
  1009. echo $"No content directory found after restoring blog"
  1010. unmount_drive
  1011. exit 287
  1012. fi
  1013. chown -R www-data:www-data /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs
  1014. # Ensure that the bundled SSL cert is being used
  1015. if [ -f /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.bundle.crt ]; then
  1016. sed -i "s|${FULLBLOG_DOMAIN_NAME}.crt|${FULLBLOG_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${FULLBLOG_DOMAIN_NAME}
  1017. fi
  1018. for d in /home/*/ ; do
  1019. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  1020. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  1021. if [ -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post ]; then
  1022. mv /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/*.md /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post
  1023. fi
  1024. fi
  1025. done
  1026. if [ -d /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME} ]; then
  1027. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${FULLBLOG_DOMAIN_NAME}.key
  1028. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.pem
  1029. fi
  1030. fi
  1031. }
  1032. function restore_cjdns {
  1033. if [[ $RESTORE_APP != 'all' ]]; then
  1034. if [[ $RESTORE_APP != 'cjdns' ]]; then
  1035. return
  1036. fi
  1037. fi
  1038. if [ -d $USB_MOUNT/backup/cjdns ]; then
  1039. echo $"Restoring cjdns installation"
  1040. restore_directory_from_usb /root/tempcjdns cjdns
  1041. rm -rf /etc/cjdns
  1042. cp -r /root/tempcjdns/etc/cjdns /etc/
  1043. if [ ! "$?" = "0" ]; then
  1044. unmount_drive
  1045. exit 8472
  1046. fi
  1047. rm -rf /root/tempcjdns
  1048. fi
  1049. }
  1050. function restore_email {
  1051. if [[ $RESTORE_APP != 'all' ]]; then
  1052. if [[ $RESTORE_APP != 'email' ]]; then
  1053. return
  1054. fi
  1055. fi
  1056. if [ -d $USB_MOUNT/backup/mail ]; then
  1057. for d in $USB_MOUNT/backup/mail/*/ ; do
  1058. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  1059. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  1060. if [ ! -d /home/$USERNAME ]; then
  1061. ${PROJECT_NAME}-adduser $USERNAME
  1062. fi
  1063. echo $"Restoring emails for $USERNAME"
  1064. restore_directory_from_usb /root/tempmail mail/$USERNAME
  1065. if [ ! -d /home/$USERNAME/Maildir ]; then
  1066. mkdir /home/$USERNAME/Maildir
  1067. fi
  1068. tar -xzvf /root/tempmail/root/tempbackupemail/$USERNAME/maildir.tar.gz -C /
  1069. if [ ! "$?" = "0" ]; then
  1070. unmount_drive
  1071. exit 927
  1072. fi
  1073. rm -rf /root/tempmail
  1074. fi
  1075. done
  1076. fi
  1077. }
  1078. function restore_dlna {
  1079. if [[ $RESTORE_APP != 'all' ]]; then
  1080. if [[ $RESTORE_APP != 'dlna' ]]; then
  1081. return
  1082. fi
  1083. fi
  1084. if [ -d /var/cache/minidlna ]; then
  1085. if [ -d $USB_MOUNT/backup/dlna ]; then
  1086. echo $"Restoring DLNA cache"
  1087. restore_directory_from_usb /root/tempdlna dlna
  1088. cp -r /root/tempdlna/var/cache/minidlna/* /var/cache/minidlna/
  1089. if [ ! "$?" = "0" ]; then
  1090. rm -rf /root/tempdlna
  1091. unmount_drive
  1092. exit 982
  1093. fi
  1094. rm -rf /root/tempdlna
  1095. fi
  1096. fi
  1097. }
  1098. function restore_voip {
  1099. if [[ $RESTORE_APP != 'all' ]]; then
  1100. if [[ $RESTORE_APP != 'voip' ]]; then
  1101. return
  1102. fi
  1103. fi
  1104. if [ -d $USB_MOUNT/backup/voip ]; then
  1105. echo $"Restoring VoIP settings"
  1106. restore_directory_from_usb /root/tempvoip voip
  1107. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.ini /etc/
  1108. if [ ! "$?" = "0" ]; then
  1109. rm -rf /root/tempvoip
  1110. unmount_drive
  1111. exit 3679
  1112. fi
  1113. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/sipwitch.conf /etc/sipwitch.conf
  1114. if [ ! "$?" = "0" ]; then
  1115. rm -rf /root/tempvoip
  1116. unmount_drive
  1117. exit 3679
  1118. fi
  1119. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.sqlite /var/lib/mumble-server/
  1120. if [ ! "$?" = "0" ]; then
  1121. rm -rf /root/tempvoip
  1122. unmount_drive
  1123. exit 276
  1124. fi
  1125. rm -rf /root/tempvoip
  1126. cp /etc/ssl/certs/mumble* /var/lib/mumble-server
  1127. cp /etc/ssl/private/mumble* /var/lib/mumble-server
  1128. chown -R mumble-server:mumble-server /var/lib/mumble-server
  1129. service sipwitch restart
  1130. service mumble-server restart
  1131. fi
  1132. }
  1133. function restore_tox {
  1134. if [[ $RESTORE_APP != 'all' ]]; then
  1135. if [[ $RESTORE_APP != 'tox' ]]; then
  1136. return
  1137. fi
  1138. fi
  1139. if [ -d $USB_MOUNT/backup/tox ]; then
  1140. echo $"Restoring Tox node settings"
  1141. restore_directory_from_usb / tox
  1142. if [ ! "$?" = "0" ]; then
  1143. unmount_drive
  1144. exit 6393
  1145. fi
  1146. cp /var/lib/tox-bootstrapd/tox-bootstrapd.conf /etc/tox-bootstrapd.conf
  1147. systemctl restart tox-bootstrapd.service
  1148. if [ ! "$?" = "0" ]; then
  1149. systemctl status tox-bootstrapd.service
  1150. unmount_drive
  1151. exit 59369
  1152. fi
  1153. fi
  1154. }
  1155. function get_restore_app {
  1156. if [ ${1} ]; then
  1157. if [ ! -d /home/${1} ]; then
  1158. RESTORE_APP=${1}
  1159. echo $"Restore $RESTORE_APP"
  1160. fi
  1161. fi
  1162. }
  1163. get_restore_app ${2}
  1164. mount_drive ${1} ${2}
  1165. check_backup_exists
  1166. check_admin_user
  1167. copy_gpg_keys
  1168. restore_configuration
  1169. same_admin_user
  1170. update_domains
  1171. restore_mariadb
  1172. restore_letsencrypt
  1173. restore_tor
  1174. restore_mutt_settings
  1175. restore_gpg
  1176. restore_procmail
  1177. restore_spamassassin
  1178. restore_admin_readme
  1179. restore_ipfs
  1180. restore_user_ssh_keys
  1181. restore_user_config
  1182. restore_user_monkeysphere
  1183. restore_user_fin
  1184. restore_user_local
  1185. restore_certs
  1186. restore_personal_settings
  1187. restore_mailing_list
  1188. restore_xmpp
  1189. restore_gnu_social
  1190. restore_hubzilla
  1191. restore_rss_reader
  1192. restore_syncthing
  1193. restore_mediagoblin
  1194. restore_gogs
  1195. restore_wiki
  1196. restore_blog
  1197. restore_cjdns
  1198. restore_email
  1199. restore_dlna
  1200. restore_voip
  1201. restore_tox
  1202. unmount_drive
  1203. echo $"Restore from USB drive is complete. You can now unplug it."
  1204. exit 0