freedombone-backup-remote 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. # utilities needed for backup commands
  37. UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
  38. for f in $UTILS_FILES
  39. do
  40. source $f
  41. done
  42. # Temporary location for data to be backed up to other servers
  43. SERVER_DIRECTORY=/root/remotebackup
  44. # get the version of Go being used
  45. GO_VERSION=$(cat /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-go | grep 'GO_VERSION=' | head -n 1 | awk -F '=' '{print $2}')
  46. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  47. ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
  48. ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME}
  49. if [ ! -f /etc/ssl/private/backup.key ]; then
  50. echo $"Creating backup key"
  51. ${PROJECT_NAME}-addcert -h backup --dhkey 2048
  52. fi
  53. if [ ! -f /home/${ADMIN_USERNAME}/backup.list ]; then
  54. exit 1
  55. fi
  56. # MariaDB password
  57. DATABASE_PASSWORD=''
  58. if [ -f /root/dbpass ]; then
  59. DATABASE_PASSWORD=$(cat /root/dbpass)
  60. fi
  61. # local directory where the backup will be made
  62. if [ ! -d $SERVER_DIRECTORY ]; then
  63. mkdir $SERVER_DIRECTORY
  64. fi
  65. if [ ! -d $SERVER_DIRECTORY/backup ]; then
  66. mkdir -p $SERVER_DIRECTORY/backup
  67. fi
  68. # The name of a currently suspended site
  69. # Sites are suspended so that verification should work
  70. SUSPENDED_SITE=
  71. function suspend_site {
  72. # suspends a given website
  73. if [[ $ENABLE_VERIFICATION != "yes" ]]; then
  74. return
  75. fi
  76. SUSPENDED_SITE="$1"
  77. nginx_dissite $SUSPENDED_SITE
  78. service nginx reload
  79. }
  80. function restart_site {
  81. # restarts a given website
  82. if [ ! $SUSPENDED_SITE ]; then
  83. return
  84. fi
  85. nginx_ensite $SUSPENDED_SITE
  86. service nginx reload
  87. SUSPENDED_SITE=
  88. }
  89. function backup_configuration {
  90. echo $"Backing up ${PROJECT_NAME} configuration files"
  91. temp_backup_dir=/root/tempbackupconfig
  92. if [ ! -d $temp_backup_dir ]; then
  93. mkdir -p $temp_backup_dir
  94. fi
  95. cp -f $CONFIG_FILE $temp_backup_dir
  96. cp -f $COMPLETION_FILE $temp_backup_dir
  97. if [ -f $BACKUP_EXTRA_DIRECTORIES ]; then
  98. cp -f $BACKUP_EXTRA_DIRECTORIES $temp_backup_dir
  99. fi
  100. # nginx password hashes
  101. if [ -f /etc/nginx/.htpasswd ]; then
  102. cp -f /etc/nginx/.htpasswd $temp_backup_dir/htpasswd
  103. fi
  104. backup_directory_to_friend $temp_backup_dir config
  105. }
  106. function backup_users {
  107. for d in /home/*/ ; do
  108. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  109. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  110. # personal settings
  111. if [ -d /home/$USERNAME/personal ]; then
  112. echo $"Backing up personal settings for $USERNAME"
  113. backup_directory_to_friend /home/$USERNAME/personal personal/$USERNAME
  114. fi
  115. # gpg keys
  116. if [ -d /home/$USERNAME/.gnupg ]; then
  117. echo $"Backing up gpg keys for $USERNAME"
  118. backup_directory_to_friend /home/$USERNAME/.gnupg gnupg/$USERNAME
  119. fi
  120. # ssh keys
  121. if [ -d /home/$USERNAME/.ssh ]; then
  122. echo $"Backing up ssh keys for $USERNAME"
  123. backup_directory_to_friend /home/$USERNAME/.ssh ssh/$USERNAME
  124. fi
  125. # syncthing files
  126. if [ -d /home/$USERNAME/Sync ]; then
  127. echo $"Backing up syncthing files for $USERNAME"
  128. backup_directory_to_friend /home/$USERNAME/Sync syncthing/$USERNAME
  129. # ensure that device IDs will be backed up as part of user config settings
  130. if [ ! -d /home/$USERNAME/.config/syncthing ]; then
  131. mkdir -p /home/$USERNAME/.config/syncthing
  132. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  133. fi
  134. if [ -f /home/$USERNAME/.syncthing-server-id ]; then
  135. cp /home/$USERNAME/.syncthing-server-id /home/$USERNAME/.config/syncthing
  136. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  137. fi
  138. if [ -f /home/$USERNAME/.syncthingids ]; then
  139. cp /home/$USERNAME/.syncthingids /home/$USERNAME/.config/syncthing
  140. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  141. fi
  142. fi
  143. # config files
  144. if [ -d /home/$USERNAME/.config ]; then
  145. echo $"Backing up config files for $USERNAME"
  146. backup_directory_to_friend /home/$USERNAME/.config config/$USERNAME
  147. fi
  148. # monkeysphere files
  149. if [ -d /home/$USERNAME/.monkeysphere ]; then
  150. echo $"Backing up monkeysphere files for $USERNAME"
  151. backup_directory_to_friend /home/$USERNAME/.monkeysphere monkeysphere/$USERNAME
  152. fi
  153. # fin files
  154. if [ -d /home/$USERNAME/.fin ]; then
  155. echo $"Backing up fin files for $USERNAME"
  156. backup_directory_to_friend /home/$USERNAME/.fin fin/$USERNAME
  157. fi
  158. # local files
  159. if [ -d /home/$USERNAME/.local ]; then
  160. echo $"Backing up local files for $USERNAME"
  161. backup_directory_to_friend /home/$USERNAME/.local local/$USERNAME
  162. fi
  163. # mutt settings
  164. if [ -f /home/$USERNAME/.muttrc ]; then
  165. echo $"Backing up Mutt settings for $USERNAME"
  166. if [ ! -d /home/$USERNAME/tempbackup ]; then
  167. mkdir -p /home/$USERNAME/tempbackup
  168. fi
  169. cp /home/$USERNAME/.muttrc /home/$USERNAME/tempbackup
  170. if [ -f /etc/Muttrc ]; then
  171. cp /etc/Muttrc /home/$USERNAME/tempbackup
  172. fi
  173. backup_directory_to_friend /home/$USERNAME/tempbackup mutt/$USERNAME
  174. fi
  175. # procmail settings
  176. if [ -f /home/$USERNAME/.procmailrc ]; then
  177. echo $"Backing up procmail settings for $USERNAME"
  178. if [ ! -d /home/$USERNAME/tempbackup ]; then
  179. mkdir -p /home/$USERNAME/tempbackup
  180. fi
  181. cp /home/$USERNAME/.procmailrc /home/$USERNAME/tempbackup
  182. backup_directory_to_friend /home/$USERNAME/tempbackup procmail/$USERNAME
  183. fi
  184. # spamassassin settings
  185. if [ -d /home/$USERNAME/.spamassassin ]; then
  186. echo $"Backing up spamassassin settings for $USERNAME"
  187. backup_directory_to_friend /home/$USERNAME/.spamassassin spamassassin/$USERNAME
  188. fi
  189. # email
  190. if [ -d /home/$USERNAME/Maildir ]; then
  191. echo $"Stopping mail server"
  192. systemctl stop exim4
  193. echo $"Creating an email archive"
  194. if [ ! -d /root/backupemail/$USERNAME ]; then
  195. mkdir -p /root/backupemail/$USERNAME
  196. fi
  197. tar -czvf /root/backupemail/$USERNAME/maildir.tar.gz /home/$USERNAME/Maildir
  198. echo $"Restarting mail server"
  199. systemctl start exim4
  200. echo $"Backing up emails for $USERNAME"
  201. backup_directory_to_friend /root/backupemail/$USERNAME mail/$USERNAME
  202. fi
  203. fi
  204. done
  205. }
  206. function backup_letsencrypt {
  207. if [ -d /etc/letsencrypt ]; then
  208. echo $"Backing up Lets Encrypt settings"
  209. backup_directory_to_friend /etc/letsencrypt letsencrypt
  210. fi
  211. }
  212. function backup_tor {
  213. if [ -d /etc/letsencrypt ]; then
  214. echo $"Backing up Tor settings"
  215. backup_directory_to_friend /var/lib/tor tor
  216. fi
  217. }
  218. function backup_certs {
  219. if [ -d /etc/ssl ]; then
  220. echo $"Backing up certificates"
  221. backup_directory_to_friend /etc/ssl ssl
  222. fi
  223. }
  224. function backup_mailing_list {
  225. if [ -d /var/spool/mlmmj ]; then
  226. echo $"Backing up the public mailing list"
  227. backup_directory_to_friend /var/spool/mlmmj mailinglist
  228. fi
  229. }
  230. function backup_web_server {
  231. if [ -d /etc/nginx ]; then
  232. echo $"Backing up web settings"
  233. backup_directory_to_friend /etc/nginx/sites-available web
  234. fi
  235. }
  236. function backup_admin_readme {
  237. if [ -f /home/$ADMIN_USERNAME/README ]; then
  238. echo $"Backing up README"
  239. if [ ! -d /home/$ADMIN_USERNAME/tempbackup ]; then
  240. mkdir -p /home/$ADMIN_USERNAME/tempbackup
  241. fi
  242. cp -f /home/$ADMIN_USERNAME/README /home/$ADMIN_USERNAME/tempbackup
  243. backup_directory_to_friend /home/$ADMIN_USERNAME/tempbackup readme
  244. fi
  245. }
  246. function backup_mariadb {
  247. if [ ${#DATABASE_PASSWORD} -gt 1 ]; then
  248. temp_backup_dir=/root/tempmariadb
  249. if [ ! -d $temp_backup_dir ]; then
  250. mkdir $temp_backup_dir
  251. fi
  252. mysqldump --password=$DATABASE_PASSWORD mysql user > $temp_backup_dir/mysql.sql
  253. if [ ! -s $temp_backup_dir/mysql.sql ]; then
  254. echo $"Unable to backup MariaDB settings"
  255. rm -rf $temp_backup_dir
  256. # Send a warning email
  257. echo $"Unable to export database settings" | mail -s "${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  258. exit 653
  259. fi
  260. echo "$DATABASE_PASSWORD" > $temp_backup_dir/db
  261. chmod 400 $temp_backup_dir/db
  262. backup_directory_to_friend $temp_backup_dir mariadb
  263. fi
  264. }
  265. # Returns the filename of a key share
  266. function get_key_share {
  267. no_of_shares=$1
  268. USERNAME="$2"
  269. REMOTE_DOMAIN="$3"
  270. # Get a share index based on the supplied domain name
  271. # This ensures that the same share is always given to the same domain
  272. sharenumstr=$(md5sum <<< "$REMOTE_DOMAIN")
  273. share_index=$(echo $((0x${sharenumstr%% *} % ${no_of_shares})) | tr -d -)
  274. # get the filename
  275. share_files=(/home/$USERNAME/.gnupg_fragments/keyshare.asc.*)
  276. share_filename=${share_files[share_index]}
  277. echo "$share_filename"
  278. }
  279. function disperse_key_shares {
  280. USERNAME=$1
  281. REMOTE_DOMAIN=$2
  282. REMOTE_SSH_PORT=$3
  283. REMOTE_PASSWORD=$4
  284. REMOTE_SERVER=$5
  285. if [ -d /home/$USERNAME/.gnupg_fragments ]; then
  286. if [ $REMOTE_DOMAIN ]; then
  287. cd /home/$USERNAME/.gnupg_fragments
  288. no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
  289. if (( no_of_shares > 1 )); then
  290. share_filename=$(get_key_share $no_of_shares "$USERNAME" "$REMOTE_DOMAIN")
  291. # create a temp directory containing the share
  292. temp_key_share_dir=/home/$USERNAME/tempkey
  293. temp_key_share_fragments=$temp_key_share_dir/.gnupg_fragments_${USERNAME}
  294. mkdir -p $temp_key_share_fragments
  295. cp $share_filename $temp_key_share_fragments/
  296. # copy the fragments directory to the remote server
  297. /usr/bin/sshpass -p "$REMOTE_PASSWORD" \
  298. scp -r -P $REMOTE_SSH_PORT $temp_key_share_fragments $REMOTE_SERVER
  299. if [ ! "$?" = "0" ]; then
  300. # Send a warning email
  301. echo "Key share to $REMOTE_SERVER failed" | \
  302. mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
  303. else
  304. # Send a confirmation email
  305. echo "Key ${share_filename} shared to $REMOTE_SERVER" | \
  306. mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
  307. fi
  308. # remove the temp file/directory
  309. shred -zu $temp_key_share_fragments/*
  310. rm -rf $temp_key_share_dir
  311. fi
  312. fi
  313. fi
  314. }
  315. function valid_backup_destination {
  316. destination_dir="$1"
  317. is_valid="yes"
  318. if [[ "$destination_dir" == "hubzilla" || \
  319. "$destination_dir" == "hubzilladata" || \
  320. "$destination_dir" == "gogs" || \
  321. "$destination_dir" == "gogsrepos" || \
  322. "$destination_dir" == "gogsssh" || \
  323. "$destination_dir" == "gnusocial" || \
  324. "$destination_dir" == "gnusocialdata" || \
  325. "$destination_dir" == "mariadb" || \
  326. "$destination_dir" == "config" || \
  327. "$destination_dir" == "letsencrypt" || \
  328. "$destination_dir" == "wiki" || \
  329. "$destination_dir" == "wiki2" || \
  330. "$destination_dir" == "xmpp" || \
  331. "$destination_dir" == "ipfs" || \
  332. "$destination_dir" == "dlna" || \
  333. "$destination_dir" == "tox" || \
  334. "$destination_dir" == "ssl" || \
  335. "$destination_dir" == "blog" || \
  336. "$destination_dir" == "syncthing" || \
  337. "$destination_dir" == "syncthingconfig" || \
  338. "$destination_dir" == "syncthingshared" || \
  339. "$destination_dir" == "mailinglist" ]]; then
  340. is_valid="no"
  341. fi
  342. echo $is_valid
  343. }
  344. function backup_extra_directories {
  345. if [ ! -f $BACKUP_EXTRA_DIRECTORIES ]; then
  346. return
  347. fi
  348. echo $"Backing up some additional directories"
  349. while read backup_line
  350. do
  351. backup_dir=$(echo "$backup_line" | awk -F ',' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  352. if [ -d "$backup_dir" ]; then
  353. destination_dir=$(echo "$backup_line" | awk -F ',' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  354. if [[ $(valid_backup_destination "$destination_dir") == "yes" ]]; then
  355. backup_directory_to_friend "$backup_dir" "$destination_dir"
  356. else
  357. echo $"WARNING: The backup directory $destination_dir is already used."
  358. echo $"Choose a different destination name for backing up $backup_dir"
  359. fi
  360. else
  361. echo $"WARNING: Directory $backup_dir does not exist"
  362. fi
  363. done <$BACKUP_EXTRA_DIRECTORIES
  364. }
  365. TEST_MODE="no"
  366. if [[ "$1" == "test" ]]; then
  367. TEST_MODE="yes"
  368. fi
  369. backup_configuration
  370. if [[ $TEST_MODE == "no" ]]; then
  371. backup_users
  372. backup_letsencrypt
  373. backup_tor
  374. backup_web_server
  375. backup_admin_readme
  376. backup_mariadb
  377. backup_certs
  378. backup_mailing_list
  379. backup_apps remote
  380. backup_extra_directories
  381. fi
  382. # For each remote server
  383. while read remote_server
  384. do
  385. # Get the server and its password
  386. # Format is:
  387. # username@domain <port number> /home/username <ssh password>
  388. REMOTE_SERVER=$(echo "${remote_server}" | awk -F ' ' '{print $1}')
  389. if [ $REMOTE_SERVER ]; then
  390. REMOTE_DOMAIN=$(echo "${remote_server}" | awk -F ' ' '{print $1}' | awk -F '@' '{print $2}')
  391. REMOTE_SSH_PORT=$(echo "${remote_server}" | awk -F ' ' '{print $2}')
  392. REMOTE_DIRECTORY=$(echo "${remote_server}" | awk -F ' ' '{print $3}')
  393. REMOTE_PASSWORD=$(echo "${remote_server}" | awk -F ' ' '{print $4}')
  394. NOW=$(date +"%Y-%m-%d %H:%M:%S")
  395. REMOTE_SERVER=$REMOTE_SERVER:$REMOTE_DIRECTORY
  396. echo "$NOW Starting backup to $REMOTE_SERVER" >> /var/log/remotebackups.log
  397. # Social key management
  398. for d in /home/*/ ; do
  399. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  400. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  401. disperse_key_shares $USERNAME $REMOTE_DOMAIN $REMOTE_SSH_PORT "$REMOTE_PASSWORD" $REMOTE_SERVER
  402. fi
  403. done
  404. if [[ $TEST_MODE == "yes" ]]; then
  405. echo "rsync -ratlzv --rsh=\"/usr/bin/sshpass -p '$REMOTE_PASSWORD' ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no\" $SERVER_DIRECTORY/backup $REMOTE_SERVER"
  406. fi
  407. rsync -ratlzv --rsh="/usr/bin/sshpass -p \"$REMOTE_PASSWORD\" ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" $SERVER_DIRECTORY/backup $REMOTE_SERVER
  408. if [ ! "$?" = "0" ]; then
  409. echo "$NOW Backup to $REMOTE_SERVER failed" >> /var/log/remotebackups.log
  410. # Send a warning email
  411. echo "Backup to $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  412. else
  413. echo "$NOW Backed up to $REMOTE_SERVER" >> /var/log/remotebackups.log
  414. fi
  415. fi
  416. done < /home/${ADMIN_USERNAME}/backup.list
  417. exit 0