freedombone-backup-remote 15KB

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