backup-server 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/bin/bash
  2. #/###################################################################\
  3. #| Make backup of castanedo.es server compressed with 7Z and |
  4. #| encrypted with $pass7z password. |
  5. #| Help: |
  6. #| -$mysqluser: admin user for MySQL. |
  7. #| -$mysqlpass: admin password form MySQL (Caution: check read |
  8. #| permissions for this file, password save in plain text) |
  9. #| -$pass7z: password used for encrypting 7Z archive. |
  10. #| -$backupDir: directory where 7Z is saved, normally is a folder |
  11. #| served for a web server (with BasicAuth for more security). |
  12. #| -$backupName: 7Z archive name. Prefix-Date-6 alphanumeric |
  13. #| random digits.7z |
  14. #| -$deleteDays: remove 7Z archives older than this days. |
  15. #| |
  16. #| Guzmán Castanedo (guzman@castanedo.es) |
  17. #| March 2017 |
  18. #| Licence: GPL v3.0 -> https://www.gnu.org/licenses/gpl-3.0.en.html |
  19. #\###################################################################/
  20. #Check things (root, 7z, mysql, ...)
  21. startTime=$(date +"%s")
  22. if [ $(id -u) -ne 0 ]; then
  23. printf "ERROR:\tNeed to be root :O\n"
  24. exit 1
  25. fi
  26. if [ ! -x "$(which 7z)" ]; then
  27. printf "ERROR:\t7z Not Installed :O\n"
  28. exit 1
  29. fi
  30. if [ ! -x "$(which mysql)" ]; then
  31. printf "ERROR:\tMySQL Not Installed :O\n"
  32. exit 1
  33. fi
  34. if [ ! -x "$(which mysqldump)" ]; then
  35. printf "ERROR:\tMySQL Not Installed :O\n"
  36. exit 1
  37. fi
  38. #Data
  39. mysqluser="root"
  40. mysqlpass="mysqlpasswd"
  41. pass7z="7zencryptionpasswd"
  42. user="www-data"
  43. group="www-data"
  44. backupDir=/var/www/backup
  45. backupName=backup-castanedo.es-$(date +"%Y-%m-%d")-$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 6 | head -n 1).7z
  46. backupOutput=$backupDir/$backupName
  47. deleteDays="+15"
  48. #Destination file
  49. printf "Archivo Backup:\t%s\n" $backupOutput
  50. #Copy webpages code (except backup and main/public)
  51. printf "Comprimiendo:\t/var/www\n"
  52. #cd /usr/share/nginx
  53. tempfile=$(mktemp -t exclude-XXX)
  54. echo "www/backup" > $tempfile
  55. echo "www/main/public" >> $tempfile
  56. 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /var/www -x@$tempfile > /dev/null
  57. if [ $? != 0 ];then
  58. printf "WARNING:\tError copiando paginas web (Ejecucion continua).\n"
  59. fi
  60. rm $tempfile
  61. #Copy MySQL databases (mysqldump)
  62. lista=$(mysql -u $mysqluser -p$mysqlpass -e "show DATABASES;")
  63. #Parse databases expect information_schema & performance_schema
  64. for database in $lista; do
  65. valid=true
  66. for excep in Database information_schema performance_schema; do
  67. if [ $database = $excep ]; then
  68. valid=false
  69. break
  70. fi
  71. done
  72. if [ $valid = true ]; then
  73. printf "Comprimiendo MySQL database:\t%s\n" $database.sql
  74. mysqldump -u $mysqluser -p$mysqlpass $database | 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput -simysql/$database.sql > /dev/null
  75. if [ $? != 0 ];then
  76. printf "WARNING:\tError copiando database (%s) (Ejecucion continua).\n" $database
  77. fi
  78. fi
  79. done
  80. #Copy nginx configuration (sites-available)
  81. printf "Comprimiendo:\t/etc/nginx/\n"
  82. 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /etc/nginx > /dev/null
  83. if [ $? != 0 ];then
  84. printf "WARNING:\tError copiando paginas web (Ejecucion continua).\n"
  85. fi
  86. #Copy Email (this could be heavy in the future)
  87. printf "Comprimiendo:\t%s\n" /var/mail
  88. #7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /var/mail > /dev/null
  89. tar -c -zf - /var/mail | 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput -simail.tar.gz > /dev/null
  90. if [ $? != 0 ];then
  91. printf "WARNING:\tError copiando emails (Ejecucion continua).\n"
  92. fi
  93. #Copy Certificates (LetsEncrypt)
  94. printf "Comprimiendo:\t%s\n" /etc/letsencrypt
  95. 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /etc/letsencrypt > /dev/null
  96. if [ $? != 0 ];then
  97. printf "WARNING:\tError copiando certificados (Ejecucion continua).\n"
  98. fi
  99. #Copy /home
  100. printf "Comprimiendo:\t%s\n" /home
  101. 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /home > /dev/null
  102. if [ $? != 0 ];then
  103. printf "WARNING:\tError copiando carpeta personal (Ejecucion continua).\n"
  104. fi
  105. #Copy GOGS
  106. printf "Comprimiendo:\t%s\n" /opt/gogs
  107. 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /opt/gogs > /dev/null
  108. if [ $? != 0 ];then
  109. printf "WARNING:\tError copiando GOGS (Ejecucion continua).\n"
  110. fi
  111. #Permissions
  112. chown $user:$group $backupOutput
  113. chmod 640 $backupOutput
  114. #Remove files older than 15 days
  115. printf "Eliminando backups antiguos (+15 dias)\n"
  116. find $backupDir -mindepth 1 -mtime $deleteDays -type f -delete
  117. if [ $? != 0 ];then
  118. printf "WARNING:\tError eliminando backup's antiguos (%s dias)\n" $deleteDays
  119. fi
  120. #End
  121. finalTime=$(date +"%s")
  122. echo "------------------------------------------------"
  123. printf "Backup completado con exito en %s segundos :)\n" $((finalTime-startTime))
  124. echo "------------------------------------------------"