backup-server 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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:\tTienes que ser 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. exit 0
  39. #Data
  40. mysqluser="root"
  41. mysqlpass="mysqlpasswd"
  42. pass7z="7zencryptionpasswd"
  43. backupDir=/usr/share/nginx/backup
  44. backupName=backup-castanedo.es-$(date +"%Y-%m-%d")-$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 6 | head -n 1).7z
  45. backupOutput=$backupDir/$backupName
  46. deleteDays="+15"
  47. #Destination file
  48. printf "Archivo Backup:\t%s\n" $backupOutput
  49. #Copy webpages code (except backup and main/public)
  50. printf "Comprimiendo:\t/usr/share/nginx\n"
  51. #cd /usr/share/nginx
  52. tempfile=$(mktemp -t exclude-XXX)
  53. echo "nginx/backup" > $tempfile
  54. echo "nginx/main/public" >> $tempfile
  55. 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /usr/share/nginx -x@$tempfile > /dev/null
  56. if [ $? != 0 ];then
  57. printf "WARNING:\tError copiando paginas web (Ejecucion continua).\n"
  58. fi
  59. rm $tempfile
  60. #Copy MySQL databases (mysqldump)
  61. lista=$(mysql -u $mysqluser -p$mysqlpass -e "show DATABASES;")
  62. #Parse databases expect information_schema, mysql & performance_schema
  63. for database in $lista; do
  64. valid=true
  65. for excep in Database information_schema performance_schema; do
  66. if [ $database = $excep ]; then
  67. valid=false
  68. break
  69. fi
  70. done
  71. if [ $valid = true ]; then
  72. printf "Comprimiendo MySQL database:\t%s\n" $database.sql
  73. mysqldump -u $mysqluser -p$mysqlpass $database | 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput -simysql/$database.sql > /dev/null
  74. if [ $? != 0 ];then
  75. printf "WARNING:\tError copiando database (%s) (Ejecucion continua).\n" $database
  76. fi
  77. fi
  78. done
  79. #Copy nginx configuration (sites-available)
  80. printf "Comprimiendo:\t/etc/nginx/sites-available\n"
  81. 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /etc/nginx/sites-available > /dev/null
  82. if [ $? != 0 ];then
  83. printf "WARNING:\tError copiando paginas web (Ejecucion continua).\n"
  84. fi
  85. #Copy Email (this could be heavy in the future)
  86. printf "Comprimiendo:\t%s\n" /var/spool/mail
  87. 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /var/spool/mail > /dev/null
  88. if [ $? != 0 ];then
  89. printf "WARNING:\tError copiando emails (Ejecucion continua).\n"
  90. fi
  91. #Copy Certificates (LetsEncrypt)
  92. printf "Comprimiendo:\t%s\n" /etc/letsencrypt
  93. 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /etc/letsencrypt > /dev/null
  94. if [ $? != 0 ];then
  95. printf "WARNING:\tError copiando certificados (Ejecucion continua).\n"
  96. fi
  97. #Copy /home
  98. printf "Comprimiendo:\t%s\n" /home
  99. 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /home > /dev/null
  100. if [ $? != 0 ];then
  101. printf "WARNING:\tError copiando carpeta personal (Ejecucion continua).\n"
  102. fi
  103. #Copy GOGS
  104. printf "Comprimiendo:\t%s\n" /opt/gogs
  105. 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput /opt/gogs > /dev/null
  106. if [ $? != 0 ];then
  107. printf "WARNING:\tError copiando GOGS (Ejecucion continua).\n"
  108. fi
  109. #Permissions
  110. chown www-data:www-data $backupOutput
  111. chmod 640 $backupOutput
  112. #Remove files older than 15 days
  113. printf "Eliminando backups antiguos (+15 dias)\n"
  114. find $backupDir -mindepth 1 -mtime $deleteDays -type f -delete
  115. if [ $? != 0 ];then
  116. printf "WARNING:\tError eliminando backup's antiguos (%s dias)\n" $deleteDays
  117. fi
  118. #End
  119. finalTime=$(date +"%s")
  120. echo "------------------------------------------------"
  121. printf "Backup completado con exito en %s segundos :)\n" $((finalTime-startTime))
  122. echo "------------------------------------------------"