freedombone-backup-remote 19KB

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