backup-server 4.6 KB

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