User Tools

Site Tools


linux:backup:database

Backup local Database

#!/bin/sh
 
DB_PW=%%PASSWORD%%
DB_USR=%%USER%%
 
KEEP_DAYS="30"
BACKUP_DIR="/root/db_backup/"
 
 
CURRENT_DATE=$(date +%Y%m%d)
TARGET_DIR="$BACKUP_DIR$CURRENT_DATE"
mkdir -p $TARGET_DIR
 
 
DB_LIST=$(mysql -b -u$DB_USR -p$DB_PW -e 'show databases\G' |awk '/Database:/{print $2}')
for db in $DB_LIST; do
echo '-- Start DB-Backup --'
                dbs=$(basename "$db")
                echo "-- Backup of $dbs --"
                mysqldump --create-options --lock-tables=0 -K -e -u$DB_USR -p$DB_PW $db \
                | gzip > $TARGET_DIR/dump_${db}.sql.gz
done
 
echo '-- End of Backup --'
echo '-- Start Backup Cleanjob --'
 
find $BASEDIR -type d -ctime +$KEEP_DAYS -exec rm -rf {} +
 
echo '-- Done with Cleanjob --'
linux/backup/database.txt · Last modified: 2018/09/05 17:27 by karloff