Bestanden uploaden naar 'resources'
meer oude bestanden
This commit is contained in:
101
resources/mysql.sh
Normal file
101
resources/mysql.sh
Normal file
@@ -0,0 +1,101 @@
|
||||
#!/bin/bash
|
||||
#==============================================================================
|
||||
SHELL=/bin/sh
|
||||
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
|
||||
# 0 2 * * * /bin/bash /tmp/backup.sh
|
||||
|
||||
#RSYNC -> sshpass -p 'ictmaatwerk' rsync /tmp/backups/. root@51.68.230.92:/tmp --delete -av --ignore-existing
|
||||
|
||||
#==============================================================================
|
||||
# CUSTOM SETTINGS
|
||||
#==============================================================================
|
||||
|
||||
# directory to put the backup files
|
||||
BACKUP_DIR=/backup
|
||||
|
||||
# MYSQL Parameters
|
||||
MYSQL_UNAME=root
|
||||
MYSQL_PWORD=ictmaatwerk
|
||||
|
||||
# Don't backup databases with these names
|
||||
# Example: starts with mysql (^mysql) or ends with _schema (_schema$)
|
||||
IGNORE_DB="(^mysql|_schema$)"
|
||||
|
||||
# include mysql and mysqldump binaries for cron bash user
|
||||
PATH=$PATH:/usr/local/mysql/bin
|
||||
|
||||
# Number of days to keep backups
|
||||
KEEP_BACKUPS_FOR=30 #days
|
||||
|
||||
#==============================================================================
|
||||
# METHODS
|
||||
#==============================================================================
|
||||
|
||||
# YYYY-MM-DD
|
||||
TIMESTAMP=$(date +%F_%H:%M)
|
||||
|
||||
function delete_old_backups()
|
||||
{
|
||||
# Delete mysql older x days
|
||||
echo "Deleting $BACKUP_DIR/*.sql.gz older than $KEEP_BACKUPS_FOR days"
|
||||
find $BACKUP_DIR -type f -name "*.sql.gz" -mtime +$KEEP_BACKUPS_FOR -exec rm {} \;
|
||||
# Delete html folder older x days
|
||||
echo "Deleting $BACKUP_DIR/*.tar.gz older than $KEEP_BACKUPS_FOR days"
|
||||
find $BACKUP_DIR -type f -name "*.tar.gz" -mtime +$KEEP_BACKUPS_FOR -exec rm {} \;
|
||||
}
|
||||
|
||||
function mysql_login() {
|
||||
local mysql_login="-u $MYSQL_UNAME"
|
||||
if [ -n "$MYSQL_PWORD" ]; then
|
||||
local mysql_login+=" -p$MYSQL_PWORD"
|
||||
fi
|
||||
echo $mysql_login
|
||||
}
|
||||
|
||||
function database_list() {
|
||||
local show_databases_sql="SHOW DATABASES WHERE \`Database\` NOT REGEXP '$IGNORE_DB'"
|
||||
echo $(mysql $(mysql_login) -e "$show_databases_sql"|awk -F " " '{if (NR!=1) print $1}')
|
||||
}
|
||||
|
||||
function echo_status(){
|
||||
printf '\r';
|
||||
printf ' %0.s' {0..100}
|
||||
printf '\r';
|
||||
printf "$1"'\r'
|
||||
}
|
||||
|
||||
function backup_database(){
|
||||
backup_file="$BACKUP_DIR/$TIMESTAMP.$database.sql.gz"
|
||||
output+="$database => $backup_file\n"
|
||||
echo_status "...backing up $count of $total databases: $database"
|
||||
$(mysqldump $(mysql_login) $database | gzip -9 > $backup_file)
|
||||
}
|
||||
|
||||
function backup_databases(){
|
||||
local databases=$(database_list)
|
||||
local total=$(echo $databases | wc -w | xargs)
|
||||
local output=""
|
||||
local count=1
|
||||
for database in $databases; do
|
||||
backup_database
|
||||
local count=$((count+1))
|
||||
done
|
||||
echo -ne $output | column -t
|
||||
}
|
||||
|
||||
function hr(){
|
||||
printf '=%.0s' {1..100}
|
||||
printf "\n"
|
||||
}
|
||||
|
||||
#==============================================================================
|
||||
# RUN SCRIPT
|
||||
#==============================================================================
|
||||
mkdir -p /backup
|
||||
chmod -R 755 /backup
|
||||
# MYSQL DUMP
|
||||
delete_old_backups
|
||||
hr
|
||||
backup_databases
|
||||
hr
|
||||
printf "All backed up!\n\n"
|
||||
Reference in New Issue
Block a user