From 9cfa6ae47a1be7361921750795bb0db5d8a41e9e Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Mon, 18 Jan 2016 00:00:03 +0100 Subject: Multiple updates --- Bash/mysql-database-backup.sh | 12 +++---- Bash/virtualhosts-backup.sh | 75 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 Bash/virtualhosts-backup.sh diff --git a/Bash/mysql-database-backup.sh b/Bash/mysql-database-backup.sh index a2df521..ef4e2e1 100644 --- a/Bash/mysql-database-backup.sh +++ b/Bash/mysql-database-backup.sh @@ -1,9 +1,9 @@ #!/bin/bash #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# -# MySQL database backup script [Thomas Lange ] # +# MySQL database backup script [Thomas Lange ] # #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# # # -# This database backup script loop through each database (except the excluded # +# This database backup script goes through each database (except the excluded # # databases in DATABASE_EXCLUDED) and creates a bzip2 compressed backup file. # # # #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# @@ -18,7 +18,7 @@ DATABASE_EXCLUDED="(Database|mysql|information_schema|performance_schema)" #=============================================================================== # Define backup directories and target filename #=============================================================================== -DIRECTORY_ROOT="/mnt/backups/databases/" +DIRECTORY_ROOT="/mnt/data/backups/databases/" DIRECTORY_PATH="$(date +%Y-%m-%d-%Hh%Mm)/" DIRECTORY_FILE="${DIRECTORY_ROOT}${DIRECTORY_PATH}%s.sql.bz2" @@ -35,14 +35,14 @@ if [ ! -d "${DIRECTORY_ROOT}${DIRECTORY_PATH}" ]; then fi #=============================================================================== -# Fetch all databases from local MySQL server +# Fetch all database names from local MySQL server #=============================================================================== DATABASES=`mysql --user="${DATABASE_USERNAME}" --password="${DATABASE_PASSWORD}" --execute="SHOW DATABASES;" | grep -Ev "${DATABASE_EXCLUDED}"` #=============================================================================== -# Loop through all databases and create compressed dump +# Loop through all database names and create compressed database backup #=============================================================================== for database in ${DATABASES}; do - echo "[INFO] Creating compressed database backup for ${database}" + echo "[INFO] Creating compressed backup for database ${database} [...]" mysqldump --lock-all-tables --user="${DATABASE_USERNAME}" --password="${DATABASE_PASSWORD}" "${database}" | bzip2 > $(printf "${DIRECTORY_FILE}" "${database}") done \ No newline at end of file diff --git a/Bash/virtualhosts-backup.sh b/Bash/virtualhosts-backup.sh new file mode 100644 index 0000000..839b199 --- /dev/null +++ b/Bash/virtualhosts-backup.sh @@ -0,0 +1,75 @@ +#!/bin/bash +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# +# Virtualhost backup script [Thomas Lange ] # +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# +# # +# This script goes through the first level in the virtual host root directory # +# and creates for each domain name within a compressed backup file. The script # +# is compatible with a directory structure like the following: # +# # +# |--- bob # +# | |--- bobs-blog.tld # +# | |--- config # +# | |--- htdocs # +# |--- alice # +# | |--- alice-domain.tld # +# | |--- config # +# | |--- htdocs # +# |--- thomas # +# |--- sub1.thomas-blog.tld # +# | |--- config # +# | |--- htdocs # +# |--- sub2.thomas-blog.tld # +# |--- config # +# |--- htdocs # +# # +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# + +#=============================================================================== +# Define backup main directories +#=============================================================================== +DIRECTORY_ROOT="/mnt/data/backups/virtualhosts/" +DIRECTORY_PATH="$(date +%Y-%m-%d-%Hh%Mm)/" +DIRECTORY_FROM="/var/www/" + +#=============================================================================== +# Delete old backups in backup root directory +#=============================================================================== +find "${DIRECTORY_ROOT}" -type d -mtime +14 -exec rm -r {} \; + +#=============================================================================== +# Create backup path directory if not exists +#=============================================================================== +if [ ! -d "${DIRECTORY_ROOT}${DIRECTORY_PATH}" ]; then + mkdir "${DIRECTORY_ROOT}${DIRECTORY_PATH}" +fi + +#=============================================================================== +# Loop through all usernames within the source root directory +#=============================================================================== +for username in $(find ${DIRECTORY_FROM}* -maxdepth 0 -type d -exec basename {} \;); do + + #=============================================================================== + # Define backup sub directories and target filename + #=============================================================================== + DIRECTORY_USER="${DIRECTORY_ROOT}${DIRECTORY_PATH}${username}/" + DIRECTORY_FILE="${DIRECTORY_USER}%s.tar.bz2" + + echo "[INFO] Entering directory: ${DIRECTORY_FROM}${username}/:" + + #=============================================================================== + # Create backup sub path directory if not exists + #=============================================================================== + if [ ! -d "${DIRECTORY_USER}" ]; then + echo "[INFO] Creating backup directory for user $username [...]" + mkdir "${DIRECTORY_USER}" + fi + + #=============================================================================== + # Loop through all virtualhosts within the user directory + #=============================================================================== + for virtualhost in $(find ${DIRECTORY_FROM}${username}/* -maxdepth 0 -type d -exec basename {} \;); do + echo "[INFO] Creating compressed backup for virtualhost $virtualhost [...]" + tar --create --bzip2 --file "$(printf "${DIRECTORY_FILE}" "${virtualhost}")" --directory "${DIRECTORY_FROM}${username}/" "${virtualhost}" + done +done \ No newline at end of file -- cgit v1.2.3