freedombone-restore-remote 34KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Restore from a given remote server
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  31. CONFIG_FILE=$HOME/${PROJECT_NAME}.cfg
  32. BACKUP_EXTRA_DIRECTORIES=/root/backup-extra-dirs.csv
  33. export TEXTDOMAIN=${PROJECT_NAME}-restore-remote
  34. export TEXTDOMAINDIR="/usr/share/locale"
  35. SERVER_NAME=$1
  36. # whether to restore everything or just a specific application
  37. RESTORE_APP='all'
  38. if [ ${2} ]; then
  39. RESTORE_APP=${2}
  40. fi
  41. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | -nawk -F ':' '{print $2}')
  42. ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME}
  43. # Temporary location for data to be backed up to other servers
  44. SERVER_DIRECTORY=/root/remoterestore
  45. BACKUP_LIST=/home/${ADMIN_USERNAME}/backup.list
  46. if [ ! $SERVER_NAME ]; then
  47. echo $'restorefromfriend [server]'
  48. exit 1
  49. fi
  50. if [ ! -f $BACKUP_LIST ]; then
  51. echo $"No friends list found at $BACKUP_LIST"
  52. exit 2
  53. fi
  54. if ! grep -q "$SERVER_NAME" $BACKUP_LIST; then
  55. echo $"Server not found within the friends list"
  56. exit 3
  57. fi
  58. REMOTE_SERVER=$(grep -i "$SERVER_NAME" $BACKUP_LIST | awk -F ' ' '{print $1}')
  59. REMOTE_SSH_PORT=$(grep -i "$SERVER_NAME" $BACKUP_LIST | awk -F ' ' '{print $2}')
  60. REMOTE_DIRECTORY=$(grep -i "$SERVER_NAME" $BACKUP_LIST | awk -F ' ' '{print $3}')
  61. REMOTE_PASSWORD=$(grep -i "$SERVER_NAME" $BACKUP_LIST | awk -F ' ' '{print $4}')
  62. REMOTE_SERVER=$REMOTE_SERVER:$REMOTE_DIRECTORY
  63. NOW=$(date +"%Y-%m-%d %H:%M:%S")
  64. echo "$NOW Starting restore from $REMOTE_SERVER" >> /var/log/remotebackups.log
  65. rsync -ratlzv --rsh="/usr/bin/sshpass -p $REMOTE_PASSWORD ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" $REMOTE_SERVER/backup $SERVER_DIRECTORY
  66. if [ ! "$?" = "0" ]; then
  67. echo "$NOW Restore from $REMOTE_SERVER failed" >> /var/log/remotebackups.log
  68. # Send a warning email
  69. echo "Restore from $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} restore from friend" $ADMIN_EMAIL_ADDRESS
  70. exit 790
  71. else
  72. echo "$NOW Restored encrypted data from $REMOTE_SERVER" >> /var/log/remotebackups.log
  73. fi
  74. # MariaDB password
  75. DATABASE_PASSWORD=$(cat /root/dbpass)
  76. function restore_directory_from_friend {
  77. if [ ! -d ${1} ]; then
  78. mkdir ${1}
  79. fi
  80. obnam restore -r $SERVER_DIRECTORY/backup/${2} --to ${1}
  81. }
  82. function copy_gpg_keys {
  83. echo $"Copying GPG keys from admin user to root"
  84. cp -r /home/$ADMIN_USERNAME/.gnupg /root
  85. }
  86. function restore_database_from_friend {
  87. DATABASE_PASSWORD=
  88. RESTORE_SUBDIR="root"
  89. if [ -d $SERVER_DIRECTORY/backup/${1} ]; then
  90. echo $"Restoring ${1} database"
  91. restore_directory_from_friend /root/temp${1}data ${1}data
  92. if [ ! -f /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql ]; then
  93. echo $"Unable to restore ${1} database"
  94. rm -rf /root/temp${1}data
  95. exit 503
  96. fi
  97. mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${1} -o < /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
  98. if [ ! "$?" = "0" ]; then
  99. echo "$mysqlsuccess"
  100. exit 964
  101. fi
  102. shred -zu /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/*
  103. rm -rf /root/temp${1}data
  104. echo $"Restoring ${1} installation"
  105. restore_directory_from_friend /root/temp${1} ${1}
  106. RESTORE_SUBDIR="var"
  107. if [ ${1} ]; then
  108. # special handling of ttrss
  109. if [[ ${2} == "ttrss" ]]; then
  110. if [ -d /etc/share/tt-rss ]; then
  111. rm -rf /etc/share/tt-rss
  112. mv /root/temp${1}/etc/share/tt-rss /etc/share/
  113. if [ ! "$?" = "0" ]; then
  114. exit 639
  115. fi
  116. if [ -d /etc/letsencrypt/live/${2} ]; then
  117. ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
  118. ln -s /etc/letsencrypt/live/${2}/fullchain.pem /etc/ssl/certs/${2}.pem
  119. else
  120. # Ensure that the bundled SSL cert is being used
  121. if [ -f /etc/ssl/certs/${2}.bundle.crt ]; then
  122. sed -i "s|${2}.crt|${2}.bundle.crt|g" /etc/nginx/sites-available/${2}
  123. fi
  124. fi
  125. fi
  126. fi
  127. if [ -d /var/www/${2}/htdocs ]; then
  128. if [ -d /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs ]; then
  129. rm -rf /var/www/${2}/htdocs
  130. mv /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs /var/www/${2}/
  131. if [ ! "$?" = "0" ]; then
  132. exit 683
  133. fi
  134. if [ -d /etc/letsencrypt/live/${2} ]; then
  135. ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
  136. ln -s /etc/letsencrypt/live/${2}/fullchain.pem /etc/ssl/certs/${2}.pem
  137. else
  138. # Ensure that the bundled SSL cert is being used
  139. if [ -f /etc/ssl/certs/${2}.bundle.crt ]; then
  140. sed -i "s|${2}.crt|${2}.bundle.crt|g" /etc/nginx/sites-available/${2}
  141. fi
  142. fi
  143. fi
  144. fi
  145. fi
  146. fi
  147. }
  148. function restore_configuration {
  149. if [[ $RESTORE_APP != 'all' ]]; then
  150. if [[ $RESTORE_APP != 'configuration' ]]; then
  151. return
  152. fi
  153. fi
  154. if [ -d $SERVER_DIRECTORY/backup/config ]; then
  155. echo $"Restoring configuration files"
  156. restore_directory_from_friend /root/tempconfig config
  157. cp -f /root/tempconfig/root/${PROJECT_NAME}.cfg $CONFIG_FILE
  158. if [ ! "$?" = "0" ]; then
  159. unmount_drive
  160. rm -rf /root/tempconfig
  161. exit 5372
  162. fi
  163. if [ -f $CONFIG_FILE ]; then
  164. # install according to the config file
  165. freedombone -c $CONFIG_FILE
  166. fi
  167. cp -f /root/tempconfig/root/${PROJECT_NAME}-completed.txt $COMPLETION_FILE
  168. if [ ! "$?" = "0" ]; then
  169. unmount_drive
  170. rm -rf /root/tempconfig
  171. exit 7252
  172. fi
  173. if [ -f /root/tempconfig${BACKUP_EXTRA_DIRECTORIES} ]; then
  174. cp -f /root/tempconfig${BACKUP_EXTRA_DIRECTORIES} ${BACKUP_EXTRA_DIRECTORIES}
  175. if [ ! "$?" = "0" ]; then
  176. unmount_drive
  177. rm -rf /root/tempconfig
  178. exit 62121
  179. fi
  180. fi
  181. # restore nginx password hashes
  182. if [ -f /root/tempconfig/root/htpasswd ]; then
  183. cp -f /root/tempconfig/root/htpasswd /etc/nginx/.htpasswd
  184. fi
  185. rm -rf /root/tempconfig
  186. fi
  187. }
  188. function restore_mariadb {
  189. if [[ $RESTORE_APP != 'all' ]]; then
  190. if [[ $RESTORE_APP != 'mariadb' ]]; then
  191. return
  192. fi
  193. fi
  194. if [ -d $SERVER_DIRECTORY/backup/mariadb ]; then
  195. echo $"Restoring MariaDB settings"
  196. restore_directory_from_friend /root/tempmariadb mariadb
  197. echo $"Get the MariaDB password from the backup"
  198. if [ ! -f /root/tempmariadb/root/tempmariadb/db ]; then
  199. echo $"MariaDB password file not found"
  200. exit 495
  201. fi
  202. BACKUP_MARIADB_PASSWORD=$(cat /root/tempmariadb/root/tempmariadb/db)
  203. if [[ "$BACKUP_MARIADB_PASSWORD" != "$DATABASE_PASSWORD" ]]; then
  204. echo $"Restore the MariaDB user table"
  205. mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" mysql -o < /root/tempmariadb/root/tempmariadb/mysql.sql)
  206. if [ ! "$?" = "0" ]; then
  207. echo $"Try again using the password obtained from backup"
  208. mysqlsuccess=$(mysql -u root --password="$BACKUP_MARIADB_PASSWORD" mysql -o < /root/tempmariadb/root/tempmariadb/mysql.sql)
  209. fi
  210. if [ ! "$?" = "0" ]; then
  211. echo "$mysqlsuccess"
  212. exit 962
  213. fi
  214. echo $"Restarting database"
  215. service mysql restart
  216. echo $"Change the MariaDB password to the backup version"
  217. DATABASE_PASSWORD=$BACKUP_MARIADB_PASSWORD
  218. fi
  219. shred -zu /root/tempmariadb/root/tempmariadb/db
  220. rm -rf /root/tempmariadb
  221. # Change database password file
  222. echo "$DATABASE_PASSWORD" > /root/dbpass
  223. chmod 600 /root/dbpass
  224. fi
  225. }
  226. function restore_letsencrypt {
  227. if [[ $RESTORE_APP != 'all' ]]; then
  228. if [[ $RESTORE_APP != 'letsencrypt' ]]; then
  229. return
  230. fi
  231. fi
  232. if [ -d $SERVER_DIRECTORY/backup/letsencrypt ]; then
  233. echo $"Restoring Lets Encrypt settings"
  234. restore_directory_from_friend / letsencrypt
  235. fi
  236. }
  237. function restore_tor {
  238. if [[ $RESTORE_APP != 'all' ]]; then
  239. if [[ $RESTORE_APP != 'tor' ]]; then
  240. return
  241. fi
  242. fi
  243. if [ -d $SERVER_DIRECTORY/backup/tor ]; then
  244. echo $"Restoring Tor settings"
  245. restore_directory_from_friend / tor
  246. fi
  247. }
  248. function restore_mutt_settings {
  249. if [[ $RESTORE_APP != 'all' ]]; then
  250. if [[ $RESTORE_APP != 'mutt' ]]; then
  251. return
  252. fi
  253. fi
  254. for d in $SERVER_DIRECTORY/backup/mutt/*/ ; do
  255. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  256. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  257. if [ -d $SERVER_DIRECTORY/backup/mutt/$USERNAME ]; then
  258. if [ ! -d /home/$USERNAME ]; then
  259. ${PROJECT_NAME}-adduser $USERNAME
  260. fi
  261. echo $"Restoring Mutt settings for $USERNAME"
  262. restore_directory_from_friend /root/tempmutt mutt/$USERNAME
  263. if [ -f /root/tempmutt/home/$USERNAME/tempbackup/.muttrc ]; then
  264. cp -f /root/tempmutt/home/$USERNAME/tempbackup/.muttrc /home/$USERNAME/.muttrc
  265. fi
  266. if [ -f /root/tempmutt/home/$USERNAME/tempbackup/Muttrc ]; then
  267. cp -f /root/tempmutt/home/$USERNAME/tempbackup/Muttrc /etc/Muttrc
  268. fi
  269. if [ ! "$?" = "0" ]; then
  270. rm -rf /root/tempmutt
  271. exit 276
  272. fi
  273. rm -rf /root/tempmutt
  274. fi
  275. fi
  276. done
  277. }
  278. function restore_gpg {
  279. if [[ $RESTORE_APP != 'all' ]]; then
  280. if [[ $RESTORE_APP != 'gpg' ]]; then
  281. return
  282. fi
  283. fi
  284. for d in $SERVER_DIRECTORY/backup/gnupg/*/ ; do
  285. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  286. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  287. if [ -d $SERVER_DIRECTORY/backup/gnupg/$USERNAME ]; then
  288. if [ ! -d /home/$USERNAME ]; then
  289. ${PROJECT_NAME}-adduser $USERNAME
  290. fi
  291. echo $"Restoring gnupg settings for $USERNAME"
  292. restore_directory_from_friend /root/tempgnupg gnupg/$USERNAME
  293. cp -r /root/tempgnupg/home/$USERNAME/.gnupg /home/$USERNAME/
  294. if [ ! "$?" = "0" ]; then
  295. rm -rf /root/tempgnupg
  296. exit 276
  297. fi
  298. rm -rf /root/tempgnupg
  299. if [[ "$USERNAME" == "$ADMIN_USERNAME" ]]; then
  300. cp -r /home/$USERNAME/.gnupg /root
  301. if [ ! "$?" = "0" ]; then
  302. exit 283
  303. fi
  304. fi
  305. fi
  306. fi
  307. done
  308. }
  309. function restore_procmail {
  310. if [[ $RESTORE_APP != 'all' ]]; then
  311. if [[ $RESTORE_APP != 'procmail' ]]; then
  312. return
  313. fi
  314. fi
  315. for d in $SERVER_DIRECTORY/backup/procmail/*/ ; do
  316. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  317. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  318. if [ -d $SERVER_DIRECTORY/backup/procmail/$USERNAME ]; then
  319. if [ ! -d /home/$USERNAME ]; then
  320. ${PROJECT_NAME}-adduser $USERNAME
  321. fi
  322. echo $"Restoring procmail settings for $USERNAME"
  323. restore_directory_from_friend /root/tempprocmail procmail/$USERNAME
  324. cp -f /root/tempprocmail/home/$USERNAME/tempbackup/.procmailrc /home/$USERNAME/
  325. if [ ! "$?" = "0" ]; then
  326. rm -rf /root/tempprocmail
  327. exit 276
  328. fi
  329. rm -rf /root/tempprocmail
  330. fi
  331. fi
  332. done
  333. }
  334. function restore_spamassassin {
  335. if [[ $RESTORE_APP != 'all' ]]; then
  336. if [[ $RESTORE_APP != 'spamassassin' ]]; then
  337. return
  338. fi
  339. fi
  340. for d in $SERVER_DIRECTORY/backup/spamassassin/*/ ; do
  341. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  342. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  343. if [ -d $SERVER_DIRECTORY/backup/spamassassin/$USERNAME ]; then
  344. if [ ! -d /home/$USERNAME ]; then
  345. ${PROJECT_NAME}-adduser $USERNAME
  346. fi
  347. echo $"Restoring spamassassin settings for $USERNAME"
  348. restore_directory_from_friend /root/tempspamassassin spamassassin/$USERNAME
  349. cp -rf /root/tempspamassassin/home/$USERNAME/.spamassassin /home/$USERNAME/
  350. if [ ! "$?" = "0" ]; then
  351. rm -rf /root/tempspamassassin
  352. exit 276
  353. fi
  354. rm -rf /root/tempspamassassin
  355. fi
  356. fi
  357. done
  358. }
  359. function restore_admin_readme {
  360. if [[ $RESTORE_APP != 'all' ]]; then
  361. if [[ $RESTORE_APP != 'readme' ]]; then
  362. return
  363. fi
  364. fi
  365. if [ -d $SERVER_DIRECTORY/backup/readme ]; then
  366. echo $"Restoring README"
  367. restore_directory_from_friend /root/tempreadme readme
  368. cp -f /root/tempreadme/home/$ADMIN_USERNAME/tempbackup/README /home/$ADMIN_USERNAME/
  369. if [ ! "$?" = "0" ]; then
  370. rm -rf /root/tempreadme
  371. exit 276
  372. fi
  373. rm -rf /root/tempreadme
  374. fi
  375. }
  376. function restore_ipfs {
  377. if [[ $RESTORE_APP != 'all' ]]; then
  378. if [[ $RESTORE_APP != 'ipfs' ]]; then
  379. return
  380. fi
  381. fi
  382. if [ -d $SERVER_DIRECTORY/backup/ipfs ]; then
  383. echo $"Restoring IPFS"
  384. restore_directory_from_friend /root/tempipfs ipfs
  385. cp -rf /root/tempipfs/home/$ADMIN_USERNAME/.ipfs/* /home/$ADMIN_USERNAME/.ipfs
  386. if [ ! "$?" = "0" ]; then
  387. rm -rf /root/tempipfs
  388. exit 276
  389. fi
  390. rm -rf /root/tempipfs
  391. fi
  392. }
  393. function restore_ssh_keys {
  394. if [[ $RESTORE_APP != 'all' ]]; then
  395. if [[ $RESTORE_APP != 'ssh' ]]; then
  396. return
  397. fi
  398. fi
  399. for d in $SERVER_DIRECTORY/backup/ssh/*/ ; do
  400. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  401. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  402. if [ -d $SERVER_DIRECTORY/backup/ssh/$USERNAME ]; then
  403. if [ ! -d /home/$USERNAME ]; then
  404. ${PROJECT_NAME}-adduser $USERNAME
  405. fi
  406. echo $"Restoring ssh keys for $USERNAME"
  407. restore_directory_from_friend /root/tempssh ssh/$USERNAME
  408. cp -r /root/tempssh/home/$USERNAME/.ssh /home/$USERNAME/
  409. if [ ! "$?" = "0" ]; then
  410. rm -rf /root/tempssh
  411. exit 664
  412. fi
  413. rm -rf /root/tempssh
  414. fi
  415. fi
  416. done
  417. }
  418. function restore_user_config {
  419. if [[ $RESTORE_APP != 'all' ]]; then
  420. if [[ $RESTORE_APP != 'userconfig' ]]; then
  421. return
  422. fi
  423. fi
  424. for d in $SERVER_DIRECTORY/backup/config/*/ ; do
  425. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  426. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  427. if [ -d $SERVER_DIRECTORY/backup/config/$USERNAME ]; then
  428. if [ ! -d /home/$USERNAME ]; then
  429. ${PROJECT_NAME}-adduser $USERNAME
  430. fi
  431. echo $"Restoring config files for $USERNAME"
  432. restore_directory_from_friend /root/tempconfig config/$USERNAME
  433. cp -r /root/tempconfig/home/$USERNAME/.config /home/$USERNAME/
  434. if [ ! "$?" = "0" ]; then
  435. rm -rf /root/tempconfig
  436. exit 664
  437. fi
  438. rm -rf /root/tempconfig
  439. fi
  440. fi
  441. done
  442. }
  443. function restore_user_fin {
  444. if [[ $RESTORE_APP != 'all' ]]; then
  445. if [[ $RESTORE_APP != 'userfin' ]]; then
  446. return
  447. fi
  448. fi
  449. for d in $SERVER_DIRECTORY/backup/fin/*/ ; do
  450. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  451. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  452. if [ -d $SERVER_DIRECTORY/backup/fin/$USERNAME ]; then
  453. if [ ! -d /home/$USERNAME ]; then
  454. ${PROJECT_NAME}-adduser $USERNAME
  455. fi
  456. echo $"Restoring fin files for $USERNAME"
  457. restore_directory_from_friend /root/tempfin fin/$USERNAME
  458. cp -r /root/tempfin/home/$USERNAME/.fin /home/$USERNAME/
  459. if [ ! "$?" = "0" ]; then
  460. rm -rf /root/tempfin
  461. exit 664
  462. fi
  463. rm -rf /root/tempfin
  464. fi
  465. fi
  466. done
  467. }
  468. function restore_user_local {
  469. if [[ $RESTORE_APP != 'all' ]]; then
  470. if [[ $RESTORE_APP != 'userlocal' ]]; then
  471. return
  472. fi
  473. fi
  474. for d in $SERVER_DIRECTORY/backup/local/*/ ; do
  475. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  476. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  477. if [ -d $SERVER_DIRECTORY/backup/local/$USERNAME ]; then
  478. if [ ! -d /home/$USERNAME ]; then
  479. ${PROJECT_NAME}-adduser $USERNAME
  480. fi
  481. echo $"Restoring local files for $USERNAME"
  482. restore_directory_from_friend /root/templocal local/$USERNAME
  483. cp -r /root/templocal/home/$USERNAME/.local /home/$USERNAME/
  484. if [ ! "$?" = "0" ]; then
  485. rm -rf /root/templocal
  486. exit 664
  487. fi
  488. rm -rf /root/templocal
  489. fi
  490. fi
  491. done
  492. }
  493. function restore_certs {
  494. if [[ $RESTORE_APP != 'all' ]]; then
  495. if [[ $RESTORE_APP != 'certs' ]]; then
  496. return
  497. fi
  498. fi
  499. if [ -d $SERVER_DIRECTORY/backup/ssl ]; then
  500. echo $"Restoring certificates"
  501. restore_directory_from_friend /root/tempssl ssl
  502. cp -r /root/tempssl/etc/ssl/* /etc/ssl
  503. if [ ! "$?" = "0" ]; then
  504. exit 276
  505. fi
  506. rm -rf /root/tempssl
  507. # restore ownership
  508. if [ -f /etc/ssl/private/xmpp.key ]; then
  509. chown prosody:prosody /etc/ssl/private/xmpp.key
  510. chown prosody:prosody /etc/ssl/certs/xmpp.*
  511. fi
  512. if [ -d /etc/dovecot ]; then
  513. chown root:dovecot /etc/ssl/private/dovecot.*
  514. chown root:dovecot /etc/ssl/certs/dovecot.*
  515. fi
  516. if [ -f /etc/ssl/private/exim.key ]; then
  517. cp /etc/ssl/private/exim.key /etc/exim4
  518. cp /etc/ssl/certs/exim.crt /etc/exim4
  519. cp /etc/ssl/certs/exim.dhparam /etc/exim4
  520. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  521. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  522. fi
  523. if [ -f /etc/ssl/private/mumble.key ]; then
  524. if [ -d /var/lib/mumble-server ]; then
  525. cp /etc/ssl/certs/mumble.* /var/lib/mumble-server
  526. cp /etc/ssl/private/mumble.key /var/lib/mumble-server
  527. chown -R mumble-server:mumble-server /var/lib/mumble-server
  528. fi
  529. fi
  530. fi
  531. }
  532. function restore_personal_settings {
  533. if [[ $RESTORE_APP != 'all' ]]; then
  534. if [[ $RESTORE_APP != 'personal' ]]; then
  535. return
  536. fi
  537. fi
  538. for d in $SERVER_DIRECTORY/backup/personal/*/ ; do
  539. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  540. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  541. if [ -d $SERVER_DIRECTORY/backup/personal/$USERNAME ]; then
  542. if [ ! -d /home/$USERNAME ]; then
  543. ${PROJECT_NAME}-adduser $USERNAME
  544. fi
  545. echo $"Restoring personal settings for $USERNAME"
  546. restore_directory_from_friend /root/temppersonal personal/$USERNAME
  547. if [ -d /home/$USERNAME/personal ]; then
  548. rm -rf /home/$USERNAME/personal
  549. fi
  550. mv /root/temppersonal/home/$USERNAME/personal /home/$USERNAME
  551. if [ ! "$?" = "0" ]; then
  552. exit 184
  553. fi
  554. rm -rf /root/temppersonal
  555. fi
  556. fi
  557. done
  558. }
  559. function restore_mailing_list {
  560. if [[ $RESTORE_APP != 'all' ]]; then
  561. if [[ $RESTORE_APP != 'mailinglist' ]]; then
  562. return
  563. fi
  564. fi
  565. if [ -d /var/spool/mlmmj ]; then
  566. echo $"Restoring public mailing list"
  567. restore_directory_from_friend /root/tempmailinglist mailinglist
  568. cp -r /root/tempmailinglist/root/spool/mlmmj/* /var/spool/mlmmj
  569. if [ ! "$?" = "0" ]; then
  570. exit 526
  571. fi
  572. rm -rf /root/tempmailinglist
  573. fi
  574. }
  575. function restore_xmpp {
  576. if [[ $RESTORE_APP != 'all' ]]; then
  577. if [[ $RESTORE_APP != 'xmpp' ]]; then
  578. return
  579. fi
  580. fi
  581. if [ -d /var/lib/prosody ]; then
  582. echo $"Restoring XMPP settings"
  583. restore_directory_from_friend /root/tempxmpp xmpp
  584. cp -r /root/tempxmpp/var/lib/prosody/* /var/lib/prosody
  585. if [ ! "$?" = "0" ]; then
  586. exit 725
  587. fi
  588. rm -rf /root/tempxmpp
  589. service prosody restart
  590. chown -R prosody:prosody /var/lib/prosody/*
  591. fi
  592. }
  593. function restore_gnu_social {
  594. if [[ $RESTORE_APP != 'all' ]]; then
  595. if [[ $RESTORE_APP != 'gnusocial' ]]; then
  596. return
  597. fi
  598. fi
  599. if grep -q "GNU Social domain" $COMPLETION_FILE; then
  600. MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
  601. restore_database_from_friend gnusocial ${MICROBLOG_DOMAIN_NAME}
  602. if [ -d /root/tempgnusocial ]; then
  603. rm -rf /root/tempgnusocial
  604. fi
  605. fi
  606. }
  607. function restore_rss_reader {
  608. if [[ $RESTORE_APP != 'all' ]]; then
  609. if [[ $RESTORE_APP != 'ttrss' ]]; then
  610. return
  611. fi
  612. fi
  613. if grep -q "RSS reader domain" $COMPLETION_FILE; then
  614. RSS_READER_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "RSS reader domain" | awk -F ':' '{print $2}')
  615. restore_database_from_friend ttrss ${RSS_READER_DOMAIN_NAME}
  616. if [ -d $SERVER_DIRECTORY/backup/ttrss ]; then
  617. chown -R www-data:www-data /etc/share/tt-rss
  618. fi
  619. if [ -d /root/tempttrss ]; then
  620. rm -rf /root/tempttrss
  621. fi
  622. fi
  623. }
  624. function restore_hubzilla {
  625. if [[ $RESTORE_APP != 'all' ]]; then
  626. if [[ $RESTORE_APP != 'hubzilla' ]]; then
  627. return
  628. fi
  629. fi
  630. if grep -q "Hubzilla domain" $COMPLETION_FILE; then
  631. HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
  632. restore_database_from_friend hubzilla ${HUBZILLA_DOMAIN_NAME}
  633. if [ -d $SERVER_DIRECTORY/backup/hubzilla ]; then
  634. if [ ! -d /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3 ]; then
  635. mkdir -p /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  636. fi
  637. chmod 777 /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  638. chown -R www-data:www-data /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/*
  639. fi
  640. if [ -d /root/temphubzilla ]; then
  641. rm -rf /root/temphubzilla
  642. fi
  643. fi
  644. }
  645. function restore_owncloud {
  646. if [[ $RESTORE_APP != 'all' ]]; then
  647. if [[ $RESTORE_APP != 'owncloud' ]]; then
  648. return
  649. fi
  650. fi
  651. OWNCLOUD_PATH=/var/www/owncloud
  652. if grep -q "Owncloud domain" $COMPLETION_FILE; then
  653. OWNCLOUD_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Owncloud domain" | awk -F ':' '{print $2}')
  654. restore_database_from_friend owncloud $OWNCLOUD_DOMAIN_NAME
  655. if [ -d $SERVER_DIRECTORY/backup/owncloudfiles ]; then
  656. echo $"Restoring Owncloud installation"
  657. cp -r /root/tempowncloudfiles/* /
  658. if [ ! "$?" = "0" ]; then
  659. exit 1458
  660. fi
  661. rm -rf /root/tempowncloudfiles
  662. fi
  663. if [ -d $SERVER_DIRECTORY/backup/owncloudconfig ]; then
  664. echo $"Restoring Owncloud installation"
  665. cp -r /root/tempowncloudconfig/* /
  666. if [ ! "$?" = "0" ]; then
  667. exit 2571
  668. fi
  669. rm -rf /root/tempowncloudconfig
  670. fi
  671. chown -R www-data:www-data $OWNCLOUD_PATH
  672. chown root:root $OWNCLOUD_PATH/config/config.php
  673. # re-index files
  674. for d in /home/*/ ; do
  675. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  676. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  677. $OWNCLOUD_PATH/occ files:scan $USERNAME
  678. fi
  679. done
  680. fi
  681. }
  682. function restore_mediagoblin {
  683. if [[ $RESTORE_APP != 'all' ]]; then
  684. if [[ $RESTORE_APP != 'mediagoblin' ]]; then
  685. return
  686. fi
  687. fi
  688. if grep -q "Mediagoblin domain" $COMPLETION_FILE; then
  689. MEDIAGOBLIN_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Mediagoblin domain" | awk -F ':' '{print $2}')
  690. if [ -d $SERVER_DIRECTORY/backup/mediagoblin ]; then
  691. echo $"Restoring Mediagoblin installation"
  692. restore_directory_from_friend /root/tempmediagoblin mediagoblin
  693. cp -r /root/tempmediagoblin/* /
  694. if [ ! "$?" = "0" ]; then
  695. exit 5626
  696. fi
  697. rm -rf /root/tempmediagoblin
  698. fi
  699. chown -hR mediagoblin:www-data /var/www/$MEDIAGOBLIN_DOMAIN_NAME/htdocs
  700. fi
  701. }
  702. function restore_gogs {
  703. if [[ $RESTORE_APP != 'all' ]]; then
  704. if [[ $RESTORE_APP != 'gogs' ]]; then
  705. return
  706. fi
  707. fi
  708. if grep -q "Gogs domain" $COMPLETION_FILE; then
  709. GIT_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
  710. restore_database_from_friend gogs $GIT_DOMAIN_NAME
  711. if [ -d $SERVER_DIRECTORY/backup/gogs ]; then
  712. if [ ! -d /home/git/go/src/github.com/gogits/gogs/custom ]; then
  713. mkdir -p /home/git/go/src/github.com/gogits/gogs/custom
  714. fi
  715. cp -r /root/tempgogs/home/git/go/src/github.com/gogits/gogs/custom/* /home/git/go/src/github.com/gogits/gogs/custom/
  716. if [ ! "$?" = "0" ]; then
  717. exit 5885
  718. fi
  719. echo $"Restoring Gogs repos"
  720. restore_directory_from_friend /root/tempgogsrepos gogsrepos
  721. cp -r /root/tempgogsrepos/home/git/gogs-repositories/* /home/git/gogs-repositories/
  722. if [ ! "$?" = "0" ]; then
  723. exit 7649
  724. fi
  725. echo $"Restoring Gogs authorized_keys"
  726. restore_directory_from_friend /root/tempgogsssh gogsssh
  727. if [ ! -d /home/git/.ssh ]; then
  728. mkdir /home/git/.ssh
  729. fi
  730. cp -r /root/tempgogsssh/home/git/.ssh/* /home/git/.ssh/
  731. if [ ! "$?" = "0" ]; then
  732. exit 74239
  733. fi
  734. rm -rf /root/tempgogs
  735. rm -rf /root/tempgogsrepos
  736. rm -rf /root/tempgogsssh
  737. chown -R git:git /home/git
  738. fi
  739. fi
  740. }
  741. function restore_wiki {
  742. if [[ $RESTORE_APP != 'all' ]]; then
  743. if [[ $RESTORE_APP != 'wiki' ]]; then
  744. return
  745. fi
  746. fi
  747. if [ -d $SERVER_DIRECTORY/backup/wiki ]; then
  748. WIKI_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Wiki domain" | awk -F ':' '{print $2}')
  749. echo $"Restoring Wiki installation $WIKI_DOMAIN_NAME"
  750. restore_directory_from_friend /root/tempwiki wiki
  751. cp -r /root/tempwiki/var/lib/dokuwiki/* /var/lib/dokuwiki/
  752. if [ ! "$?" = "0" ]; then
  753. exit 868
  754. fi
  755. restore_directory_from_friend /root/tempwiki2 wiki2
  756. cp -r /root/tempwiki2/etc/dokuwiki/* /etc/dokuwiki/
  757. if [ ! "$?" = "0" ]; then
  758. exit 869
  759. fi
  760. rm -rf /root/tempwiki
  761. rm -rf /root/tempwiki2
  762. chown -R www-data:www-data /var/lib/dokuwiki/*
  763. # Ensure that the bundled SSL cert is being used
  764. if [ -f /etc/ssl/certs/${WIKI_DOMAIN_NAME}.bundle.crt ]; then
  765. sed -i "s|${WIKI_DOMAIN_NAME}.crt|${WIKI_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${WIKI_DOMAIN_NAME}
  766. fi
  767. if [ -d /etc/letsencrypt/live/${WIKI_DOMAIN_NAME} ]; then
  768. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${WIKI_DOMAIN_NAME}.key
  769. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${WIKI_DOMAIN_NAME}.pem
  770. fi
  771. fi
  772. }
  773. function restore_blog {
  774. if [[ $RESTORE_APP != 'all' ]]; then
  775. if [[ $RESTORE_APP != 'blog' ]]; then
  776. return
  777. fi
  778. fi
  779. if [ -d $SERVER_DIRECTORY/backup/blog ]; then
  780. FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
  781. echo $"Restoring blog installation $FULLBLOG_DOMAIN_NAME"
  782. mkdir /root/tempblog
  783. restore_directory_from_friend /root/tempblog blog
  784. rm -rf /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs
  785. cp -r /root/tempblog/var/www/${FULLBLOG_DOMAIN_NAME}/htdocs /var/www/${FULLBLOG_DOMAIN_NAME}/
  786. if [ ! "$?" = "0" ]; then
  787. exit 593
  788. fi
  789. rm -rf /root/tempblog
  790. if [ ! -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content ]; then
  791. echo $"No content directory found after restoring blog"
  792. exit 287
  793. fi
  794. # Ensure that the bundled SSL cert is being used
  795. if [ -f /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.bundle.crt ]; then
  796. sed -i "s|${FULLBLOG_DOMAIN_NAME}.crt|${FULLBLOG_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${FULLBLOG_DOMAIN_NAME}
  797. fi
  798. for d in /home/*/ ; do
  799. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  800. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  801. if [ -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post ]; then
  802. mv /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/*.md /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post
  803. fi
  804. done
  805. if [ -d /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME} ]; then
  806. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${FULLBLOG_DOMAIN_NAME}.key
  807. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.pem
  808. fi
  809. fi
  810. }
  811. function restore_cjdns {
  812. if [[ $RESTORE_APP != 'all' ]]; then
  813. if [[ $RESTORE_APP != 'cjdns' ]]; then
  814. return
  815. fi
  816. fi
  817. if [ -d $SERVER_DIRECTORY/backup/cjdns ]; then
  818. echo $"Restoring cjdns installation"
  819. restore_directory_from_friend /root/tempcjdns cjdns
  820. rm -rf /etc/cjdns
  821. cp -r /root/tempcjdns/etc/cjdns /etc/
  822. if [ ! "$?" = "0" ]; then
  823. exit 7438
  824. fi
  825. rm -rf /root/tempcjdns
  826. fi
  827. }
  828. function restore_voip {
  829. if [[ $RESTORE_APP != 'all' ]]; then
  830. if [[ $RESTORE_APP != 'voip' ]]; then
  831. return
  832. fi
  833. fi
  834. if [ -d $SERVER_DIRECTORY/backup/voip ]; then
  835. echo $"Restoring VoIP settings"
  836. restore_directory_from_friend /root/tempvoip voip
  837. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.ini /etc/
  838. if [ ! "$?" = "0" ]; then
  839. rm -rf /root/tempvoip
  840. exit 7823
  841. fi
  842. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/sipwitch.conf /etc/sipwitch.conf
  843. if [ ! "$?" = "0" ]; then
  844. rm -rf /root/tempvoip
  845. exit 7823
  846. fi
  847. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.sqlite /var/lib/mumble-server/
  848. if [ ! "$?" = "0" ]; then
  849. rm -rf /root/tempvoip
  850. exit 276
  851. fi
  852. rm -rf /root/tempvoip
  853. cp /etc/ssl/certs/mumble* /var/lib/mumble-server
  854. cp /etc/ssl/private/mumble* /var/lib/mumble-server
  855. chown -R mumble-server:mumble-server /var/lib/mumble-server
  856. service sipwitch restart
  857. service mumble-server restart
  858. fi
  859. }
  860. function restore_tox {
  861. if [[ $RESTORE_APP != 'all' ]]; then
  862. if [[ $RESTORE_APP != 'tox' ]]; then
  863. return
  864. fi
  865. fi
  866. if [ -d $SERVER_DIRECTORY/backup/tox ]; then
  867. echo $"Restoring Tox node settings"
  868. restore_directory_from_friend / tox
  869. if [ ! "$?" = "0" ]; then
  870. exit 93653
  871. fi
  872. cp /var/lib/tox-bootstrapd/tox-bootstrapd.conf /etc/tox-bootstrapd.conf
  873. systemctl restart tox-bootstrapd.service
  874. if [ ! "$?" = "0" ]; then
  875. systemctl status tox-bootstrapd.service
  876. exit 59369
  877. fi
  878. fi
  879. }
  880. function restore_email {
  881. if [[ $RESTORE_APP != 'all' ]]; then
  882. if [[ $RESTORE_APP != 'email' ]]; then
  883. return
  884. fi
  885. fi
  886. for d in $SERVER_DIRECTORY/backup/mail/*/ ; do
  887. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  888. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  889. if [ -d $SERVER_DIRECTORY/backup/mail/$USERNAME ]; then
  890. if [ ! -d /home/$USERNAME ]; then
  891. ${PROJECT_NAME}-adduser $USERNAME
  892. fi
  893. echo $"Restoring emails for $USERNAME"
  894. restore_directory_from_friend /root/tempmail mail/$USERNAME
  895. if [ ! -d /home/$USERNAME/Maildir ]; then
  896. mkdir /home/$USERNAME/Maildir
  897. fi
  898. tar -xzvf /root/tempmail/root/tempbackupemail/$USERNAME/maildir.tar.gz -C /
  899. if [ ! "$?" = "0" ]; then
  900. exit 927
  901. fi
  902. rm -rf /root/tempmail
  903. fi
  904. fi
  905. done
  906. }
  907. function restore_dlna {
  908. if [[ $RESTORE_APP != 'all' ]]; then
  909. if [[ $RESTORE_APP != 'dlna' ]]; then
  910. return
  911. fi
  912. fi
  913. if [ -d /var/cache/minidlna ]; then
  914. if [ -d $SERVER_DIRECTORY/backup/dlna ]; then
  915. echo $"Restoring DLNA cache"
  916. restore_directory_from_friend /root/tempdlna dlna
  917. cp -r /root/tempdlna/var/cache/minidlna/* /var/cache/minidlna/
  918. if [ ! "$?" = "0" ]; then
  919. exit 982
  920. fi
  921. rm -rf /root/tempdlna
  922. fi
  923. fi
  924. }
  925. # Social key management
  926. # Recover any key fragments and reconstruct the gpg key
  927. ${PROJECT_NAME}-recoverkey -u ${ADMIN_USERNAME} -l $BACKUP_LIST
  928. copy_gpg_keys
  929. restore_configuration
  930. restore_mariadb
  931. restore_letsencrypt
  932. restore_mutt_settings
  933. restore_gpg
  934. restore_procmail
  935. restore_spamassassin
  936. restore_admin_readme
  937. restore_ipfs
  938. restore_ssh_keys
  939. restore_user_config
  940. restore_user_fin
  941. restore_user_local
  942. restore_certs
  943. restore_personal_settings
  944. restore_mailing_list
  945. restore_xmpp
  946. restore_gnu_social
  947. restore_hubzilla
  948. restore_rss_reader
  949. restore_owncloud
  950. restore_mediagoblin
  951. restore_gogs
  952. restore_wiki
  953. restore_blog
  954. restore_cjdns
  955. restore_voip
  956. restore_tox
  957. restore_email
  958. restore_dlna
  959. echo $"*** Remote restore was successful ***"
  960. exit 0