Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # A minimalistic backup tool for MySQL or files with auto-cleanup after a certain period, on a Ubuntu or Centos based environments.
- # 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
- # For each one of them we generate the md5 checksum.
- #
- # Requirements: md5sum (usually installed) and mysqldump (it comes with MySQL/MariaDB)
- #
- # Feel free to contribute :)
- #
- # (c) mihai kelemen <mihai@webmanage.ro>
- # January 24, 2019
- #
- # A single configuration file (must be placed in /root/backup.d) has the following entries:
- #
- #####################################################################
- ## database= # database name
- ## user= # database user
- ## password='' # database password (make sure that the password is between single quotes)
- ## backup_directory= # absolute directory path for storing the backup
- ## remove_after_number_of_days=30 # number of backup days, until we remove old backups
- ## backup_files=true # create backup for files - boolean true|false
- ## backup_file_day=6 # when the files need to be backed up - day of week: Monday is 0 ... Sunday is 6.
- ## files_path= # absolute path where are the files located
- ####################################################################
- #
- # Set a cronjob to run daily (or how often is necessary)
- # A typical cronjob is to run daily at midnight
- # 0 0 * * * /bin/bash /root/backup.sh > /root/backup.log 2>&1
- for config in /root/backup.d/*.conf;
- do
- # read configuration
- . $config;
- # if we do not have the directory then create it and set directory permission to 600
- if [ ! -d $backup_directory ]; then
- mkdir -p -m600 $backup_directory;
- fi
- # timestamp down to the second, so in this way we can have multiple daily without having the issue of overwritting existing backups
- timestamp=`date +%Y-%m-%d_%H-%M-%S`;
- cd $backup_directory;
- # backup files
- # file name will be saved as files-YYYY-MM-DD_HH_MM_SS.tar.gz
- if [ $(date +%u) -gt $backup_file_day ] && [ $backup_files == 'true' ]; then
- /bin/tar -czpf $backup_directory/files-$timestamp.tar.gz --exclude=files-$timestamp.tar.gz $files_path
- md5sum files-$timestamp.tar.gz > files-$timestamp.md5
- fi
- # create the database backup as dump-YYYY-MM-DD_HH_MM_SS.sql.gz
- /usr/bin/mysqldump -u $user -p$password $database | gzip > $backup_directory/dump-$timestamp.sql.gz
- md5sum dump-$timestamp.sql.gz > dump-$timestamp.md5
- # make some cleanup: remove files older than $remove_after_number_of_days
- find $backup_directory/ -mtime +$remove_after_number_of_days -exec rm {} \;
- done
Add Comment
Please, Sign In to add comment