freedombone-restore-local 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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}-utils-go | grep 'GO_VERSION=' | head -n 1 | awk -F '=' '{print $2}')
  48. GVM_HOME=$(cat /usr/local/bin/${PROJECT_NAME}-utils-go | grep 'GVM_HOME=' | head -n 1 | awk -F '=' '{print $2}')
  49. else
  50. GO_VERSION=$(cat /usr/bin/${PROJECT_NAME}-utils-go | grep 'GO_VERSION=' | head -n 1 | awk -F '=' '{print $2}')
  51. GVM_HOME=$(cat /usr/bin/${PROJECT_NAME}-utils-go | 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. # stop the daemons
  780. cd /var/www/${MICROBLOG_DOMAIN_NAME}/htdocs
  781. scripts/stopdaemons.sh
  782. restore_database gnusocial ${MICROBLOG_DOMAIN_NAME}
  783. if [ -d /root/tempgnusocial ]; then
  784. rm -rf /root/tempgnusocial
  785. fi
  786. # start the daemons
  787. cd /var/www/${MICROBLOG_DOMAIN_NAME}/htdocs
  788. scripts/startdaemons.sh
  789. fi
  790. }
  791. function restore_rss_reader {
  792. if [[ $RESTORE_APP != 'all' ]]; then
  793. if [[ $RESTORE_APP != 'ttrss' ]]; then
  794. return
  795. fi
  796. fi
  797. if [ $RSS_READER_DOMAIN_NAME ]; then
  798. restore_database ttrss ${RSS_READER_DOMAIN_NAME}
  799. if [ -d $USB_MOUNT/backup/ttrss ]; then
  800. chown -R www-data:www-data /etc/share/tt-rss
  801. if [ -d /root/tempttrss ]; then
  802. rm -rf /root/tempttrss
  803. fi
  804. fi
  805. fi
  806. }
  807. function restore_hubzilla {
  808. if [[ $RESTORE_APP != 'all' ]]; then
  809. if [[ $RESTORE_APP != 'hubzilla' ]]; then
  810. return
  811. fi
  812. fi
  813. if [ $HUBZILLA_DOMAIN_NAME ]; then
  814. restore_database hubzilla ${HUBZILLA_DOMAIN_NAME}
  815. if [ -d $USB_MOUNT/backup/hubzilla ]; then
  816. if [ ! -d /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3 ]; then
  817. mkdir -p /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  818. fi
  819. chmod 777 /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  820. chown -R www-data:www-data /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/*
  821. if [ -d /root/temphubzilla ]; then
  822. rm -rf /root/temphubzilla
  823. fi
  824. fi
  825. fi
  826. }
  827. function restore_syncthing {
  828. if [[ $RESTORE_APP != 'all' ]]; then
  829. if [[ $RESTORE_APP != 'syncthing' ]]; then
  830. return
  831. fi
  832. fi
  833. if [ -f /etc/systemd/system/syncthing.service ]; then
  834. systemctl stop syncthing
  835. systemctl stop cron
  836. fi
  837. if [ -d $USB_MOUNT/backup/syncthingconfig ]; then
  838. echo $"Restoring syncthing configuration"
  839. restore_directory_from_usb /root/tempsyncthingconfig syncthingconfig
  840. cp -r /root/tempsyncthingconfig/* /
  841. if [ ! "$?" = "0" ]; then
  842. unmount_drive
  843. systemctl start syncthing
  844. systemctl start cron
  845. exit 6833
  846. fi
  847. rm -rf /root/tempsyncthingconfig
  848. fi
  849. if [ -d $USB_MOUNT/backup/syncthingshared ]; then
  850. echo $"Restoring syncthing shared files"
  851. restore_directory_from_usb /root/tempsyncthingshared syncthingshared
  852. cp -r /root/tempsyncthingshared/* /
  853. if [ ! "$?" = "0" ]; then
  854. unmount_drive
  855. systemctl start syncthing
  856. systemctl start cron
  857. exit 37904
  858. fi
  859. rm -rf /root/tempsyncthingshared
  860. fi
  861. if [ -d $USB_MOUNT/backup/syncthing ]; then
  862. for d in $USB_MOUNT/backup/syncthing/*/ ; do
  863. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  864. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  865. if [ ! -d /home/$USERNAME ]; then
  866. ${PROJECT_NAME}-adduser $USERNAME
  867. fi
  868. echo $"Restoring syncthing files for $USERNAME"
  869. restore_directory_from_usb /root/tempsyncthing syncthing/$USERNAME
  870. cp -r /root/tempsyncthing/home/$USERNAME/Sync /home/$USERNAME/
  871. if [ ! "$?" = "0" ]; then
  872. rm -rf /root/tempsyncthing
  873. unmount_drive
  874. systemctl start syncthing
  875. systemctl start cron
  876. exit 68438
  877. fi
  878. rm -rf /root/tempsyncthing
  879. # restore device IDs from config settings
  880. if [ -f /home/$USERNAME/.config/syncthing/.syncthing-server-id ]; then
  881. cp /home/$USERNAME/.config/syncthing/.syncthing-server-id /home/$USERNAME/.syncthing-server-id
  882. chown $USERNAME:$USERNAME /home/$USERNAME/.syncthing-server-id
  883. fi
  884. if [ -f /home/$USERNAME/.config/syncthing/.syncthingids ]; then
  885. cp /home/$USERNAME/.config/syncthing/.syncthingids /home/$USERNAME/.syncthingids
  886. chown $USERNAME:$USERNAME /home/$USERNAME/.syncthingids
  887. fi
  888. fi
  889. done
  890. fi
  891. if [ -f /etc/systemd/system/syncthing.service ]; then
  892. systemctl start syncthing
  893. systemctl start cron
  894. fi
  895. }
  896. function restore_mediagoblin {
  897. if [[ $RESTORE_APP != 'all' ]]; then
  898. if [[ $RESTORE_APP != 'mediagoblin' ]]; then
  899. return
  900. fi
  901. fi
  902. if [ ! $MEDIAGOBLIN_DOMAIN_NAME ]; then
  903. return
  904. fi
  905. if [ -d $USB_MOUNT/backup/mediagoblin ]; then
  906. restore_directory_from_usb /root/tempmediagoblin mediagoblin
  907. cp -r /root/tempmediagoblin/* /
  908. if [ ! "$?" = "0" ]; then
  909. 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. unmount_drive
  939. exit 981
  940. fi
  941. echo $"Restoring Gogs repos"
  942. restore_directory_from_usb /root/tempgogsrepos gogsrepos
  943. cp -r /root/tempgogsrepos/home/git/gogs-repositories/* /home/git/gogs-repositories/
  944. if [ ! "$?" = "0" ]; then
  945. unmount_drive
  946. exit 67574
  947. fi
  948. echo $"Restoring Gogs authorized_keys"
  949. restore_directory_from_usb /root/tempgogsssh gogsssh
  950. if [ ! -d /home/git/.ssh ]; then
  951. mkdir /home/git/.ssh
  952. fi
  953. cp -r /root/tempgogsssh/home/git/.ssh/* /home/git/.ssh/
  954. if [ ! "$?" = "0" ]; then
  955. unmount_drive
  956. exit 8463
  957. fi
  958. rm -rf /root/tempgogs
  959. rm -rf /root/tempgogsrepos
  960. rm -rf /root/tempgogsssh
  961. chown -R git:git /home/git
  962. fi
  963. fi
  964. }
  965. function restore_wiki {
  966. if [[ $RESTORE_APP != 'all' ]]; then
  967. if [[ $RESTORE_APP != 'wiki' ]]; then
  968. return
  969. fi
  970. fi
  971. if [ $WIKI_DOMAIN_NAME ]; then
  972. echo $"Restoring Wiki installation ${WIKI_DOMAIN_NAME}"
  973. restore_directory_from_usb /root/tempwiki wiki
  974. cp -r /root/tempwiki/var/lib/dokuwiki/* /var/lib/dokuwiki/
  975. if [ ! "$?" = "0" ]; then
  976. unmount_drive
  977. exit 868
  978. fi
  979. restore_directory_from_usb /root/tempwiki2 wiki2
  980. cp -r /root/tempwiki2/etc/dokuwiki/* /etc/dokuwiki/
  981. if [ ! "$?" = "0" ]; then
  982. unmount_drive
  983. exit 869
  984. fi
  985. rm -rf /root/tempwiki
  986. rm -rf /root/tempwiki2
  987. chown -R www-data:www-data /var/lib/dokuwiki/*
  988. # Ensure that the bundled SSL cert is being used
  989. if [ -f /etc/ssl/certs/${WIKI_DOMAIN_NAME}.bundle.crt ]; then
  990. sed -i "s|${WIKI_DOMAIN_NAME}.crt|${WIKI_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${WIKI_DOMAIN_NAME}
  991. fi
  992. if [ -d /etc/letsencrypt/live/${WIKI_DOMAIN_NAME} ]; then
  993. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${WIKI_DOMAIN_NAME}.key
  994. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${WIKI_DOMAIN_NAME}.pem
  995. fi
  996. fi
  997. }
  998. function restore_blog {
  999. if [[ $RESTORE_APP != 'all' ]]; then
  1000. if [[ $RESTORE_APP != 'blog' ]]; then
  1001. return
  1002. fi
  1003. fi
  1004. if [ $FULLBLOG_DOMAIN_NAME ]; then
  1005. echo $"Restoring blog installation"
  1006. restore_directory_from_usb /root/tempblog blog
  1007. rm -rf /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs
  1008. cp -r /root/tempblog/var/www/${FULLBLOG_DOMAIN_NAME}/htdocs /var/www/${FULLBLOG_DOMAIN_NAME}/
  1009. if [ ! "$?" = "0" ]; then
  1010. unmount_drive
  1011. exit 593
  1012. fi
  1013. rm -rf /root/tempblog
  1014. if [ ! -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content ]; then
  1015. echo $"No content directory found after restoring blog"
  1016. unmount_drive
  1017. exit 287
  1018. fi
  1019. chown -R www-data:www-data /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs
  1020. # Ensure that the bundled SSL cert is being used
  1021. if [ -f /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.bundle.crt ]; then
  1022. sed -i "s|${FULLBLOG_DOMAIN_NAME}.crt|${FULLBLOG_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${FULLBLOG_DOMAIN_NAME}
  1023. fi
  1024. for d in /home/*/ ; do
  1025. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  1026. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  1027. if [ -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post ]; then
  1028. mv /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/*.md /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post
  1029. fi
  1030. fi
  1031. done
  1032. if [ -d /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME} ]; then
  1033. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${FULLBLOG_DOMAIN_NAME}.key
  1034. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.pem
  1035. fi
  1036. fi
  1037. }
  1038. function restore_cjdns {
  1039. if [[ $RESTORE_APP != 'all' ]]; then
  1040. if [[ $RESTORE_APP != 'cjdns' ]]; then
  1041. return
  1042. fi
  1043. fi
  1044. if [ -d $USB_MOUNT/backup/cjdns ]; then
  1045. echo $"Restoring cjdns installation"
  1046. restore_directory_from_usb /root/tempcjdns cjdns
  1047. rm -rf /etc/cjdns
  1048. cp -r /root/tempcjdns/etc/cjdns /etc/
  1049. if [ ! "$?" = "0" ]; then
  1050. unmount_drive
  1051. exit 8472
  1052. fi
  1053. rm -rf /root/tempcjdns
  1054. fi
  1055. }
  1056. function restore_email {
  1057. if [[ $RESTORE_APP != 'all' ]]; then
  1058. if [[ $RESTORE_APP != 'email' ]]; then
  1059. return
  1060. fi
  1061. fi
  1062. if [ -d $USB_MOUNT/backup/mail ]; then
  1063. for d in $USB_MOUNT/backup/mail/*/ ; do
  1064. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  1065. if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
  1066. if [ ! -d /home/$USERNAME ]; then
  1067. ${PROJECT_NAME}-adduser $USERNAME
  1068. fi
  1069. echo $"Restoring emails for $USERNAME"
  1070. restore_directory_from_usb /root/tempmail mail/$USERNAME
  1071. if [ ! -d /home/$USERNAME/Maildir ]; then
  1072. mkdir /home/$USERNAME/Maildir
  1073. fi
  1074. tar -xzvf /root/tempmail/root/tempbackupemail/$USERNAME/maildir.tar.gz -C /
  1075. if [ ! "$?" = "0" ]; then
  1076. unmount_drive
  1077. exit 927
  1078. fi
  1079. rm -rf /root/tempmail
  1080. fi
  1081. done
  1082. fi
  1083. }
  1084. function restore_dlna {
  1085. if [[ $RESTORE_APP != 'all' ]]; then
  1086. if [[ $RESTORE_APP != 'dlna' ]]; then
  1087. return
  1088. fi
  1089. fi
  1090. if [ -d /var/cache/minidlna ]; then
  1091. if [ -d $USB_MOUNT/backup/dlna ]; then
  1092. echo $"Restoring DLNA cache"
  1093. restore_directory_from_usb /root/tempdlna dlna
  1094. cp -r /root/tempdlna/var/cache/minidlna/* /var/cache/minidlna/
  1095. if [ ! "$?" = "0" ]; then
  1096. rm -rf /root/tempdlna
  1097. unmount_drive
  1098. exit 982
  1099. fi
  1100. rm -rf /root/tempdlna
  1101. fi
  1102. fi
  1103. }
  1104. function restore_voip {
  1105. if [[ $RESTORE_APP != 'all' ]]; then
  1106. if [[ $RESTORE_APP != 'voip' ]]; then
  1107. return
  1108. fi
  1109. fi
  1110. if [ -d $USB_MOUNT/backup/voip ]; then
  1111. echo $"Restoring VoIP settings"
  1112. restore_directory_from_usb /root/tempvoip voip
  1113. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.ini /etc/
  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/sipwitch.conf /etc/sipwitch.conf
  1120. if [ ! "$?" = "0" ]; then
  1121. rm -rf /root/tempvoip
  1122. unmount_drive
  1123. exit 3679
  1124. fi
  1125. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.sqlite /var/lib/mumble-server/
  1126. if [ ! "$?" = "0" ]; then
  1127. rm -rf /root/tempvoip
  1128. unmount_drive
  1129. exit 276
  1130. fi
  1131. rm -rf /root/tempvoip
  1132. cp /etc/ssl/certs/mumble* /var/lib/mumble-server
  1133. cp /etc/ssl/private/mumble* /var/lib/mumble-server
  1134. chown -R mumble-server:mumble-server /var/lib/mumble-server
  1135. service sipwitch restart
  1136. service mumble-server restart
  1137. fi
  1138. }
  1139. function restore_tox {
  1140. if [[ $RESTORE_APP != 'all' ]]; then
  1141. if [[ $RESTORE_APP != 'tox' ]]; then
  1142. return
  1143. fi
  1144. fi
  1145. if [ -d $USB_MOUNT/backup/tox ]; then
  1146. echo $"Restoring Tox node settings"
  1147. restore_directory_from_usb / tox
  1148. if [ ! "$?" = "0" ]; then
  1149. unmount_drive
  1150. exit 6393
  1151. fi
  1152. cp /var/lib/tox-bootstrapd/tox-bootstrapd.conf /etc/tox-bootstrapd.conf
  1153. systemctl restart tox-bootstrapd.service
  1154. if [ ! "$?" = "0" ]; then
  1155. systemctl status tox-bootstrapd.service
  1156. unmount_drive
  1157. exit 59369
  1158. fi
  1159. fi
  1160. }
  1161. function get_restore_app {
  1162. if [ ${1} ]; then
  1163. if [ ! -d /home/${1} ]; then
  1164. RESTORE_APP=${1}
  1165. echo $"Restore $RESTORE_APP"
  1166. fi
  1167. fi
  1168. }
  1169. get_restore_app ${2}
  1170. mount_drive ${1} ${2}
  1171. check_backup_exists
  1172. check_admin_user
  1173. copy_gpg_keys
  1174. restore_configuration
  1175. same_admin_user
  1176. update_domains
  1177. restore_mariadb
  1178. restore_letsencrypt
  1179. restore_tor
  1180. restore_mutt_settings
  1181. restore_gpg
  1182. restore_procmail
  1183. restore_spamassassin
  1184. restore_admin_readme
  1185. restore_ipfs
  1186. restore_user_ssh_keys
  1187. restore_user_config
  1188. restore_user_monkeysphere
  1189. restore_user_fin
  1190. restore_user_local
  1191. restore_certs
  1192. restore_personal_settings
  1193. restore_mailing_list
  1194. restore_xmpp
  1195. restore_gnu_social
  1196. restore_hubzilla
  1197. restore_rss_reader
  1198. restore_syncthing
  1199. restore_mediagoblin
  1200. restore_gogs
  1201. restore_wiki
  1202. restore_blog
  1203. restore_cjdns
  1204. restore_email
  1205. restore_dlna
  1206. restore_voip
  1207. restore_tox
  1208. unmount_drive
  1209. echo $"Restore from USB drive is complete. You can now unplug it."
  1210. exit 0