freedombone-restore-local 45KB

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