Guest User

Untitled

a guest
Sep 10th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. #! /bin/bash
  2. # You need to execute this bash file on tmux, to have an easy way to see the output
  3. # then, just execute the script and have a daily report of the status
  4. #
  5. # Default ouput on $path/log.md can be readed by markdown and by a human
  6. #
  7. # Maybe you'll need to do some modifications
  8. # Autor: Teo Gonzalez Calzada @thblckjkr
  9.  
  10. # Global variables
  11. path="/home/$USER/backup" # path destination of restores
  12. dbAuthFile="/var/www/database.ini" # ini file with credentials to open databases
  13.  
  14. # Get database ini passwords and user
  15. username=$(awk -F "=" '/username/ {print $2}' $dbAuthFile | tr -d ' ')
  16. password=$(awk -F "=" '/password/ {print $2}' $dbAuthFile | tr -d ' ')
  17.  
  18. # Get key to crypt
  19. key=$(</var/www/decrypt_key)
  20.  
  21. # Define more things
  22. enctype="-aes-256-cbc" # encryption type for files
  23. mysqldatabases="test test2" # Databases to create a restore on mysql
  24. codepath="/var/www/html/" # Path of code to restore
  25.  
  26. # Checks if the file exists, if not create a log
  27. if [ ! -e "$path/log.md" ] ; then
  28. printf "#Backup restore point#\n" > $path/log.md
  29. printf "Document log sucessfully started, system running"
  30. fi
  31.  
  32. while :
  33. do
  34. printf "###Starting restore###\n\n" >> $path/log.md
  35. printf "Starting restore point at `date`\n"
  36.  
  37. # Variables declaration
  38. fDate=`date +%Y-%m-%d_%H-%M` # Date formatted for today
  39. mysqldumpname="mysql_r$fDate.sql.gz" # Final name of the mysql dump
  40. codedumpname="files_r$fDate.tar.gz" # Final name of the code files restoration
  41. mongodumpname="mongo_r$fDate.tgz" # Final name of the MongoDB dump
  42.  
  43. ## MySQL ##
  44. #Create databases restore point
  45. a=`mysqldump -u $username -p$password --extended-insert --databases $mysqldatabases | gzip > $path/$mysqldumpname`
  46. b=`openssl enc $enctype -in $path/$mysqldumpname -out $path/$mysqldumpname.enc -k $key`
  47. #Add info to the log
  48. printf "GunZiped and encripted SQL Database with name **$mysqldumpname** at *`date`*\n\n" >> $path/log.md
  49. printf "> dump: $a \n" >> $path/log.md
  50. printf "> crypt: $b \n" >> $path/log.md
  51.  
  52. rm -rf $path/$mysqldumpname #remove unencrypted
  53. ## MySQL ##
  54.  
  55. ## MongoDB ##
  56. #Create a MongoDB restoration
  57. g=`mongodump --out $path/mongo`
  58. h=`sudo tar -zcf $path/$mongodumpname $path/mongo`
  59. i=`openssl enc $enctype -in $path/$mongodumpname -out $path/$mongodumpname.enc -k $key`
  60. #Add the info to the log
  61. printf "GunZiped and encripted Mongo database with name **$codedumpname** at *`date`*\n\n" >> $path/log.md
  62. printf "> mongo: $g \n" >> $path/log.md
  63. printf "> tar: $h \n" >> $path/log.md
  64. printf "> crypt: $h \n" >> $path/log.md
  65.  
  66. rm -rf $path/mongo
  67. rm -rf $path/$mongodumpname
  68. ## MongoDB ##
  69.  
  70. ## CODE ##
  71. #Create a code restoration
  72. d=`sudo tar -zcf $path/$codedumpname $codepath --exclude=''`
  73. e=`openssl enc $enctype -in $path/$codedumpname -out $path/$codedumpname.enc -k $key`
  74. #Add the info to the log
  75. printf "GunZiped and encripted Code files with name **$codedumpname** at *`date`*\n\n" >> $path/log.md
  76. printf "> tar: $d \n" >> $path/log.md
  77. printf "> crypt: $e \n" >> $path/log.md
  78.  
  79. rm -rf $path/$codedumpname
  80. ## CODE ##
  81.  
  82. ## CLEAN ##
  83. # Delete code restorations older than 15 days
  84. find $path -name "*.tar.gz.enc" -mtime +15 -exec rm -rf {} \;
  85.  
  86. # Delete mysql from 30 days of more
  87. find $path -name "*.sql.gz.enc" -mtime +30 -exec rm -rf {} \;
  88.  
  89. # Delete mongodb of 30 days or older
  90. find $path -name "*.tgz.enc" -mtime +30 -exec rm -rf {} \;
  91.  
  92. printf "> Removed unencripted files \n\n" >> $path/log.md
  93. ## CLEAN ##
  94.  
  95. ## RE-RUN ##
  96. # Wait's 1/2 day to run again
  97. printf " *Restoration point end at `date`*\n\n --- \n\n" >> $path/log.md
  98. printf "Restoration process ended, sleeping for one day\n"
  99. sleep 12h
  100. done
Add Comment
Please, Sign In to add comment