freedombone-backup-remote 15KB

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