freedombone-backup-remote 21KB

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