centos5 6下mysql資料庫定時備份

2021-05-26 18:29:11 字數 2725 閱讀 8923

// 單個資料庫備份

[root@localhost /]# vi mysql-backup.sh

db_user="root"

db_passwd="123456"

db_host="192.168.1.110"

db_name="ecshop"

# the directory for story your backup file.

backup_dir="/backup"

# date format for backup file (dd-mm-yyyy)

time="$(date +"%y-%m-%d-%h-%m-%s")"

# mysql, mysqldump and some other bin's path

mysql="/usr/local/mysql/bin/mysql"

mysqldump="/usr/local/mysql/bin/mysqldump"

gzip="/bin/gzip"

$mysqldump -u $db_user -h $db_host -p$db_passwd $db_name | $gzip -9 > "$backup_dir/$db_name"_"$time.gz"

// 整個資料庫備份

(只保留7天以內的備份資料,不備份資料表中的information_schema 和 performance_schema)

[root@localhost /]# vi mysql-backup.sh

db_user="root"

db_passwd="123456"

db_host="localhost"

# the directory for story your backup file.

backup_dir="/backup/"

# date format for backup file (dd-mm-yyyy)

time="$(date +"%h-%m-%s-%m-%d-%y")"

# mysql, mysqldump and some other bin's path

mysql="/usr/local/mysql/bin/mysql"

mysqldump="/usr/local/mysql/bin/mysqldump"

mkdir="/bin/mkdir"

rm="/bin/rm"

mv="/bin/mv"

gzip="/bin/gzip"

# check the directory for store backup is writeable

test ! -w $backup_dir && echo "error: $backup_dir is un-writeable." && exit 0

# the directory for story the newest backup

test ! -d "$backup_dir/backup.0/" && $mkdir "$backup_dir/backup.0/"

echo "start to backup...";

# get all databases

# don't backup information_schema、performance_schema

all_db="$($mysql -u $db_user -h $db_host -p$db_passwd -bse 'show databases')"

all_db=$;

all_db=$;

for db in $all_db

do$mysqldump -u $db_user -h $db_host -p$db_passwd $db | $gzip -9 > "$backup_dir/backup.0/$time.$db.gz"

done

# delete the oldest backup

test -d "$backup_dir/backup.7/" && $rm -rf "$backup_dir/backup.7"

# rotate backup directory

for int in 6 5 4 3 2 1 0

doif(test -d "$backup_dir"/backup."$int")

then

next_int=`expr $int + 1`

$mv "$backup_dir"/backup."$int" "$backup_dir"/backup."$next_int"

fidone

echo "backup success!"

exit 0;

執行資料庫自動備份指令碼

# chmod 700 mysql-backup.sh                   // 只允許管理員執行此指令碼

# ./mysql-backup.sh

# ls –la /mysql-backup.sh

讓備份指令碼每天自動執行

# crontab –e

# 00 03 * * * /mysql-backup.sh                  // 每天凌晨3點執行

資料庫出現問題後的恢復操作:

可以通過mysql管理工具直接將備份壓塑包裡的資料進行匯入即可

centos 5 6 下MySQL安裝配置過程詳解

安裝和配置過程如下 centos下安裝mysql路徑 root sample cd 切換到主目錄進行安裝 centos下安裝mysql root sample yum y install mysql server 安裝mysql 配置mysql root sample vi etc my.cnf 編...

Centos5, 6下更改系統時間和時區

檢視日期 使用 r 引數會以數字顯示時區 date 選擇時區,用當地的時區檔案覆蓋預設的 cd usr share zoneinfo asia sudo cp shanghai etc localtime 修改時間 sudo date s 20150514 18 07 00 硬體時間操作 檢視硬體時...

在CentOS 5 6上安裝Git教程

首先安裝git依賴的一些包.yum install zlib devel yum install openssl devel yum install perl yum install cpio yum install expat devel yum install gettext devel yum...