MySQL自動備份指令碼及異地定時FTP
分享個MySQL自動備份指令碼、定時執行設定及Windows自動FTP。
前提環境:MySQL資料庫伺服器開啟vsftpd,並配置合適帳號以便能被內網儲存伺服器FTP
第一步:編寫MySQL自動執行指令碼
#!/bin/bash
# mysql_db_backup.sh: backup mysql databases.
#
# Last updated: Wed Nov 9 07:01:01 CST 2011
# ----------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2011 Andy Yao
# Blog:http://t.qq.com/andy_microblog
# ----------------------------------------------------------------------
# your mysql login information
# db_user is mysql username
# db_passwd is mysql password
# db_host is mysql host
# -----------------------------
db_user="root"
db_passwd="123456"
db_host="192.168.1.11"
# the directory for story your backup file.
backup_dir="/mnt/sdb1/mysql_db_backup"
# date format for backup file (dd-mm-yyyy)
time="$(date +"%Y-%m-%d_%H-%M-%S")"
file_time="$(date +"%Y-%m-%d_%H-%M-%S")"
mysql_backup_path="$backup_dir/$file_time"
mkdir $backup_dir/$file_time
log_path="$backup_dir/$file_time.log.txt"
#------------this log is for monitor ssh status
ssh_log_path="$backup_dir/log.txt"
echo "---------------------" >> $ssh_log_path
date >> $ssh_log_path
echo "-------------------------------------------------------------------------------" >> $log_path
echo "--------------" >> $log_path
echo "--------" >> $log_path
echo "backup mysql db start" >> $log_path
date >> $log_path
echo "---------------------" >> $log_path
#!/bin/bash
cat /dev/null > $backup_dir/mysqlback.txt
connmsg=`mysql -h$db_host -u$db_user -p$db_passwd $db <<EOF
show databases;
exit
EOF`
echo "$connmsg" > $backup_dir/mysqlback.txt
while read line
do
if [ "$line" != "Database" ]; then
#mysqldump -u$user -p$ps "$line" >/share/"$line".sql
echo "--------" >> $log_path
date >> $log_path
echo "$line" >> $log_path
mysqldump -h$db_host -u$db_user -p$db_passwd "$line" --lock-tables=false | gzip -9 > "$mysql_backup_path/$line.$time.sql.gz"
date >> $log_path
echo "--------" >> $log_path
fi
done < $backup_dir/mysqlback.txt
echo "---------------------" >> $log_path
echo "backup mysql db stop" >> $log_path
date >> $log_path
echo "--------" >> $log_path
echo "--------------" >> $log_path
echo "-------------------------------------------------------------------------------" >> $log_path
#------------this log is for monitor ssh status
date >> $ssh_log_path
echo "---------------------" >> $ssh_log_path
ls -l $mysql_backup_path >> $log_path
echo "--------------" >> $log_path
cd $backup_dir
du -s >> $log_path
du -sm >> $log_path
du -sh >> $log_path
echo "--------------" >> $log_path
du -h |sort -rk2 >> $log_path
exit 0;
第二步:定時執行mysql備份指令碼,設定crontab,這個應該不用解釋吧?
[root@localhost /]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
01 7 * * * root /mysql_db_backup/mysql_db_backup_auto_db_filelist.sh
* */1 * * * root ntpdate 203.129.68.14 && hwclock -w
第三步:Windows端自動定時FTP,將下程式碼儲存為bat,並設定計劃任務
@echo off & color 1f & title 自動FTPMYSQL備份檔案
mode con: cols=60 lines=10
echo ==========================================================
echo --
echo --
echo -- ----### 自動FTPMYSQL備份檔案 ###----
echo --
echo --
echo --處理中,請不要手動關閉程式視窗,
echo --
echo --完成後,程式會自動關閉...
set xtime=%time::=%
set xdate=%date%
set copy_path=%xdate:~0,4%-%xdate:~5,2%-%xdate:~8,2%_07-01-01
rem 指定LOG存放路徑
set log_path=c:\bat\log\ftp_mysql_copy.log.txt
echo -------------------------------------- >>%log_path%
echo -------------------- >>%log_path%
date /t >>%log_path% & time /t >>%log_path%
echo --開始------------------ >>%log_path%
cd E:\MYSQL_BACKUP_12
e:
md %copy_path%
cd %copy_path%
echo open 192.168.1.11 >ftp.src
echo username>>ftp.src
echo password>>ftp.src
echo cd /software/mysql_db_backup/%copy_path%/>>ftp.src
echo pwd>>ftp.src
echo ls>>ftp.src
echo prompt>>ftp.src
echo bin>>ftp.src
echo mget *>>ftp.src
echo bye>>ftp.src
ftp -s:ftp.src
del ftp.src
echo --結束------------------ >>%log_path%
date /t >>%log_path% & time /t >>%log_path%
echo -------------------- >>%log_path%
echo -------------------------------------- >>%log_path%
上面的弄完後,你可以開始測試了。
前提環境:MySQL資料庫伺服器開啟vsftpd,並配置合適帳號以便能被內網儲存伺服器FTP
第一步:編寫MySQL自動執行指令碼
#!/bin/bash
# mysql_db_backup.sh: backup mysql databases.
#
# Last updated: Wed Nov 9 07:01:01 CST 2011
# ----------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2011 Andy Yao
# Blog:http://t.qq.com/andy_microblog
# ----------------------------------------------------------------------
# your mysql login information
# db_user is mysql username
# db_passwd is mysql password
# db_host is mysql host
# -----------------------------
db_user="root"
db_passwd="123456"
db_host="192.168.1.11"
# the directory for story your backup file.
backup_dir="/mnt/sdb1/mysql_db_backup"
# date format for backup file (dd-mm-yyyy)
time="$(date +"%Y-%m-%d_%H-%M-%S")"
file_time="$(date +"%Y-%m-%d_%H-%M-%S")"
mysql_backup_path="$backup_dir/$file_time"
mkdir $backup_dir/$file_time
log_path="$backup_dir/$file_time.log.txt"
#------------this log is for monitor ssh status
ssh_log_path="$backup_dir/log.txt"
echo "---------------------" >> $ssh_log_path
date >> $ssh_log_path
echo "-------------------------------------------------------------------------------" >> $log_path
echo "--------------" >> $log_path
echo "--------" >> $log_path
echo "backup mysql db start" >> $log_path
date >> $log_path
echo "---------------------" >> $log_path
#!/bin/bash
cat /dev/null > $backup_dir/mysqlback.txt
connmsg=`mysql -h$db_host -u$db_user -p$db_passwd $db <<EOF
show databases;
exit
EOF`
echo "$connmsg" > $backup_dir/mysqlback.txt
while read line
do
if [ "$line" != "Database" ]; then
#mysqldump -u$user -p$ps "$line" >/share/"$line".sql
echo "--------" >> $log_path
date >> $log_path
echo "$line" >> $log_path
mysqldump -h$db_host -u$db_user -p$db_passwd "$line" --lock-tables=false | gzip -9 > "$mysql_backup_path/$line.$time.sql.gz"
date >> $log_path
echo "--------" >> $log_path
fi
done < $backup_dir/mysqlback.txt
echo "---------------------" >> $log_path
echo "backup mysql db stop" >> $log_path
date >> $log_path
echo "--------" >> $log_path
echo "--------------" >> $log_path
echo "-------------------------------------------------------------------------------" >> $log_path
#------------this log is for monitor ssh status
date >> $ssh_log_path
echo "---------------------" >> $ssh_log_path
ls -l $mysql_backup_path >> $log_path
echo "--------------" >> $log_path
cd $backup_dir
du -s >> $log_path
du -sm >> $log_path
du -sh >> $log_path
echo "--------------" >> $log_path
du -h |sort -rk2 >> $log_path
exit 0;
第二步:定時執行mysql備份指令碼,設定crontab,這個應該不用解釋吧?
[root@localhost /]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
01 7 * * * root /mysql_db_backup/mysql_db_backup_auto_db_filelist.sh
* */1 * * * root ntpdate 203.129.68.14 && hwclock -w
第三步:Windows端自動定時FTP,將下程式碼儲存為bat,並設定計劃任務
@echo off & color 1f & title 自動FTPMYSQL備份檔案
mode con: cols=60 lines=10
echo ==========================================================
echo --
echo --
echo -- ----### 自動FTPMYSQL備份檔案 ###----
echo --
echo --
echo --處理中,請不要手動關閉程式視窗,
echo --
echo --完成後,程式會自動關閉...
set xtime=%time::=%
set xdate=%date%
set copy_path=%xdate:~0,4%-%xdate:~5,2%-%xdate:~8,2%_07-01-01
rem 指定LOG存放路徑
set log_path=c:\bat\log\ftp_mysql_copy.log.txt
echo -------------------------------------- >>%log_path%
echo -------------------- >>%log_path%
date /t >>%log_path% & time /t >>%log_path%
echo --開始------------------ >>%log_path%
cd E:\MYSQL_BACKUP_12
e:
md %copy_path%
cd %copy_path%
echo open 192.168.1.11 >ftp.src
echo username>>ftp.src
echo password>>ftp.src
echo cd /software/mysql_db_backup/%copy_path%/>>ftp.src
echo pwd>>ftp.src
echo ls>>ftp.src
echo prompt>>ftp.src
echo bin>>ftp.src
echo mget *>>ftp.src
echo bye>>ftp.src
ftp -s:ftp.src
del ftp.src
echo --結束------------------ >>%log_path%
date /t >>%log_path% & time /t >>%log_path%
echo -------------------- >>%log_path%
echo -------------------------------------- >>%log_path%
上面的弄完後,你可以開始測試了。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9034054/viewspace-2054723/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 自動定時備份 mysql 資料庫 的 shell 指令碼MySql資料庫指令碼
- Mysql定時備份資料指令碼MySql指令碼
- linux mysql定時備份指令碼記錄LinuxMySql指令碼
- Linux下Mysql定時自動備份LinuxMySql
- 自動備份指令碼指令碼
- MySQL自動備份指令碼30天自動刪除MySql指令碼
- ORACLE常用定時備份指令碼Oracle指令碼
- 異地自動備份INSTALL資料夾
- 【MySQL】Xtrabackup備份及恢復指令碼MySql指令碼
- oracle自動冷備份指令碼Oracle指令碼
- linux實現mysql資料庫每天自動備份定時備份LinuxMySql資料庫
- mysql備份指令碼MySql指令碼
- xtrabackup備份mysql以及建立自動定時任務MySql
- MySQL 定時備份MySql
- Windows環境PgSql自動備份指令碼WindowsSQL指令碼
- shell 備份檔案指令碼+自動清理指令碼
- 【BASIS】HANA備份指令碼,Catalog自動清理指令碼
- 兩套mysql備份指令碼MySql指令碼
- MySQL 定時增量備份MySql
- MYSQL定時備份(一)MySql
- mysql容器的定時備份指令碼,有二種方法,備份好後打包傳送到遠端備份主機MySql指令碼
- Linux自動備份MySQLLinuxMySql
- MySQL中binlog備份指令碼分享MySql指令碼
- 【DATAPUMP】Oracle資料泵定時備份刪除指令碼Oracle指令碼
- 寶塔自動備份網站到FTP空間網站FTP
- MySQL:Windows下分庫備份指令碼MySqlWindows指令碼
- 定時備份MySQL資料庫MySql資料庫
- gitlab自動定時備份檔案,備份失敗傳送郵件Gitlab
- 備份指令碼指令碼
- ManagerDB 備份檔案管理與異地備份
- MYSQL定時任務-定時清除備份資料MySql
- Mysql資料庫自動備份MySql資料庫
- MySQL備份指令碼,應該這麼寫MySql指令碼
- MySQL資料庫備份的shell指令碼MySql資料庫指令碼
- 實戰-MySQL定時增量備份(2)MySql
- ORACLE備份指令碼Oracle指令碼
- rman 備份指令碼指令碼
- Liunx備份mysql資料庫的shell指令碼MySql資料庫指令碼
- 用友U8設定了自動備份,備份失效