Last active
February 3, 2019 10:45
-
-
Save mihaikelemen/3d39d4457188a24b42cc0f76edbb047a to your computer and use it in GitHub Desktop.
A minimalistic backup tool for MySQL / MariaDB or files with auto-cleanup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 present by default) | |
# - tar (usually present by default) | |
# - mysqldump (it comes with MySQL/MariaDB) | |
# | |
# Defaults: | |
# - If a project does not have any alert level for running low space a default value of 5 GB is used | |
# - If no email recipient(s) defined or email is not present as a service, the output is added into the log | |
# | |
# Important: | |
# - On low space the backup process will be stopped | |
# - 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 | |
# | |
# Feel free to contribute :) | |
# | |
# (c) mihai kelemen <[email protected]> | |
# 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 start removing 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 1 ... Sunday is 7. | |
## files_path= # absolute path where are the files located | |
## available_free_space_alert=10 # raise a notification when space is lower than 10 GB (always use GB) | |
## [email protected],[email protected] # email(s) to be notified on low space situation | |
#################################################################### | |
# | |
# 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; | |
# get the available space available (in bytes) | |
available_free_space=`df -k --output=avail "$PWD" | tail -n1`; | |
# if no limit was setup, use the 5 GB value | |
if [ -z "$available_free_space_alert" ]; then | |
available_free_space_alert=5; | |
fi | |
if [[ $available_free_space -lt $(($available_free_space_alert * 1024 * 1024)) ]]; then | |
body=`df -h "$PWD"`; | |
if [ ! -z "$email" ] && [ -f `which mail` ]; then | |
echo "<pre>$body</pre>" | mail -s "$(echo -e "Space is running low on $(hostname)\nContent-Type: text/html")" $email; | |
fi | |
echo "Space is running low:"; | |
echo $body; | |
exit 1; | |
fi | |
# backup files | |
# file name will be saved as files-YYYY-MM-DD_HH_MM_SS.tar.gz | |
if [ $(date +%u) = $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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment