freedombone-backup-remote 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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@freedombone.net>
  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_configfiles {
  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. if [ -f $NODEJS_INSTALLED_APPS_FILE ]; then
  101. cp -f $NODEJS_INSTALLED_APPS_FILE $temp_backup_dir
  102. fi
  103. cp -f $CONFIGURATION_FILE $temp_backup_dir
  104. cp -f $COMPLETION_FILE $temp_backup_dir
  105. if [ -f $BACKUP_EXTRA_DIRECTORIES ]; then
  106. cp -f $BACKUP_EXTRA_DIRECTORIES $temp_backup_dir
  107. fi
  108. # nginx password hashes
  109. if [ -f /etc/nginx/.htpasswd ]; then
  110. cp -f /etc/nginx/.htpasswd $temp_backup_dir/htpasswd
  111. fi
  112. backup_directory_to_friend $temp_backup_dir configfiles
  113. }
  114. function backup_users {
  115. for d in /home/*/ ; do
  116. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  117. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  118. # personal settings
  119. if [ -d /home/$USERNAME/personal ]; then
  120. echo $"Backing up personal settings for $USERNAME"
  121. backup_directory_to_friend /home/$USERNAME/personal personal/$USERNAME
  122. fi
  123. # gpg keys
  124. if [ -d /home/$USERNAME/.gnupg ]; then
  125. echo $"Backing up gpg keys for $USERNAME"
  126. backup_directory_to_friend /home/$USERNAME/.gnupg gnupg/$USERNAME
  127. fi
  128. # ssh keys
  129. if [ -d /home/$USERNAME/.ssh ]; then
  130. echo $"Backing up ssh keys for $USERNAME"
  131. backup_directory_to_friend /home/$USERNAME/.ssh ssh/$USERNAME
  132. fi
  133. # syncthing files
  134. if [ -d /home/$USERNAME/Sync ]; then
  135. echo $"Backing up syncthing files for $USERNAME"
  136. backup_directory_to_friend /home/$USERNAME/Sync syncthing/$USERNAME
  137. # ensure that device IDs will be backed up as part of user config settings
  138. if [ ! -d /home/$USERNAME/.config/syncthing ]; then
  139. mkdir -p /home/$USERNAME/.config/syncthing
  140. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  141. fi
  142. if [ -f /home/$USERNAME/.syncthing-server-id ]; then
  143. cp /home/$USERNAME/.syncthing-server-id /home/$USERNAME/.config/syncthing
  144. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  145. fi
  146. if [ -f /home/$USERNAME/.syncthingids ]; then
  147. cp /home/$USERNAME/.syncthingids /home/$USERNAME/.config/syncthing
  148. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  149. fi
  150. fi
  151. # config files
  152. if [ -d /home/$USERNAME/.config ]; then
  153. echo $"Backing up config files for $USERNAME"
  154. backup_directory_to_friend /home/$USERNAME/.config config/$USERNAME
  155. fi
  156. # monkeysphere files
  157. if [ -d /home/$USERNAME/.monkeysphere ]; then
  158. echo $"Backing up monkeysphere files for $USERNAME"
  159. backup_directory_to_friend /home/$USERNAME/.monkeysphere monkeysphere/$USERNAME
  160. fi
  161. # fin files
  162. if [ -d /home/$USERNAME/.fin ]; then
  163. echo $"Backing up fin files for $USERNAME"
  164. backup_directory_to_friend /home/$USERNAME/.fin fin/$USERNAME
  165. fi
  166. # local files
  167. if [ -d /home/$USERNAME/.local ]; then
  168. echo $"Backing up local files for $USERNAME"
  169. backup_directory_to_friend /home/$USERNAME/.local local/$USERNAME
  170. fi
  171. # mutt settings
  172. if [ -f /home/$USERNAME/.muttrc ]; then
  173. echo $"Backing up Mutt settings for $USERNAME"
  174. if [ ! -d /home/$USERNAME/tempbackup ]; then
  175. mkdir -p /home/$USERNAME/tempbackup
  176. fi
  177. cp /home/$USERNAME/.muttrc /home/$USERNAME/tempbackup
  178. if [ -f /etc/Muttrc ]; then
  179. cp /etc/Muttrc /home/$USERNAME/tempbackup
  180. fi
  181. backup_directory_to_friend /home/$USERNAME/tempbackup mutt/$USERNAME
  182. fi
  183. # procmail settings
  184. if [ -f /home/$USERNAME/.procmailrc ]; then
  185. echo $"Backing up procmail settings for $USERNAME"
  186. if [ ! -d /home/$USERNAME/tempbackup ]; then
  187. mkdir -p /home/$USERNAME/tempbackup
  188. fi
  189. cp /home/$USERNAME/.procmailrc /home/$USERNAME/tempbackup
  190. backup_directory_to_friend /home/$USERNAME/tempbackup procmail/$USERNAME
  191. fi
  192. # spamassassin settings
  193. if [ -d /home/$USERNAME/.spamassassin ]; then
  194. echo $"Backing up spamassassin settings for $USERNAME"
  195. backup_directory_to_friend /home/$USERNAME/.spamassassin spamassassin/$USERNAME
  196. fi
  197. # email
  198. if [ -d /home/$USERNAME/Maildir ]; then
  199. echo $"Stopping mail server"
  200. systemctl stop exim4
  201. echo $"Creating an email archive"
  202. if [ ! -d /root/backupemail/$USERNAME ]; then
  203. mkdir -p /root/backupemail/$USERNAME
  204. fi
  205. tar -czvf /root/backupemail/$USERNAME/maildir.tar.gz /home/$USERNAME/Maildir
  206. echo $"Restarting mail server"
  207. systemctl start exim4
  208. echo $"Backing up emails for $USERNAME"
  209. backup_directory_to_friend /root/backupemail/$USERNAME mail/$USERNAME
  210. fi
  211. fi
  212. done
  213. }
  214. function backup_letsencrypt {
  215. if [ -d /etc/letsencrypt ]; then
  216. echo $"Backing up Lets Encrypt settings"
  217. backup_directory_to_friend /etc/letsencrypt letsencrypt
  218. fi
  219. }
  220. function backup_tor {
  221. if [ -d /etc/letsencrypt ]; then
  222. echo $"Backing up Tor settings"
  223. backup_directory_to_friend /var/lib/tor tor
  224. fi
  225. }
  226. function backup_certs {
  227. if [ -d /etc/ssl ]; then
  228. echo $"Backing up certificates"
  229. backup_directory_to_friend /etc/ssl ssl
  230. fi
  231. }
  232. function backup_mailing_list {
  233. if [ -d /var/spool/mlmmj ]; then
  234. echo $"Backing up the public mailing list"
  235. backup_directory_to_friend /var/spool/mlmmj mailinglist
  236. fi
  237. }
  238. function backup_web_server {
  239. if [ -d /etc/nginx ]; then
  240. echo $"Backing up web settings"
  241. backup_directory_to_friend /etc/nginx/sites-available web
  242. fi
  243. }
  244. function backup_admin_readme {
  245. if [ -f /home/$ADMIN_USERNAME/README ]; then
  246. echo $"Backing up README"
  247. if [ ! -d /home/$ADMIN_USERNAME/tempbackup ]; then
  248. mkdir -p /home/$ADMIN_USERNAME/tempbackup
  249. fi
  250. cp -f /home/$ADMIN_USERNAME/README /home/$ADMIN_USERNAME/tempbackup
  251. backup_directory_to_friend /home/$ADMIN_USERNAME/tempbackup readme
  252. fi
  253. }
  254. function backup_mariadb {
  255. if [ ${#DATABASE_PASSWORD} -gt 1 ]; then
  256. temp_backup_dir=/root/tempmariadb
  257. if [ ! -d $temp_backup_dir ]; then
  258. mkdir $temp_backup_dir
  259. fi
  260. mysqldump --password=$DATABASE_PASSWORD mysql user > $temp_backup_dir/mysql.sql
  261. if [ ! -s $temp_backup_dir/mysql.sql ]; then
  262. echo $"Unable to backup MariaDB settings"
  263. rm -rf $temp_backup_dir
  264. # Send a warning email
  265. echo $"Unable to export database settings" | mail -s "${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  266. exit 653
  267. fi
  268. echo "$DATABASE_PASSWORD" > $temp_backup_dir/db
  269. chmod 400 $temp_backup_dir/db
  270. backup_directory_to_friend $temp_backup_dir mariadb
  271. fi
  272. }
  273. # Returns the filename of a key share
  274. function get_key_share {
  275. no_of_shares=$1
  276. USERNAME="$2"
  277. REMOTE_DOMAIN="$3"
  278. # Get a share index based on the supplied domain name
  279. # This ensures that the same share is always given to the same domain
  280. sharenumstr=$(md5sum <<< "$REMOTE_DOMAIN")
  281. share_index=$(echo $((0x${sharenumstr%% *} % ${no_of_shares})) | tr -d -)
  282. # get the filename
  283. share_files=(/home/$USERNAME/.gnupg_fragments/keyshare.asc.*)
  284. share_filename=${share_files[share_index]}
  285. echo "$share_filename"
  286. }
  287. function disperse_key_shares {
  288. USERNAME=$1
  289. REMOTE_DOMAIN=$2
  290. REMOTE_SSH_PORT=$3
  291. REMOTE_PASSWORD=$4
  292. REMOTE_SERVER=$5
  293. if [ -d /home/$USERNAME/.gnupg_fragments ]; then
  294. if [ $REMOTE_DOMAIN ]; then
  295. cd /home/$USERNAME/.gnupg_fragments
  296. no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
  297. if (( no_of_shares > 1 )); then
  298. share_filename=$(get_key_share $no_of_shares "$USERNAME" "$REMOTE_DOMAIN")
  299. # create a temp directory containing the share
  300. temp_key_share_dir=/home/$USERNAME/tempkey
  301. temp_key_share_fragments=$temp_key_share_dir/.gnupg_fragments_${USERNAME}
  302. mkdir -p $temp_key_share_fragments
  303. cp $share_filename $temp_key_share_fragments/
  304. # copy the fragments directory to the remote server
  305. /usr/bin/sshpass -p "$REMOTE_PASSWORD" \
  306. scp -r -P $REMOTE_SSH_PORT $temp_key_share_fragments $REMOTE_SERVER
  307. if [ ! "$?" = "0" ]; then
  308. # Send a warning email
  309. echo "Key share to $REMOTE_SERVER failed" | \
  310. mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
  311. else
  312. # Send a confirmation email
  313. echo "Key ${share_filename} shared to $REMOTE_SERVER" | \
  314. mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
  315. fi
  316. # remove the temp file/directory
  317. shred -zu $temp_key_share_fragments/*
  318. rm -rf $temp_key_share_dir
  319. fi
  320. fi
  321. fi
  322. }
  323. TEST_MODE="no"
  324. if [[ "$1" == "test" ]]; then
  325. TEST_MODE="yes"
  326. fi
  327. backup_configfiles
  328. if [[ $TEST_MODE == "no" ]]; then
  329. backup_users
  330. backup_letsencrypt
  331. backup_tor
  332. backup_web_server
  333. backup_admin_readme
  334. backup_mariadb
  335. backup_certs
  336. backup_mailing_list
  337. backup_apps remote
  338. backup_extra_directories remote
  339. fi
  340. # For each remote server
  341. while read remote_server
  342. do
  343. # Get the server and its password
  344. # Format is:
  345. # username@domain <port number> /home/username <ssh password>
  346. REMOTE_SERVER=$(echo "${remote_server}" | awk -F ' ' '{print $1}')
  347. if [ $REMOTE_SERVER ]; then
  348. REMOTE_DOMAIN=$(echo "${remote_server}" | awk -F ' ' '{print $1}' | awk -F '@' '{print $2}')
  349. REMOTE_SSH_PORT=$(echo "${remote_server}" | awk -F ' ' '{print $2}')
  350. REMOTE_DIRECTORY=$(echo "${remote_server}" | awk -F ' ' '{print $3}')
  351. REMOTE_PASSWORD=$(echo "${remote_server}" | awk -F ' ' '{print $4}')
  352. NOW=$(date +"%Y-%m-%d %H:%M:%S")
  353. REMOTE_SERVER=$REMOTE_SERVER:$REMOTE_DIRECTORY
  354. echo "$NOW Starting backup to $REMOTE_SERVER" >> /var/log/remotebackups.log
  355. # Social key management
  356. for d in /home/*/ ; do
  357. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  358. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  359. disperse_key_shares $USERNAME $REMOTE_DOMAIN $REMOTE_SSH_PORT "$REMOTE_PASSWORD" $REMOTE_SERVER
  360. fi
  361. done
  362. if [[ $TEST_MODE == "yes" ]]; then
  363. echo "rsync -ratlzv --rsh=\"/usr/bin/sshpass -p '$REMOTE_PASSWORD' ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no\" $SERVER_DIRECTORY/backup $REMOTE_SERVER"
  364. fi
  365. rsync -ratlzv --rsh="/usr/bin/sshpass -p \"$REMOTE_PASSWORD\" ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" $SERVER_DIRECTORY/backup $REMOTE_SERVER
  366. if [ ! "$?" = "0" ]; then
  367. echo "$NOW Backup to $REMOTE_SERVER failed" >> /var/log/remotebackups.log
  368. # Send a warning email
  369. echo "Backup to $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  370. else
  371. echo "$NOW Backed up to $REMOTE_SERVER" >> /var/log/remotebackups.log
  372. fi
  373. fi
  374. done < /home/${ADMIN_USERNAME}/backup.list
  375. exit 0