freedombone-backup-remote 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Backup to remote servers - the web of backups
  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. ENABLE_VERIFICATION="no"
  34. export TEXTDOMAIN=${PROJECT_NAME}-backup-remote
  35. export TEXTDOMAINDIR="/usr/share/locale"
  36. # Temporary location for data to be backed up to other servers
  37. SERVER_DIRECTORY=/root/remotebackup
  38. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  39. ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
  40. ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME}
  41. if [ ! -f /etc/ssl/private/backup.key ]; then
  42. echo $"Creating backup key"
  43. ${PROJECT_NAME}-addcert -h backup --dhkey 2048
  44. fi
  45. if [ ! -f /home/${ADMIN_USERNAME}/backup.list ]; then
  46. exit 1
  47. fi
  48. # MariaDB password
  49. DATABASE_PASSWORD=''
  50. if [ -f /root/dbpass ]; then
  51. DATABASE_PASSWORD=$(cat /root/dbpass)
  52. fi
  53. # local directory where the backup will be made
  54. if [ ! -d $SERVER_DIRECTORY ]; then
  55. mkdir $SERVER_DIRECTORY
  56. fi
  57. if [ ! -d $SERVER_DIRECTORY/backup ]; then
  58. mkdir -p $SERVER_DIRECTORY/backup
  59. fi
  60. # The name of a currently suspended site
  61. # Sites are suspended so that verification should work
  62. SUSPENDED_SITE=
  63. function suspend_site {
  64. # suspends a given website
  65. if [[ $ENABLE_VERIFICATION != "yes" ]]; then
  66. return
  67. fi
  68. SUSPENDED_SITE="$1"
  69. nginx_dissite $SUSPENDED_SITE
  70. service nginx reload
  71. }
  72. function restart_site {
  73. # restarts a given website
  74. if [ ! $SUSPENDED_SITE ]; then
  75. return
  76. fi
  77. nginx_ensite $SUSPENDED_SITE
  78. service nginx reload
  79. SUSPENDED_SITE=
  80. }
  81. function backup_directory_to_friend {
  82. BACKUP_KEY_EXISTS=$(gpg --list-keys "$ADMIN_NAME (backup key)")
  83. if [ ! "$?" = "0" ]; then
  84. echo $"Backup key could not be found"
  85. restart_site
  86. exit 43382
  87. fi
  88. ADMIN_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  89. if [ ! -d $SERVER_DIRECTORY/backup/${2} ]; then
  90. mkdir -p $SERVER_DIRECTORY/backup/${2}
  91. fi
  92. obnam force-lock -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID} ${1}
  93. obnam backup -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID} ${1}
  94. if [[ $ENABLE_VERIFICATION == "yes" ]]; then
  95. obnam verify -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID} ${1}
  96. if [ ! "$?" = "0" ]; then
  97. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  98. shred -zu /root/temp${2}/*
  99. rm -rf /root/temp${2}
  100. fi
  101. # Send a warning email
  102. echo "Unable to verify ${2}" | mail -s "${PROJECT_NAME} backup to friends" ${ADMIN_EMAIL_ADDRESS}
  103. restart_site
  104. exit 953
  105. fi
  106. fi
  107. obnam forget --keep=30d -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID}
  108. if [ ! "$?" = "0" ]; then
  109. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  110. shred -zu /root/temp${2}/*
  111. rm -rf /root/temp${2}
  112. fi
  113. # Send a warning email
  114. echo "Unable to backup ${2}" | mail -s "${PROJECT_NAME} backup to friends" ${ADMIN_EMAIL_ADDRESS}
  115. restart_site
  116. exit 853
  117. fi
  118. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  119. shred -zu /root/temp${2}/*
  120. rm -rf /root/temp${2}
  121. fi
  122. }
  123. function backup_database_to_friend {
  124. if [ ${#DATABASE_PASSWORD} -lt 2 ]; then
  125. echo $"No MariaDB password was given"
  126. restart_site
  127. exit 5783
  128. fi
  129. if [ ! -d $SERVER_DIRECTORY/backup/${1} ]; then
  130. mkdir -p $SERVER_DIRECTORY/backup/${1}
  131. fi
  132. if [ ! -d $SERVER_DIRECTORY/backup/${1}data ]; then
  133. mkdir -p $SERVER_DIRECTORY/backup/${1}data
  134. fi
  135. if [ ! -d /root/temp${1}data ]; then
  136. mkdir -p /root/temp${1}data
  137. fi
  138. echo "Obtaining ${1} database backup"
  139. mysqldump --password=$DATABASE_PASSWORD ${1} > /root/temp${1}data/${1}.sql
  140. if [ ! -s /root/temp${1}data/${1}.sql ]; then
  141. echo $"${1} database could not be saved"
  142. shred -zu /root/temp${1}data/*
  143. rm -rf /root/temp${1}data
  144. # Send a warning email
  145. echo $"Unable to export ${1} database" | mail -s $"${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  146. restart_site
  147. exit 5738
  148. fi
  149. }
  150. function backup_configuration {
  151. echo $"Backing up ${PROJECT_NAME} configuration files"
  152. if [ ! -d /root/tempbackupconfig ]; then
  153. mkdir -p /root/tempbackupconfig
  154. fi
  155. cp -f $CONFIG_FILE /root/tempbackupconfig
  156. cp -f $COMPLETION_FILE /root/tempbackupconfig
  157. if [ -f $BACKUP_EXTRA_DIRECTORIES ]; then
  158. cp -f $BACKUP_EXTRA_DIRECTORIES /root/tempbackupconfig
  159. fi
  160. backup_directory_to_friend /root/tempbackupconfig config
  161. }
  162. function backup_users {
  163. for d in /home/*/ ; do
  164. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  165. if [[ $USERNAME != "git" && $USERNAME != "mirrors" ]]; then
  166. # personal settings
  167. if [ -d /home/$USERNAME/personal ]; then
  168. echo $"Backing up personal settings for $USERNAME"
  169. backup_directory_to_friend /home/$USERNAME/personal personal/$USERNAME
  170. fi
  171. # gpg keys
  172. if [ -d /home/$USERNAME/.gnupg ]; then
  173. echo $"Backing up gpg keys for $USERNAME"
  174. backup_directory_to_friend /home/$USERNAME/.gnupg gnupg/$USERNAME
  175. fi
  176. # ssh keys
  177. if [ -d /home/$USERNAME/.ssh ]; then
  178. echo $"Backing up ssh keys for $USERNAME"
  179. backup_directory_to_friend /home/$USERNAME/.ssh ssh/$USERNAME
  180. fi
  181. # config files
  182. if [ -d /home/$USERNAME/.config ]; then
  183. echo $"Backing up config files for $USERNAME"
  184. backup_directory_to_friend /home/$USERNAME/.config config/$USERNAME
  185. fi
  186. # local files
  187. if [ -d /home/$USERNAME/.local ]; then
  188. echo $"Backing up local files for $USERNAME"
  189. backup_directory_to_friend /home/$USERNAME/.local local/$USERNAME
  190. fi
  191. # mutt settings
  192. if [ -f /home/$USERNAME/.muttrc ]; then
  193. echo $"Backing up Mutt settings for $USERNAME"
  194. if [ ! -d /home/$USERNAME/tempbackup ]; then
  195. mkdir -p /home/$USERNAME/tempbackup
  196. fi
  197. cp /home/$USERNAME/.muttrc /home/$USERNAME/tempbackup
  198. if [ -f /etc/Muttrc ]; then
  199. cp /etc/Muttrc /home/$USERNAME/tempbackup
  200. fi
  201. backup_directory_to_friend /home/$USERNAME/tempbackup mutt/$USERNAME
  202. fi
  203. # procmail settings
  204. if [ -f /home/$USERNAME/.procmailrc ]; then
  205. echo $"Backing up procmail settings for $USERNAME"
  206. if [ ! -d /home/$USERNAME/tempbackup ]; then
  207. mkdir -p /home/$USERNAME/tempbackup
  208. fi
  209. cp /home/$USERNAME/.procmailrc /home/$USERNAME/tempbackup
  210. backup_directory_to_friend /home/$USERNAME/tempbackup procmail/$USERNAME
  211. fi
  212. # spamassassin settings
  213. if [ -d /home/$USERNAME/.spamassassin ]; then
  214. echo $"Backing up spamassassin settings for $USERNAME"
  215. backup_directory_to_friend /home/$USERNAME/.spamassassin spamassassin/$USERNAME
  216. fi
  217. # email
  218. if [ -d /home/$USERNAME/Maildir ]; then
  219. echo $"Creating an email archive"
  220. if [ ! -d /root/backupemail/$USERNAME ]; then
  221. mkdir -p /root/backupemail/$USERNAME
  222. fi
  223. tar -czvf /root/backupemail/$USERNAME/maildir.tar.gz /home/$USERNAME/Maildir
  224. echo $"Backing up emails for $USERNAME"
  225. backup_directory_to_friend /root/backupemail/$USERNAME mail/$USERNAME
  226. fi
  227. fi
  228. done
  229. }
  230. function backup_letsencrypt {
  231. if [ -d /etc/letsencrypt ]; then
  232. echo $"Backing up Lets Encrypt settings"
  233. backup_directory_to_friend /etc/letsencrypt letsencrypt
  234. fi
  235. }
  236. function backup_tor {
  237. if [ -d /etc/letsencrypt ]; then
  238. echo $"Backing up Tor settings"
  239. backup_directory_to_friend /var/lib/tor tor
  240. fi
  241. }
  242. function backup_rss_reader {
  243. if grep -q "RSS reader domain" $COMPLETION_FILE; then
  244. RSS_READER_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "RSS reader domain" | awk -F ':' '{print $2}')
  245. if [ -d /etc/share/tt-rss ]; then
  246. suspend_site ${RSS_READER_DOMAIN_NAME}
  247. backup_database_to_friend ttrss
  248. backup_directory_to_friend /root/tempttrssdata ttrssdata
  249. echo $"Backing up RSS reader installation"
  250. backup_directory_to_friend /etc/share/tt-rss ttrss
  251. restart_site
  252. else
  253. echo $"RSS reader domain specified but not found in /etc/share/ttrss}"
  254. fi
  255. fi
  256. }
  257. function backup_gnusocial {
  258. if grep -q "GNU Social domain" $COMPLETION_FILE; then
  259. MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
  260. if [ -d /var/www/${MICROBLOG_DOMAIN_NAME}/htdocs ]; then
  261. suspend_site ${MICROBLOG_DOMAIN_NAME}
  262. backup_database_to_friend gnusocial
  263. backup_directory_to_friend /root/tempgnusocialdata gnusocialdata
  264. echo $"Backing up GNU social installation"
  265. backup_directory_to_friend /var/www/${MICROBLOG_DOMAIN_NAME}/htdocs gnusocial
  266. restart_site
  267. else
  268. echo $"GNU Social domain specified but not found in /var/www/${MICROBLOG_DOMAIN_NAME}"
  269. fi
  270. fi
  271. }
  272. function backup_hubzilla {
  273. if grep -q "Hubzilla domain" $COMPLETION_FILE; then
  274. HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
  275. if [ -d /var/www/${HUBZILLA_DOMAIN_NAME} ]; then
  276. suspend_site ${HUBZILLA_DOMAIN_NAME}
  277. backup_database_to_friend hubzilla
  278. backup_directory_to_friend /root/temphubzilladata hubzilladata
  279. echo "Backing up Hubzilla installation"
  280. backup_directory_to_friend /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs hubzilla
  281. restart_site
  282. else
  283. echo $"Hubzilla domain specified but not found in /var/www/${HUBZILLA_DOMAIN_NAME}"
  284. exit 2578
  285. fi
  286. fi
  287. }
  288. function backup_owncloud {
  289. if [ -d /etc/owncloud ]; then
  290. OWNCLOUD_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Owncloud domain" | awk -F ':' '{print $2}')
  291. suspend_site ${OWNCLOUD_DOMAIN_NAME}
  292. backup_database_to_friend owncloud
  293. backup_directory_to_friend /root/tempownclouddata ownclouddata
  294. echo $"Backing up Owncloud data"
  295. backup_directory_to_friend /var/lib/owncloud owncloud
  296. backup_directory_to_friend /etc/owncloud owncloud2
  297. restart_site
  298. fi
  299. }
  300. function backup_gogs {
  301. if [ -d /home/git/go/src/github.com/gogits ]; then
  302. GIT_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
  303. suspend_site ${GIT_DOMAIN_NAME}
  304. backup_database_to_friend gogs
  305. backup_directory_to_friend /root/tempgogsdata gogsdata
  306. echo $"Obtaining Gogs settings backup"
  307. backup_directory_to_friend /home/git/go/src/github.com/gogits/gogs/custom gogs
  308. echo $"Obtaining Gogs repos backup"
  309. mv /home/git/gogs-repositories/*.git /home/git/gogs-repositories/bob
  310. backup_directory_to_friend /home/git/gogs-repositories gogsrepos
  311. echo $"Obtaining Gogs authorized_keys backup"
  312. backup_directory_to_friend /home/git/.ssh gogsssh
  313. restart_site
  314. fi
  315. }
  316. function backup_wiki {
  317. if [ -d /etc/dokuwiki ]; then
  318. echo $"Backing up wiki"
  319. backup_directory_to_friend /var/lib/dokuwiki wiki
  320. backup_directory_to_friend /etc/dokuwiki wiki2
  321. fi
  322. }
  323. function backup_blog {
  324. if grep -q "Blog domain" $COMPLETION_FILE; then
  325. FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
  326. if [ -d /var/www/${FULLBLOG_DOMAIN_NAME} ]; then
  327. echo $"Backing up blog"
  328. backup_directory_to_friend /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs blog
  329. else
  330. echo $"Blog domain specified but not found in /var/www/${FULLBLOG_DOMAIN_NAME}"
  331. exit 2578
  332. fi
  333. fi
  334. }
  335. function backup_certs {
  336. if [ -d /etc/ssl ]; then
  337. echo $"Backing up certificates"
  338. backup_directory_to_friend /etc/ssl ssl
  339. fi
  340. }
  341. function backup_mailing_list {
  342. if [ -d /var/spool/mlmmj ]; then
  343. echo $"Backing up the public mailing list"
  344. backup_directory_to_friend /var/spool/mlmmj mailinglist
  345. fi
  346. }
  347. function backup_xmpp {
  348. if [ -d /var/lib/prosody ]; then
  349. echo $"Backing up the XMPP settings"
  350. backup_directory_to_friend /var/lib/prosody xmpp
  351. fi
  352. }
  353. function backup_web_server {
  354. if [ -d /etc/nginx ]; then
  355. echo $"Backing up web settings"
  356. backup_directory_to_friend /etc/nginx/sites-available web
  357. fi
  358. }
  359. function backup_admin_readme {
  360. if [ -f /home/$ADMIN_USERNAME/README ]; then
  361. echo $"Backing up README"
  362. if [ ! -d /home/$ADMIN_USERNAME/tempbackup ]; then
  363. mkdir -p /home/$ADMIN_USERNAME/tempbackup
  364. fi
  365. cp -f /home/$ADMIN_USERNAME/README /home/$ADMIN_USERNAME/tempbackup
  366. backup_directory_to_friend /home/$ADMIN_USERNAME/tempbackup readme
  367. fi
  368. }
  369. function backup_ipfs {
  370. if [ -d /home/$ADMIN_USERNAME/.ipfs ]; then
  371. echo $"Backing up IPFS"
  372. backup_directory_to_friend /home/$ADMIN_USERNAME/.ipfs ipfs
  373. fi
  374. }
  375. function backup_dlna {
  376. if [ -d /var/cache/minidlna ]; then
  377. echo $"Backing up DLNA cache"
  378. backup_directory_to_friend /var/cache/minidlna dlna
  379. fi
  380. }
  381. function backup_voip {
  382. if [ -f /etc/mumble-server.ini ]; then
  383. echo $"Backing up VoIP settings"
  384. if [ ! -d /root/tempvoipbackup ]; then
  385. mkdir -p /root/tempvoipbackup
  386. fi
  387. cp -f /etc/mumble-server.ini /root/tempvoipbackup
  388. cp -f /var/lib/mumble-server/mumble-server.sqlite /root/tempvoipbackup
  389. cp -f /etc/sipwitch.conf /root/tempvoipbackup
  390. backup_directory_to_friend /root/tempvoipbackup voip
  391. fi
  392. }
  393. function backup_tox {
  394. if [ -d /var/lib/tox-bootstrapd ]; then
  395. echo "Backing up Tox node settings"
  396. if [ -d /var/lib/tox-bootstrapd/Maildir ]; then
  397. rm -rf /var/lib/tox-bootstrapd/Maildir
  398. fi
  399. cp /etc/tox-bootstrapd.conf /var/lib/tox-bootstrapd
  400. backup_directory_to_friend /var/lib/tox-bootstrapd tox
  401. fi
  402. }
  403. function backup_mariadb {
  404. if [ ${#DATABASE_PASSWORD} -gt 1 ]; then
  405. if [ ! -d /root/tempmariadb ]; then
  406. mkdir /root/tempmariadb
  407. fi
  408. mysqldump --password=$DATABASE_PASSWORD mysql user > /root/tempmariadb/mysql.sql
  409. if [ ! -s /root/tempmariadb/mysql.sql ]; then
  410. echo $"Unable to backup MariaDB settings"
  411. rm -rf /root/tempmariadb
  412. # Send a warning email
  413. echo $"Unable to export database settings" | mail -s "${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  414. exit 653
  415. fi
  416. echo "$DATABASE_PASSWORD" > /root/tempmariadb/db
  417. chmod 400 /root/tempmariadb/db
  418. backup_directory_to_friend /root/tempmariadb mariadb
  419. fi
  420. }
  421. # Returns the filename of a key share
  422. function get_key_share {
  423. no_of_shares=$1
  424. USERNAME="$2"
  425. REMOTE_DOMAIN="$3"
  426. # Get a share index based on the supplied domain name
  427. # This ensures that the same share is always given to the same domain
  428. sharenumstr=$(md5sum <<< "$REMOTE_DOMAIN")
  429. share_index=$(echo $((0x${sharenumstr%% *} % ${no_of_shares})) | tr -d -)
  430. # get the filename
  431. share_files=(/home/$USERNAME/.gnupg_fragments/keyshare.asc.*)
  432. share_filename=${share_files[share_index]}
  433. echo "$share_filename"
  434. }
  435. function disperse_key_shares {
  436. USERNAME=$1
  437. REMOTE_DOMAIN=$2
  438. REMOTE_SSH_PORT=$3
  439. REMOTE_PASSWORD=$4
  440. REMOTE_SERVER=$5
  441. if [ -d /home/$USERNAME/.gnupg_fragments ]; then
  442. if [ $REMOTE_DOMAIN ]; then
  443. cd /home/$USERNAME/.gnupg_fragments
  444. no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
  445. if (( no_of_shares > 1 )); then
  446. share_filename=$(get_key_share $no_of_shares "$USERNAME" "$REMOTE_DOMAIN")
  447. # create a temp directory containing the share
  448. temp_key_share_dir=/home/$USERNAME/tempkey
  449. temp_key_share_fragments=$temp_key_share_dir/.gnupg_fragments_${USERNAME}
  450. mkdir -p $temp_key_share_fragments
  451. cp $share_filename $temp_key_share_fragments/
  452. # copy the fragments directory to the remote server
  453. /usr/bin/sshpass -p "$REMOTE_PASSWORD" \
  454. scp -r -P $REMOTE_SSH_PORT $temp_key_share_fragments $REMOTE_SERVER
  455. if [ ! "$?" = "0" ]; then
  456. # Send a warning email
  457. echo "Key share to $REMOTE_SERVER failed" | \
  458. mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
  459. else
  460. # Send a confirmation email
  461. echo "Key ${share_filename} shared to $REMOTE_SERVER" | \
  462. mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
  463. fi
  464. # remove the temp file/directory
  465. shred -zu $temp_key_share_fragments/*
  466. rm -rf $temp_key_share_dir
  467. fi
  468. fi
  469. fi
  470. }
  471. function valid_backup_destination {
  472. destination_dir="$1"
  473. is_valid="yes"
  474. if [[ "$destination_dir" == "hubzilla" || \
  475. "$destination_dir" == "hubzilladata" || \
  476. "$destination_dir" == "gogs" || \
  477. "$destination_dir" == "gogsrepos" || \
  478. "$destination_dir" == "gogsssh" || \
  479. "$destination_dir" == "gnusocial" || \
  480. "$destination_dir" == "gnusocialdata" || \
  481. "$destination_dir" == "mariadb" || \
  482. "$destination_dir" == "config" || \
  483. "$destination_dir" == "letsencrypt" || \
  484. "$destination_dir" == "wiki" || \
  485. "$destination_dir" == "wiki2" || \
  486. "$destination_dir" == "xmpp" || \
  487. "$destination_dir" == "ipfs" || \
  488. "$destination_dir" == "dlna" || \
  489. "$destination_dir" == "tox" || \
  490. "$destination_dir" == "ssl" || \
  491. "$destination_dir" == "blog" || \
  492. "$destination_dir" == "owncloud" || \
  493. "$destination_dir" == "owncloud2" || \
  494. "$destination_dir" == "ownclouddata" || \
  495. "$destination_dir" == "mailinglist" ]]; then
  496. is_valid="no"
  497. fi
  498. echo $is_valid
  499. }
  500. function backup_extra_directories {
  501. if [ ! -f $BACKUP_EXTRA_DIRECTORIES ]; then
  502. return
  503. fi
  504. echo $"Backing up some additional directories"
  505. while read backup_line
  506. do
  507. backup_dir=$(echo "$backup_line" | awk -F ',' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  508. if [ -d "$backup_dir" ]; then
  509. destination_dir=$(echo "$backup_line" | awk -F ',' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  510. if [[ $(valid_backup_destination "$destination_dir") == "yes" ]]; then
  511. backup_directory_to_friend "$backup_dir" "$destination_dir"
  512. else
  513. echo $"WARNING: The backup directory $destination_dir is already used."
  514. echo $"Choose a different destination name for backing up $backup_dir"
  515. fi
  516. else
  517. echo $"WARNING: Directory $backup_dir does not exist"
  518. fi
  519. done <$BACKUP_EXTRA_DIRECTORIES
  520. }
  521. TEST_MODE="no"
  522. if [[ "$1" == "test" ]]; then
  523. TEST_MODE="yes"
  524. fi
  525. backup_configuration
  526. if [[ $TEST_MODE == "no" ]]; then
  527. backup_users
  528. backup_letsencrypt
  529. backup_tor
  530. backup_gnusocial
  531. backup_rss_reader
  532. backup_hubzilla
  533. backup_owncloud
  534. backup_gogs
  535. backup_wiki
  536. backup_blog
  537. backup_certs
  538. backup_mailing_list
  539. backup_xmpp
  540. backup_web_server
  541. backup_admin_readme
  542. backup_ipfs
  543. backup_dlna
  544. backup_voip
  545. backup_tox
  546. backup_mariadb
  547. backup_extra_directories
  548. fi
  549. # For each remote server
  550. while read remote_server
  551. do
  552. # Get the server and its password
  553. # Format is:
  554. # username@domain <port number> /home/username <ssh password>
  555. REMOTE_SERVER=$(echo "${remote_server}" | awk -F ' ' '{print $1}')
  556. if [ $REMOTE_SERVER ]; then
  557. REMOTE_DOMAIN=$(echo "${remote_server}" | awk -F ' ' '{print $1}' | awk -F '@' '{print $2}')
  558. REMOTE_SSH_PORT=$(echo "${remote_server}" | awk -F ' ' '{print $2}')
  559. REMOTE_DIRECTORY=$(echo "${remote_server}" | awk -F ' ' '{print $3}')
  560. REMOTE_PASSWORD=$(echo "${remote_server}" | awk -F ' ' '{print $4}')
  561. NOW=$(date +"%Y-%m-%d %H:%M:%S")
  562. REMOTE_SERVER=$REMOTE_SERVER:$REMOTE_DIRECTORY
  563. echo "$NOW Starting backup to $REMOTE_SERVER" >> /var/log/remotebackups.log
  564. # Social key management
  565. for d in /home/*/ ; do
  566. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  567. if [[ $USERNAME != "git" && $USERNAME != "mirrors" ]]; then
  568. disperse_key_shares $USERNAME $REMOTE_DOMAIN $REMOTE_SSH_PORT "$REMOTE_PASSWORD" $REMOTE_SERVER
  569. fi
  570. done
  571. if [[ $TEST_MODE == "yes" ]]; then
  572. echo "rsync -ratlzv --rsh=\"/usr/bin/sshpass -p '$REMOTE_PASSWORD' ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no\" $SERVER_DIRECTORY/backup $REMOTE_SERVER"
  573. fi
  574. rsync -ratlzv --rsh="/usr/bin/sshpass -p \"$REMOTE_PASSWORD\" ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" $SERVER_DIRECTORY/backup $REMOTE_SERVER
  575. if [ ! "$?" = "0" ]; then
  576. echo "$NOW Backup to $REMOTE_SERVER failed" >> /var/log/remotebackups.log
  577. # Send a warning email
  578. echo "Backup to $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  579. else
  580. echo "$NOW Backed up to $REMOTE_SERVER" >> /var/log/remotebackups.log
  581. fi
  582. fi
  583. done < /home/${ADMIN_USERNAME}/backup.list
  584. exit 0