寫教學的最大目的是教會未來的自己

網站備份 shell script 篇 FTP 版本

最近因為工作關係,同時要撰寫一個自動備份的shell script 透過crontab 的排成 定期替網站備份

然而我在今天發現一個令人恐怖的事情,就是我忽略的ftp 的傳輸方式有兩種(一個已經執行半年的腳本….他出錯了…)

一種是專門傳送純文字檔的ASCII 模式和 其他用的binary模式。

這兩種方式的最大差别在於ASCII 是一次一個byte 再傳送,接收端接收到後再重新組合,你可以想像成對方也開個空白檔案,在一個字一個字抄過去,那這有什麼問題呢?就是 再換行符號這種地方,會因為不同OS 的設定不同的情況下,做不同的轉換,這就有可能造成檔案變質的問題,而對於壓縮檔來說…..就真的很慘….。

那用 binary傳送文字檔呢?….定多就是換行的地方掛掉而已XD

 

以下是 經過修正後的腳本

也請大家保留 授權聲明

#!/bin/sh
# Copyright (c) 2012 Chong-Po Liao ([email protected])
#
#  This file is free software: you may copy, redistribute and/or modify it
#  under the terms of the GNU General Public License version 3 as published
#  by the Free Software Foundation.
#
#  This file is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see 
#
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo 'back up start....'
t=$(date +%Y%m%d)
mysqldump -u帳號 -p密碼 資料庫名稱 > 網站位置/DB.sql
tar zcfp 壓縮檔的位置/webBackUp_$t.tar.gz  網站位置

cd 壓縮檔存放的資料夾位置

ftp -in FTPIP << !
user 帳號 密碼
binary
cd 要放的位置
put webBackUp_$t.tar.gz

quit
!

echo 'done..'

Post to Twitter Post to Plurk Post to Facebook Send Gmail

發表迴響

Copyright © 2024. All Rights Reserved.

歡迎光臨
初音