Shell Script

Backup Script

1. 개요

MySQL 자동 백업 스크립트

 

2. Source Code

#!/bin/sh
# mysql database backup
#
# Setup cron : /etc/crontab or crontab -e
 
# mysql 경로
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/mysql/bin
 
# Set variables
Today="`date +%Y%m%d`"
mkdir /home/db_backup/$Today
datadir="/home/mysql-data/"
backup_dir="/backup/db/"
mysqluser="root"
mysqlpass="[password]"
 
# Delete file 5 days ago
# deletefile="*$(date +%Y%m%d --date='5 days ago')*"
# rm -f $backup_dir$deletefile
 
# Database별로 백업
for db in $(ls $datadir) ;do
    if [ -d $datadir$db ] ; then
        # mysql root 패스워드가 있는 경우
        mysqldump --single-transaction -u $mysqluser -p$mysqlpass $db > \
            $backup_dir$Today/$db"_"$(date +%Y%m%d_%H%M).sql
    # mysql root 패스워드가 없는 경우
    # mysqldump -u $mysqluser  $db  > \
    # $backup_dir$IP"_"$db"_"$(date +%Y%m%d_%H%M).sql
    fi
done

 

Back To Top