freedombone-restore-remote 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Restore from a given remote server
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. PROJECT_NAME='freedombone'
  29. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  30. CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg
  31. MONGODB_APPS_FILE=$HOME/.mongodbapps
  32. BACKUP_EXTRA_DIRECTORIES=/root/backup-extra-dirs.csv
  33. export TEXTDOMAIN=${PROJECT_NAME}-restore-remote
  34. export TEXTDOMAINDIR="/usr/share/locale"
  35. UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*"
  36. for f in $UTILS_FILES
  37. do
  38. source "$f"
  39. done
  40. SERVER_NAME="$1"
  41. # whether to restore everything or just a specific application
  42. RESTORE_APP='all'
  43. if [ "${2}" ]; then
  44. RESTORE_APP="${2}"
  45. fi
  46. ADMIN_USERNAME=$(get_completion_param "Admin user")
  47. ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME}
  48. # Temporary location for data to be backed up to other servers
  49. SERVER_DIRECTORY=/root/remoterestore
  50. BACKUP_LIST=/home/${ADMIN_USERNAME}/backup.list
  51. if [ ! "$SERVER_NAME" ]; then
  52. echo $'restorefromfriend [server]'
  53. exit 1
  54. fi
  55. if [ ! -f "$BACKUP_LIST" ]; then
  56. echo $"No friends list found at $BACKUP_LIST"
  57. exit 2
  58. fi
  59. if ! grep -q "$SERVER_NAME" "$BACKUP_LIST"; then
  60. echo $"Server not found within the friends list"
  61. exit 3
  62. fi
  63. REMOTE_SERVER=$(grep -i "$SERVER_NAME" "$BACKUP_LIST" | awk -F ' ' '{print $1}')
  64. REMOTE_SSH_PORT=$(grep -i "$SERVER_NAME" "$BACKUP_LIST" | awk -F ' ' '{print $2}')
  65. REMOTE_DIRECTORY=$(grep -i "$SERVER_NAME" "$BACKUP_LIST" | awk -F ' ' '{print $3}')
  66. REMOTE_PASSWORD=$(grep -i "$SERVER_NAME" "$BACKUP_LIST" | awk -F ' ' '{print $4}')
  67. REMOTE_SERVER=$REMOTE_SERVER:$REMOTE_DIRECTORY
  68. NOW=$(date +"%Y-%m-%d %H:%M:%S")
  69. echo "$NOW Starting restore from $REMOTE_SERVER" >> /var/log/remotebackups.log
  70. if ! rsync -ratlzv --rsh="/usr/bin/sshpass -p $REMOTE_PASSWORD ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" "$REMOTE_SERVER/backup" "$SERVER_DIRECTORY"; then
  71. echo "$NOW Restore from $REMOTE_SERVER failed" >> /var/log/remotebackups.log
  72. # Send a warning email
  73. echo "Restore from $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} restore from friend" "$ADMIN_EMAIL_ADDRESS"
  74. exit 790
  75. else
  76. echo "$NOW Restored encrypted data from $REMOTE_SERVER" >> /var/log/remotebackups.log
  77. fi
  78. # MariaDB password
  79. DATABASE_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
  80. function copy_gpg_keys {
  81. echo $"Copying GPG keys from admin user to root"
  82. cp -r "/home/$ADMIN_USERNAME/.gnupg" /root
  83. gpg_set_permissions root
  84. }
  85. function restore_blocklist {
  86. if [[ $RESTORE_APP != 'all' ]]; then
  87. if [[ $RESTORE_APP != 'blocklist' ]]; then
  88. return
  89. fi
  90. fi
  91. if [ -d "$USB_MOUNT/backup/blocklist" ]; then
  92. echo $"Restoring blocklist"
  93. temp_restore_dir=/root/tempblocklist
  94. restore_directory_from_friend $temp_restore_dir blocklist
  95. if [ -d $temp_restore_dir/root/tempbackupblocklist ]; then
  96. cp -f $temp_restore_dir/root/tempbackupblocklist/${PROJECT_NAME}-firewall-domains.cfg /root/${PROJECT_NAME}-firewall-domains.cfg
  97. else
  98. cp -f $temp_restore_dir/${PROJECT_NAME}-firewall-domains.cfg /root/${PROJECT_NAME}-firewall-domains.cfg
  99. fi
  100. rm -rf $temp_restore_dir
  101. firewall_refresh_blocklist
  102. fi
  103. }
  104. function restore_configfiles {
  105. if [[ $RESTORE_APP != 'all' ]]; then
  106. if [[ $RESTORE_APP != 'configfiles' ]]; then
  107. return
  108. fi
  109. fi
  110. if [ -d $SERVER_DIRECTORY/backup/configfiles ]; then
  111. echo $"Restoring configuration files"
  112. temp_restore_dir=/root/tempconfigfiles
  113. restore_directory_from_friend $temp_restore_dir configfiles
  114. if [ -d $temp_restore_dir/root ]; then
  115. if [ -f $temp_restore_dir/root/.nostore ]; then
  116. if [ ! -f /root/.nostore ]; then
  117. touch /root/.nostore
  118. fi
  119. else
  120. if [ -f /root/.nostore ]; then
  121. rm /root/.nostore
  122. fi
  123. fi
  124. else
  125. if [ -f $temp_restore_dir/.nostore ]; then
  126. if [ ! -f /root/.nostore ]; then
  127. touch /root/.nostore
  128. fi
  129. else
  130. if [ -f /root/.nostore ]; then
  131. rm /root/.nostore
  132. fi
  133. fi
  134. fi
  135. #if [ -f $temp_restore_dir$NODEJS_INSTALLED_APPS_FILE ]; then
  136. # cp -f $temp_restore_dir$NODEJS_INSTALLED_APPS_FILE $NODEJS_INSTALLED_APPS_FILE
  137. #fi
  138. #if [ -f $temp_restore_dir/root/${PROJECT_NAME}.cfg ]; then
  139. # cp -f $temp_restore_dir/root/${PROJECT_NAME}.cfg $CONFIGURATION_FILE
  140. # if [ ! "$?" = "0" ]; then
  141. # unmount_drive
  142. # rm -rf $temp_restore_dir
  143. # exit 5372
  144. # fi
  145. #fi
  146. if [ -f "$temp_restore_dir$MONGODB_APPS_FILE" ]; then
  147. if ! cp -f "$temp_restore_dir$MONGODB_APPS_FILE" "$MONGODB_APPS_FILE"; then
  148. unmount_drive
  149. rm -rf $temp_restore_dir
  150. exit 7835335
  151. fi
  152. fi
  153. #if [ -f $CONFIGURATION_FILE ]; then
  154. # # install according to the config file
  155. # freedombone -c $CONFIGURATION_FILE
  156. #fi
  157. #if [ -f $temp_restore_dir/root/${PROJECT_NAME}-completed.txt ]; then
  158. # cp -f $temp_restore_dir/root/${PROJECT_NAME}-completed.txt $COMPLETION_FILE
  159. # if [ ! "$?" = "0" ]; then
  160. # unmount_drive
  161. # rm -rf $temp_restore_dir
  162. # exit 7252
  163. # fi
  164. #fi
  165. if [ -f "${temp_restore_dir}${BACKUP_EXTRA_DIRECTORIES}" ]; then
  166. if ! cp -f "${temp_restore_dir}${BACKUP_EXTRA_DIRECTORIES}" "${BACKUP_EXTRA_DIRECTORIES}"; then
  167. unmount_drive
  168. rm -rf $temp_restore_dir
  169. exit 62121
  170. fi
  171. fi
  172. # restore nginx password hashes
  173. if [ -f $temp_restore_dir/root/htpasswd ]; then
  174. cp -f $temp_restore_dir/root/htpasswd /etc/nginx/.htpasswd
  175. fi
  176. rm -rf $temp_restore_dir
  177. fi
  178. }
  179. function restore_mariadb {
  180. if [[ $RESTORE_APP != 'all' ]]; then
  181. if [[ $RESTORE_APP != 'mariadb' ]]; then
  182. return
  183. fi
  184. fi
  185. if [[ $(is_completed install_mariadb) == "0" ]]; then
  186. function_check install_mariadb
  187. install_mariadb
  188. fi
  189. if [ -d $SERVER_DIRECTORY/backup/mariadb ]; then
  190. echo $"Restoring MariaDB settings"
  191. temp_restore_dir=/root/tempmariadb
  192. restore_directory_from_friend $temp_restore_dir mariadb
  193. store_original_mariadb_password
  194. echo $'Obtaining MariaDB password'
  195. db_pass=$(cat /root/.mariadboriginal)
  196. if [ ${#db_pass} -gt 0 ]; then
  197. echo $"Restore the MariaDB user table"
  198. if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
  199. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}${temp_restore_dir}/mysql.sql)
  200. else
  201. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}/mysql.sql)
  202. fi
  203. # shellcheck disable=SC2181
  204. if [ ! "$?" = "0" ]; then
  205. echo $"Try again using the password obtained from backup"
  206. db_pass=$(${PROJECT_NAME}-pass -u root -a mariadb)
  207. if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
  208. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}${temp_restore_dir}/mysql.sql)
  209. else
  210. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}/mysql.sql)
  211. fi
  212. fi
  213. # shellcheck disable=SC2181
  214. if [ ! "$?" = "0" ]; then
  215. echo "$mysqlsuccess"
  216. exit 962
  217. fi
  218. echo $"Restarting database"
  219. systemctl restart mariadb
  220. echo $"Ensure MariaDB handles authentication"
  221. MARIADB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
  222. mariadb_fix_authentication
  223. fi
  224. rm -rf ${temp_restore_dir}
  225. fi
  226. }
  227. function restore_postgresql {
  228. if [[ $RESTORE_APP != 'all' ]]; then
  229. if [[ $RESTORE_APP != 'postgresql' ]]; then
  230. return
  231. fi
  232. fi
  233. if [[ $(is_completed install_postgresql) == "0" ]]; then
  234. function_check install_postgresql
  235. install_postgresql
  236. fi
  237. if [ -d $SERVER_DIRECTORY/backup/postgresql ]; then
  238. echo $"Restoring Postgresql settings"
  239. temp_restore_dir=/root/temppostgresql
  240. restore_directory_from_friend $temp_restore_dir postgresql
  241. store_original_postgresql_password
  242. echo $'Obtaining Postgresql password'
  243. db_pass=$(cat /root/.postgresqloriginal)
  244. if [ ${#db_pass} -gt 0 ]; then
  245. echo $"Restore the Postgresql user table"
  246. if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
  247. mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}${temp_restore_dir}/postgresql.sql)
  248. else
  249. mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}/postgresql.sql)
  250. fi
  251. # shellcheck disable=SC2181
  252. if [ ! "$?" = "0" ]; then
  253. echo $"Try again using the password obtained from backup"
  254. db_pass=$("${PROJECT_NAME}-pass" -u root -a postgresql)
  255. if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
  256. mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}${temp_restore_dir}/postgresql.sql)
  257. else
  258. mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}/postgresql.sql)
  259. fi
  260. fi
  261. # shellcheck disable=SC2181
  262. if [ ! "$?" = "0" ]; then
  263. echo "$mysqlsuccess"
  264. exit 962
  265. fi
  266. echo $"Restarting database"
  267. systemctl restart postgresql
  268. echo $"Ensure postgresql handles authentication"
  269. POSTGRESQL_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a postgresql)
  270. fi
  271. rm -rf ${temp_restore_dir}
  272. fi
  273. }
  274. function restore_letsencrypt {
  275. if [[ $RESTORE_APP != 'all' ]]; then
  276. if [[ $RESTORE_APP != 'letsencrypt' ]]; then
  277. return
  278. fi
  279. fi
  280. if [ -d $SERVER_DIRECTORY/backup/letsencrypt ]; then
  281. echo $"Restoring Lets Encrypt settings"
  282. restore_directory_from_friend /etc/letsencrypt letsencrypt
  283. fi
  284. }
  285. function restore_passwordstore {
  286. if [[ $RESTORE_APP != 'all' ]]; then
  287. if [[ $RESTORE_APP != 'passwords' ]]; then
  288. return
  289. fi
  290. fi
  291. if [ -d $SERVER_DIRECTORY/backup/passwordstore ]; then
  292. store_original_mariadb_password
  293. echo $"Restoring password store"
  294. restore_directory_from_friend /root/.passwords passwordstore
  295. fi
  296. }
  297. function restore_tor {
  298. if [[ $RESTORE_APP != 'all' ]]; then
  299. if [[ $RESTORE_APP != 'tor' ]]; then
  300. return
  301. fi
  302. fi
  303. if [ -d $SERVER_DIRECTORY/backup/tor ]; then
  304. echo $"Restoring Tor settings"
  305. restore_directory_from_friend /var/lib/tor tor
  306. fi
  307. }
  308. function restore_mutt_settings {
  309. if [[ $RESTORE_APP != 'all' ]]; then
  310. if [[ $RESTORE_APP != 'mutt' ]]; then
  311. return
  312. fi
  313. fi
  314. for d in $SERVER_DIRECTORY/backup/mutt/*/ ; do
  315. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  316. # skip over configurations
  317. if [[ "$USERNAME" == *'configs' ]]; then
  318. continue
  319. fi
  320. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  321. if [ -d "$SERVER_DIRECTORY/backup/mutt/$USERNAME" ]; then
  322. if [ ! -d "/home/$USERNAME" ]; then
  323. ${PROJECT_NAME}-adduser "$USERNAME"
  324. fi
  325. echo $"Restoring Mutt configurations for $USERNAME"
  326. restore_directory_from_friend "/home/$USERNAME/.mutt" "mutt/${USERNAME}configs"
  327. echo $"Restoring Mutt settings for $USERNAME"
  328. temp_restore_dir=/root/tempmutt
  329. restore_directory_from_friend ${temp_restore_dir} "mutt/$USERNAME"
  330. if [ -d "${temp_restore_dir}/home/$USERNAME/tempbackup" ]; then
  331. if [ -f "${temp_restore_dir}/home/$USERNAME/tempbackup/.muttrc" ]; then
  332. cp -f "${temp_restore_dir}/home/$USERNAME/tempbackup/.muttrc" "/home/$USERNAME/.muttrc"
  333. sed -i '/set sidebar_delim/d' "/home/$USERNAME/.muttrc"
  334. sed -i '/set sidebar_sort/d' "/home/$USERNAME/.muttrc"
  335. fi
  336. if [ -f "${temp_restore_dir}/home/$USERNAME/tempbackup/Muttrc" ]; then
  337. cp -f "${temp_restore_dir}/home/$USERNAME/tempbackup/Muttrc" /etc/Muttrc
  338. sed -i '/set sidebar_delim/d' /etc/Muttrc
  339. sed -i '/set sidebar_sort/d' /etc/Muttrc
  340. fi
  341. else
  342. if [ -f ${temp_restore_dir}/.muttrc ]; then
  343. cp -f "${temp_restore_dir}/.muttrc" "/home/$USERNAME/.muttrc"
  344. sed -i '/set sidebar_delim/d' "/home/$USERNAME/.muttrc"
  345. sed -i '/set sidebar_sort/d' "/home/$USERNAME/.muttrc"
  346. fi
  347. if [ -f ${temp_restore_dir}/Muttrc ]; then
  348. cp -f ${temp_restore_dir}/Muttrc /etc/Muttrc
  349. sed -i '/set sidebar_delim/d' /etc/Muttrc
  350. sed -i '/set sidebar_sort/d' /etc/Muttrc
  351. fi
  352. fi
  353. # shellcheck disable=SC2181
  354. if [ ! "$?" = "0" ]; then
  355. rm -rf ${temp_restore_dir}
  356. exit 276
  357. fi
  358. rm -rf ${temp_restore_dir}
  359. fi
  360. fi
  361. done
  362. }
  363. function restore_gpg {
  364. if [[ $RESTORE_APP != 'gpg' ]]; then
  365. return
  366. fi
  367. for d in $SERVER_DIRECTORY/backup/gnupg/*/ ; do
  368. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  369. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  370. if [ -d "$SERVER_DIRECTORY/backup/gnupg/$USERNAME" ]; then
  371. if [ ! -d "/home/$USERNAME" ]; then
  372. ${PROJECT_NAME}-adduser "$USERNAME"
  373. fi
  374. echo $"Restoring gnupg settings for $USERNAME"
  375. temp_restore_dir=/root/tempgnupg
  376. restore_directory_from_friend ${temp_restore_dir} "gnupg/$USERNAME"
  377. if [ -d "${temp_restore_dir}/home/$USERNAME/.gnupg" ]; then
  378. cp -r "${temp_restore_dir}/home/$USERNAME/.gnupg" "/home/$USERNAME/"
  379. else
  380. if [ ! -d "/home/$USERNAME/.gnupg" ]; then
  381. mkdir "/home/$USERNAME/.gnupg"
  382. fi
  383. cp -r "${temp_restore_dir}/"* "/home/$USERNAME/.gnupg/"
  384. fi
  385. # shellcheck disable=SC2181
  386. if [ ! "$?" = "0" ]; then
  387. rm -rf ${temp_restore_dir}
  388. exit 276
  389. fi
  390. rm -rf ${temp_restore_dir}
  391. if [[ "$USERNAME" == "$ADMIN_USERNAME" ]]; then
  392. if ! cp -r "/home/$USERNAME/.gnupg" /root; then
  393. exit 283
  394. fi
  395. gpg_set_permissions root
  396. fi
  397. fi
  398. fi
  399. done
  400. }
  401. function restore_procmail {
  402. if [[ $RESTORE_APP != 'all' ]]; then
  403. if [[ $RESTORE_APP != 'procmail' ]]; then
  404. return
  405. fi
  406. fi
  407. for d in $SERVER_DIRECTORY/backup/procmail/*/ ; do
  408. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  409. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  410. if [ -d "$SERVER_DIRECTORY/backup/procmail/$USERNAME" ]; then
  411. if [ ! -d "/home/$USERNAME" ]; then
  412. ${PROJECT_NAME}-adduser "$USERNAME"
  413. fi
  414. echo $"Restoring procmail settings for $USERNAME"
  415. temp_restore_dir=/root/tempprocmail
  416. restore_directory_from_friend ${temp_restore_dir} "procmail/$USERNAME"
  417. if [ -d "${temp_restore_dir}/home/$USERNAME/tempbackup" ]; then
  418. cp -f "${temp_restore_dir}/home/$USERNAME/tempbackup/.procmailrc" "/home/$USERNAME/"
  419. else
  420. cp -f "${temp_restore_dir}/.procmailrc" "/home/$USERNAME/.procmailrc"
  421. fi
  422. # shellcheck disable=SC2181
  423. if [ ! "$?" = "0" ]; then
  424. rm -rf ${temp_restore_dir}
  425. exit 276
  426. fi
  427. rm -rf ${temp_restore_dir}
  428. fi
  429. fi
  430. done
  431. }
  432. function restore_spamassassin {
  433. if [[ $RESTORE_APP != 'all' ]]; then
  434. if [[ $RESTORE_APP != 'spamassassin' ]]; then
  435. return
  436. fi
  437. fi
  438. for d in $SERVER_DIRECTORY/backup/spamassassin/*/ ; do
  439. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  440. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  441. if [ -d "$SERVER_DIRECTORY/backup/spamassassin/$USERNAME" ]; then
  442. if [ ! -d "/home/$USERNAME" ]; then
  443. ${PROJECT_NAME}-adduser "$USERNAME"
  444. fi
  445. echo $"Restoring spamassassin settings for $USERNAME"
  446. temp_restore_dir=/root/tempspamassassin
  447. restore_directory_from_friend $temp_restore_dir "spamassassin/$USERNAME"
  448. if [ -d "$temp_restore_dir/home/$USERNAME" ]; then
  449. cp -rf "$temp_restore_dir/home/$USERNAME/.spamassassin" "/home/$USERNAME/"
  450. else
  451. if [ ! -d "/home/$USERNAME/.spamassassin" ]; then
  452. mkdir "/home/$USERNAME/.spamassassin"
  453. fi
  454. cp -rf $temp_restore_dir/* "/home/$USERNAME/.spamassassin/"
  455. fi
  456. # shellcheck disable=SC2181
  457. if [ ! "$?" = "0" ]; then
  458. rm -rf $temp_restore_dir
  459. exit 276
  460. fi
  461. rm -rf $temp_restore_dir
  462. fi
  463. fi
  464. done
  465. }
  466. function restore_admin_readme {
  467. if [[ $RESTORE_APP != 'all' ]]; then
  468. if [[ $RESTORE_APP != 'readme' ]]; then
  469. return
  470. fi
  471. fi
  472. if [ -d $SERVER_DIRECTORY/backup/readme ]; then
  473. echo $"Restoring README"
  474. temp_restore_dir=/root/tempreadme
  475. restore_directory_from_friend $temp_restore_dir readme
  476. if [ -d "$temp_restore_dir/home/$ADMIN_USERNAME/tempbackup" ]; then
  477. cp -f "$temp_restore_dir/home/$ADMIN_USERNAME/tempbackup/README" "/home/$ADMIN_USERNAME/"
  478. else
  479. cp -f "$temp_restore_dir/README" "/home/$ADMIN_USERNAME/README"
  480. fi
  481. # shellcheck disable=SC2181
  482. if [ ! "$?" = "0" ]; then
  483. rm -rf $temp_restore_dir
  484. exit 276
  485. fi
  486. rm -rf $temp_restore_dir
  487. fi
  488. }
  489. function restore_ssh_keys {
  490. if [[ $RESTORE_APP != 'all' ]]; then
  491. if [[ $RESTORE_APP != 'ssh' ]]; then
  492. return
  493. fi
  494. fi
  495. for d in $SERVER_DIRECTORY/backup/ssh/*/ ; do
  496. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  497. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  498. if [ -d "$SERVER_DIRECTORY/backup/ssh/$USERNAME" ]; then
  499. if [ ! -d "/home/$USERNAME" ]; then
  500. ${PROJECT_NAME}-adduser "$USERNAME"
  501. fi
  502. echo $"Restoring ssh keys for $USERNAME"
  503. temp_restore_dir=/root/tempssh
  504. restore_directory_from_friend $temp_restore_dir "ssh/$USERNAME"
  505. if [ -d "$temp_restore_dir/home/$USERNAME/.ssh" ]; then
  506. cp -r "$temp_restore_dir/home/$USERNAME/.ssh" "/home/$USERNAME/"
  507. else
  508. if [ ! -d "/home/$USERNAME/.ssh" ]; then
  509. mkdir "/home/$USERNAME/.ssh"
  510. fi
  511. cp -r $temp_restore_dir/* "/home/$USERNAME/.ssh/"
  512. fi
  513. # shellcheck disable=SC2181
  514. if [ ! "$?" = "0" ]; then
  515. rm -rf $temp_restore_dir
  516. exit 664
  517. fi
  518. rm -rf $temp_restore_dir
  519. fi
  520. fi
  521. done
  522. }
  523. function restore_user_config {
  524. if [[ $RESTORE_APP != 'all' ]]; then
  525. if [[ $RESTORE_APP != 'userconfig' ]]; then
  526. return
  527. fi
  528. fi
  529. for d in $SERVER_DIRECTORY/backup/config/*/ ; do
  530. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  531. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  532. if [ -d "$SERVER_DIRECTORY/backup/config/$USERNAME" ]; then
  533. if [ ! -d "/home/$USERNAME" ]; then
  534. ${PROJECT_NAME}-adduser "$USERNAME"
  535. fi
  536. echo $"Restoring config files for $USERNAME"
  537. temp_restore_dir=/root/tempconfig
  538. restore_directory_from_friend $temp_restore_dir "config/$USERNAME"
  539. if [ -d "$temp_restore_dir/home/$USERNAME" ]; then
  540. cp -r "$temp_restore_dir/home/$USERNAME/.config" "/home/$USERNAME/"
  541. else
  542. if [ ! -d "/home/$USERNAME/.config" ]; then
  543. mkdir "/home/$USERNAME/.config"
  544. fi
  545. cp -r "$temp_restore_dir/"* "/home/$USERNAME/.config/"
  546. fi
  547. # shellcheck disable=SC2181
  548. if [ ! "$?" = "0" ]; then
  549. rm -rf $temp_restore_dir
  550. exit 664
  551. fi
  552. rm -rf $temp_restore_dir
  553. fi
  554. fi
  555. done
  556. }
  557. function restore_user_monkeysphere {
  558. if [[ $RESTORE_APP != 'all' ]]; then
  559. if [[ $RESTORE_APP != 'usermonkeysphere' ]]; then
  560. return
  561. fi
  562. fi
  563. for d in $SERVER_DIRECTORY/backup/monkeysphere/*/ ; do
  564. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  565. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  566. if [ -d "$SERVER_DIRECTORY/backup/monkeysphere/$USERNAME" ]; then
  567. if [ ! -d "/home/$USERNAME" ]; then
  568. ${PROJECT_NAME}-adduser "$USERNAME"
  569. fi
  570. echo $"Restoring monkeysphere ids for $USERNAME"
  571. temp_restore_dir=/root/tempmonkeysphere
  572. restore_directory_from_friend $temp_restore_dir "monkeysphere/$USERNAME"
  573. if [ -d "$temp_restore_dir/home/$USERNAME/.monkeysphere" ]; then
  574. cp -r "$temp_restore_dir/home/$USERNAME/.monkeysphere" "/home/$USERNAME/"
  575. else
  576. if [ ! -d "/home/$USERNAME/.monkeysphere" ]; then
  577. mkdir "/home/$USERNAME/.monkeysphere"
  578. fi
  579. cp -r $temp_restore_dir/* "/home/$USERNAME/.monkeysphere/"
  580. fi
  581. # shellcheck disable=SC2181
  582. if [ ! "$?" = "0" ]; then
  583. rm -rf $temp_restore_dir
  584. exit 664
  585. fi
  586. rm -rf $temp_restore_dir
  587. fi
  588. fi
  589. # The admin user is the identity certifier
  590. MY_EMAIL_ADDRESS="${ADMIN_USERNAME}@${HOSTNAME}"
  591. read_config_param MY_EMAIL_ADDRESS
  592. MY_GPG_PUBLIC_KEY_ID=$(gpg_pubkey_from_email "$ADMIN_USERNAME" "$MY_EMAIL_ADDRESS")
  593. fpr=$(gpg --with-colons --fingerprint "$MY_GPG_PUBLIC_KEY_ID" | grep fpr | head -n 1 | awk -F ':' '{print $10}')
  594. monkeysphere-authentication add-identity-certifier "$fpr"
  595. monkeysphere-authentication update-users
  596. done
  597. }
  598. function restore_user_fin {
  599. if [[ $RESTORE_APP != 'all' ]]; then
  600. if [[ $RESTORE_APP != 'userfin' ]]; then
  601. return
  602. fi
  603. fi
  604. for d in $SERVER_DIRECTORY/backup/fin/*/ ; do
  605. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  606. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  607. if [ -d "$SERVER_DIRECTORY/backup/fin/$USERNAME" ]; then
  608. if [ ! -d "/home/$USERNAME" ]; then
  609. ${PROJECT_NAME}-adduser "$USERNAME"
  610. fi
  611. echo $"Restoring fin files for $USERNAME"
  612. temp_restore_dir=/root/tempfin
  613. restore_directory_from_friend $temp_restore_dir "fin/$USERNAME"
  614. if [ -d "$temp_restore_dir/home/$USERNAME/.fin" ]; then
  615. cp -r "$temp_restore_dir/home/$USERNAME/.fin" "/home/$USERNAME/"
  616. else
  617. if [ ! -d "/home/$USERNAME/.fin" ]; then
  618. mkdir "/home/$USERNAME/.fin"
  619. fi
  620. cp -r $temp_restore_dir/* "/home/$USERNAME/.fin/"
  621. fi
  622. # shellcheck disable=SC2181
  623. if [ ! "$?" = "0" ]; then
  624. rm -rf $temp_restore_dir
  625. exit 664
  626. fi
  627. rm -rf $temp_restore_dir
  628. fi
  629. fi
  630. done
  631. }
  632. function restore_user_local {
  633. if [[ $RESTORE_APP != 'all' ]]; then
  634. if [[ $RESTORE_APP != 'userlocal' ]]; then
  635. return
  636. fi
  637. fi
  638. for d in $SERVER_DIRECTORY/backup/local/*/ ; do
  639. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  640. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  641. if [ -d "$SERVER_DIRECTORY/backup/local/$USERNAME" ]; then
  642. if [ ! -d "/home/$USERNAME" ]; then
  643. ${PROJECT_NAME}-adduser "$USERNAME"
  644. fi
  645. echo $"Restoring local files for $USERNAME"
  646. temp_restore_dir=/root/templocal
  647. restore_directory_from_friend $temp_restore_dir "local/$USERNAME"
  648. if [ -d "$temp_restore_dir/home/$USERNAME/.local" ]; then
  649. cp -r "$temp_restore_dir/home/$USERNAME/.local" "/home/$USERNAME/"
  650. else
  651. if [ ! -d "/home/$USERNAME/.local" ]; then
  652. mkdir "/home/$USERNAME/.local"
  653. fi
  654. cp -r $temp_restore_dir/* "/home/$USERNAME/.local/"
  655. fi
  656. # shellcheck disable=SC2181
  657. if [ ! "$?" = "0" ]; then
  658. rm -rf $temp_restore_dir
  659. exit 664
  660. fi
  661. rm -rf $temp_restore_dir
  662. fi
  663. fi
  664. done
  665. }
  666. function restore_certs {
  667. if [[ $RESTORE_APP != 'all' ]]; then
  668. if [[ $RESTORE_APP != 'certs' ]]; then
  669. return
  670. fi
  671. fi
  672. if [ -d $SERVER_DIRECTORY/backup/ssl ]; then
  673. echo $"Restoring certificates"
  674. restore_directory_from_friend /root/tempssl ssl
  675. if [ -d /root/tempssl/etc/ssl ]; then
  676. cp -r /root/tempssl/etc/ssl/* /etc/ssl
  677. else
  678. cp -r /root/tempssl/* /etc/ssl/
  679. fi
  680. # shellcheck disable=SC2181
  681. if [ ! "$?" = "0" ]; then
  682. exit 276
  683. fi
  684. rm -rf /root/tempssl
  685. update-ca-certificates
  686. # restore ownership
  687. if [ -f /etc/ssl/private/xmpp.key ]; then
  688. chown prosody:prosody /etc/ssl/private/xmpp.key
  689. chown prosody:prosody /etc/ssl/certs/xmpp.*
  690. fi
  691. if [ -d /etc/dovecot ]; then
  692. chown root:dovecot /etc/ssl/private/dovecot.*
  693. chown root:dovecot /etc/ssl/certs/dovecot.*
  694. fi
  695. if [ -f /etc/ssl/private/exim.key ]; then
  696. cp /etc/ssl/private/exim.key /etc/exim4
  697. cp /etc/ssl/certs/exim.crt /etc/exim4
  698. cp /etc/ssl/certs/exim.dhparam /etc/exim4
  699. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  700. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  701. fi
  702. if [ -f /etc/ssl/private/mumble.key ]; then
  703. if [ -d /var/lib/mumble-server ]; then
  704. cp /etc/ssl/certs/mumble.* /var/lib/mumble-server
  705. cp /etc/ssl/private/mumble.key /var/lib/mumble-server
  706. chown -R mumble-server:mumble-server /var/lib/mumble-server
  707. fi
  708. fi
  709. fi
  710. }
  711. function restore_personal_settings {
  712. if [[ $RESTORE_APP != 'all' ]]; then
  713. if [[ $RESTORE_APP != 'personal' ]]; then
  714. return
  715. fi
  716. fi
  717. for d in $SERVER_DIRECTORY/backup/personal/*/ ; do
  718. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  719. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  720. if [ -d "$SERVER_DIRECTORY/backup/personal/$USERNAME" ]; then
  721. if [ ! -d "/home/$USERNAME" ]; then
  722. ${PROJECT_NAME}-adduser "$USERNAME"
  723. fi
  724. echo $"Restoring personal settings for $USERNAME"
  725. temp_restore_dir=/root/temppersonal
  726. restore_directory_from_friend $temp_restore_dir "personal/$USERNAME"
  727. if [ -d "$temp_restore_dir/home/$USERNAME/personal" ]; then
  728. if [ -d "/home/$USERNAME/personal" ]; then
  729. rm -rf "/home/$USERNAME/personal"
  730. fi
  731. # shellcheck disable=SC2086
  732. mv $temp_restore_dir/home/$USERNAME/personal /home/$USERNAME
  733. else
  734. if [ ! -d "/home/$USERNAME/personal" ]; then
  735. mkdir "/home/$USERNAME/personal"
  736. fi
  737. cp -r $temp_restore_dir/* "/home/$USERNAME/personal/"
  738. fi
  739. # shellcheck disable=SC2181
  740. if [ ! "$?" = "0" ]; then
  741. exit 18437643
  742. fi
  743. rm -rf $temp_restore_dir
  744. fi
  745. fi
  746. done
  747. }
  748. function restore_mailing_list {
  749. if [[ $RESTORE_APP != 'all' ]]; then
  750. if [[ $RESTORE_APP != 'mailinglist' ]]; then
  751. return
  752. fi
  753. fi
  754. if [ -d /var/spool/mlmmj ]; then
  755. echo $"Restoring public mailing list"
  756. temp_restore_dir=/root/tempmailinglist
  757. restore_directory_from_friend $temp_restore_dir mailinglist
  758. if [ -d $temp_restore_dir/root/spool/mlmmj ]; then
  759. cp -r $temp_restore_dir/root/spool/mlmmj/* /var/spool/mlmmj
  760. else
  761. cp -r $temp_restore_dir/* /var/spool/mlmmj/
  762. fi
  763. # shellcheck disable=SC2181
  764. if [ ! "$?" = "0" ]; then
  765. exit 526
  766. fi
  767. rm -rf $temp_restore_dir
  768. fi
  769. }
  770. function restore_email {
  771. if [[ $RESTORE_APP != 'all' ]]; then
  772. if [[ $RESTORE_APP != 'email' ]]; then
  773. return
  774. fi
  775. fi
  776. for d in $SERVER_DIRECTORY/backup/mail/*/ ; do
  777. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  778. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  779. if [ -d "$SERVER_DIRECTORY/backup/mail/$USERNAME" ]; then
  780. if [ ! -d "/home/$USERNAME" ]; then
  781. ${PROJECT_NAME}-adduser "$USERNAME"
  782. fi
  783. echo $"Restoring emails for $USERNAME"
  784. temp_restore_dir=/root/tempmail
  785. restore_directory_from_friend $temp_restore_dir "mail/$USERNAME"
  786. if [ ! -d "/home/$USERNAME/Maildir" ]; then
  787. mkdir "/home/$USERNAME/Maildir"
  788. fi
  789. if [ -d "$temp_restore_dir/root/tempbackupemail/$USERNAME" ]; then
  790. tar -xzvf "$temp_restore_dir/root/tempbackupemail/$USERNAME/maildir.tar.gz" -C /
  791. else
  792. tar -xzvf $temp_restore_dir/maildir.tar.gz -C /
  793. fi
  794. # shellcheck disable=SC2181
  795. if [ ! "$?" = "0" ]; then
  796. exit 927
  797. fi
  798. rm -rf $temp_restore_dir
  799. fi
  800. fi
  801. done
  802. }
  803. # Social key management
  804. # Recover any key fragments and reconstruct the gpg key
  805. ${PROJECT_NAME}-recoverkey -u "${ADMIN_USERNAME}" -l "$BACKUP_LIST"
  806. copy_gpg_keys
  807. gpg_agent_setup root
  808. restore_blocklist
  809. restore_configfiles
  810. restore_passwordstore
  811. restore_mariadb
  812. restore_postgresql
  813. restore_letsencrypt
  814. restore_mutt_settings
  815. restore_gpg
  816. restore_procmail
  817. restore_spamassassin
  818. restore_admin_readme
  819. restore_ssh_keys
  820. restore_user_config
  821. restore_user_monkeysphere
  822. restore_user_fin
  823. restore_user_local
  824. restore_certs
  825. restore_personal_settings
  826. restore_mailing_list
  827. restore_email
  828. restore_apps remote
  829. set_user_permissions
  830. update_default_domain
  831. # ensure that all TLS certificates are pinned
  832. #${PROJECT_NAME}-pin-cert all
  833. echo $"*** Remote restore was successful ***"
  834. exit 0