Guest User

Untitled

a guest
Jan 28th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 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: md5sum (usually installed) and mysqldump (it comes with MySQL/MariaDB)
  8. #
  9. # Feel free to contribute :)
  10. #
  11. # (c) mihai kelemen <mihai@webmanage.ro>
  12. # January 24, 2019
  13. #
  14. # A single configuration file (must be placed in /root/backup.d) has the following entries:
  15. #
  16. #####################################################################
  17. ## database= # database name
  18. ## user= # database user
  19. ## password='' # database password (make sure that the password is between single quotes)
  20. ## backup_directory= # absolute directory path for storing the backup
  21. ## remove_after_number_of_days=30 # number of backup days, until we remove old backups
  22. ## backup_files=true # create backup for files - boolean true|false
  23. ## backup_file_day=6 # when the files need to be backed up - day of week: Monday is 0 ... Sunday is 6.
  24. ## files_path= # absolute path where are the files located
  25. ####################################################################
  26. #
  27. # Set a cronjob to run daily (or how often is necessary)
  28. # A typical cronjob is to run daily at midnight
  29. # 0 0 * * * /bin/bash /root/backup.sh > /root/backup.log 2>&1
  30.  
  31. for config in /root/backup.d/*.conf;
  32. do
  33. # read configuration
  34. . $config;
  35.  
  36. # if we do not have the directory then create it and set directory permission to 600
  37. if [ ! -d $backup_directory ]; then
  38. mkdir -p -m600 $backup_directory;
  39. fi
  40.  
  41. # timestamp down to the second, so in this way we can have multiple daily without having the issue of overwritting existing backups
  42. timestamp=`date +%Y-%m-%d_%H-%M-%S`;
  43. cd $backup_directory;
  44.  
  45. # backup files
  46. # file name will be saved as files-YYYY-MM-DD_HH_MM_SS.tar.gz
  47. if [ $(date +%u) -gt $backup_file_day ] && [ $backup_files == 'true' ]; then
  48. /bin/tar -czpf $backup_directory/files-$timestamp.tar.gz --exclude=files-$timestamp.tar.gz $files_path
  49. md5sum files-$timestamp.tar.gz > files-$timestamp.md5
  50. fi
  51.  
  52. # create the database backup as dump-YYYY-MM-DD_HH_MM_SS.sql.gz
  53. /usr/bin/mysqldump -u $user -p$password $database | gzip > $backup_directory/dump-$timestamp.sql.gz
  54. md5sum dump-$timestamp.sql.gz > dump-$timestamp.md5
  55.  
  56. # make some cleanup: remove files older than $remove_after_number_of_days
  57. find $backup_directory/ -mtime +$remove_after_number_of_days -exec rm {} \;
  58.  
  59. done
Add Comment
Please, Sign In to add comment