Guest User

Untitled

a guest
Feb 4th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # A minimalistic backup tool for MySQL or files with auto-cleanup after a certain period, on a Ubuntu or Centos based environments.
  4. # It can handle multiple projects (websites) located on the server where this tool is present. Add one or more *.conf files in the backup.d directory
  5. # For each one of them we generate the md5 checksum.
  6. #
  7. # Requirements:
  8. # - md5sum (usually present by default)
  9. # - tar (usually present by default)
  10. # - mysqldump (it comes with MySQL/MariaDB)
  11. #
  12. # Defaults:
  13. # - If a project does not have any alert level for running low space a default value of 5 GB is used
  14. # - If no email recipient(s) defined or email is not present as a service, the output is added into the log
  15. #
  16. # Important:
  17. # - On low space the backup process will be stopped
  18. # - On Centos 6 comment out the lines regarding available free space (from line 59 ... 78), because the `df` does not implement the GNU version of coreutils
  19. #
  20. # Feel free to contribute :)
  21. #
  22. # (c) mihai kelemen <mihai@webmanage.ro>
  23. # January 24, 2019
  24. #
  25. # A single configuration file (must be placed in /root/backup.d) has the following entries:
  26. #
  27. #####################################################################
  28. ## database= # database name
  29. ## user= # database user
  30. ## password='' # database password (make sure that the password is between single quotes)
  31. ## backup_directory= # absolute directory path for storing the backup
  32. ## remove_after_number_of_days=30 # number of backup days, until we start removing old backups
  33. ## backup_files=true # create backup for files - boolean true|false
  34. ## backup_file_day=6 # when the files need to be backed up - day of week: Monday is 1 ... Sunday is 7.
  35. ## files_path= # absolute path where are the files located
  36. ## available_free_space_alert=10 # raise a notification when space is lower than 10 GB (always use GB)
  37. ## stop_on_full=false # if true stop on space is full, if false cleanup old backups - boolean true|false
  38. ## email=user@domain.com,user2@domain.com # email(s) to be notified on low space situation
  39. ####################################################################
  40. #
  41. # Set a cronjob to run daily (or how often is necessary)
  42. # A typical cronjob is to run daily at midnight
  43. # 0 0 * * * /bin/bash /root/backup.sh > /root/backup.log 2>&1
  44.  
  45. for config in /root/backup.d/*.conf;
  46. do
  47. # read configuration
  48. . $config;
  49.  
  50. # if we do not have the directory then create it and set directory permission to 600
  51. if [ ! -d $backup_directory ]; then
  52. mkdir -p -m600 $backup_directory;
  53. fi
  54.  
  55. # timestamp down to the second, so in this way we can have multiple daily without having the issue of overwritting existing backups
  56. timestamp=`date +%Y-%m-%d_%H-%M-%S`;
  57. cd $backup_directory;
  58.  
  59. # get the available space available (in bytes)
  60. available_free_space=`df -k --output=avail "$PWD" | tail -n1`;
  61.  
  62. # if no limit was setup, use the 5 GB value
  63. if [ -z "$available_free_space_alert" ]; then
  64. available_free_space_alert=5;
  65. fi
  66.  
  67. if [[ $available_free_space -lt $(($available_free_space_alert * 1024 * 1024)) ]]; then
  68.  
  69. body=`df -h "$PWD"`;
  70.  
  71. if [ ! -z "$email" ] && [ -f `which mail` ]; then
  72. echo "<pre>$body</pre>" | mail -s "$(echo -e "Space is running low on $(hostname)\nContent-Type: text/html")" $email;
  73. fi
  74.  
  75. echo "Space is running low:";
  76. echo $body;
  77. if [ -z "$stop_on_full" ] || [ $stop_on_full = 'true' ]; then
  78. exit 1;
  79. fi
  80.  
  81. while [ $available_free_space -lt $(($available_free_space_alert * 1024 * 1024)) ]; do
  82. rm "$(ls -t | tail -1)";
  83. available_free_space=`df -k --output=avail "$PWD" | tail -n1`;
  84. done
  85.  
  86. fi
  87.  
  88. # backup files
  89. # file name will be saved as files-YYYY-MM-DD_HH_MM_SS.tar.gz
  90. if [ $(date +%u) = $backup_file_day ] && [ $backup_files = 'true' ]; then
  91. /bin/tar -czpf $backup_directory/files-$timestamp.tar.gz --exclude=files-$timestamp.tar.gz $files_path
  92. md5sum files-$timestamp.tar.gz > files-$timestamp.md5
  93. fi
  94.  
  95. # create the database backup as dump-YYYY-MM-DD_HH_MM_SS.sql.gz
  96. /usr/bin/mysqldump -u $user -p$password $database | gzip > $backup_directory/dump-$timestamp.sql.gz
  97. md5sum dump-$timestamp.sql.gz > dump-$timestamp.md5
  98.  
  99. # make some cleanup: remove files older than $remove_after_number_of_days
  100. find $backup_directory/ -mtime +$remove_after_number_of_days -exec rm {} \;
  101.  
  102. done
Add Comment
Please, Sign In to add comment