如何定時備份windows 2008 Server網站目錄到遠端伺服器
設計理念1.先將要備份的目錄壓縮起來(節省傳輸時間)2.然後再ftp到遠端的伺服器去(建立日期目錄)3.定時自動執行(如每日凌晨三點)首先,3可用Windows附屬應用程式=>系統工具=>排定的工作來完成,我們最後設定即可。1和2則需要寫支DOS batch程式來完成,假設叫做remote_backup_web.bat。底下是該bat檔案的內容
rem ---------------------------------------------------------------------------
rem Backup web application to remote host
rem
rem $Id: remote_backup_web.bat 2015-09-01 14:24:43Z woff $
rem ---------------------------------------------------------------------------
@echo off
rem Find out web server's IP address
ipconfig | findstr "IPv4" > ip1.txt
for /f "tokens=1-2 delims=:" %%i in (ip1.txt) do echo %%j > ip2.txt
for /f "tokens=1-2 delims= " %%i in (ip2.txt) do (set ip=%%i)
del ip1.txt
del ip2.txt
rem Get today's year, month, day
for /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set year=%%a& set month=%%b& set day=%%c)
rem Modify the following line for your environment
set TARGET=C:\Intel
set TARGETNAME=serverweb
set BACKUPDIR=D:\backup
set BACKUPFILE=%BACKUPDIR%\%year%\%TARGETNAME%_%ip%_%year%%month%%day%.zip
set LOGFILE=%TARGETNAME%_backup.log
echo. | date | findstr "現在日期" >> %LOGFILE%
echo. | time | findstr "現在時間" >> %LOGFILE%
rem Compress the target directory before ftp
winrar A %BACKUPFILE% %TARGET%
if not exist %BACKUPDIR%\%year% mkdir %BACKUPDIR%\%year%
if not exist %BACKUPFILE% goto nofile
goto ftpsession
:nofile
echo can't find %BACKUPFILE% >> %LOGFILE%
echo.>> %LOGFILE%
goto end
:ftpsession
for /f "tokens=1-3 delims= " %%i in (ftp.cfg) do (set FTPSERVER=%%i&set USERNAME=%%j&set PASSWORD=%%k)
echo open %FTPSERVER% > ftpscript.txt
echo user %USERNAME% %PASSWORD% >> ftpscript.txt
echo cd letsgoapp >> ftpscript.txt
echo bi >> ftpscript.txt
echo put %BACKUPFILE% >> ftpscript.txt
echo bye >> ftpscript.txt
echo ---=== ftp session begins ===--->> %LOGFILE%
echo ftp %BACKUPFILE% to %FTPSERVER% >> %LOGFILE%
ftp -i -n < ftpscript.txt >> %LOGFILE%
echo ---=== ftp session ends ===--->> %LOGFILE%
echo. |time | findstr "現在時間">> %LOGFILE%
echo.>> %LOGFILE%
del ftpscript.txt
set TARGET=
set TARGETNAME=
set BACKUPDIR=
set BACKUPFILE=
set LOGFILE=
set FTPSERVER=
set USERNAME=
set PASSWORD=
:end
其中zip指令(取自Info-ZIP發行之免費軟體)並非Windows內建的指令,請由本站下載,放到C:\Windows目錄下
Windows內建的FTP指令說明可以透過執行ftp --usage指令得到:
ftp --usage
Transfers files to and from a computer running an FTP server service
(sometimes called a daemon). Ftp can be used interactively.
FTP [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-w:windowsize] [-A]
-v Suppresses display of remote server responses.
-n Suppresses auto-login upon initial connection.
-i Turns off interactive prompting during multiple file
transfers.
-d Enables debugging.
-g Disables filename globbing (see GLOB command).
-s:filename Specifies a text file containing FTP commands; the
commands will automatically run after FTP starts.
-a Use any local interface when binding data connection.
-A login as anonymous.
-w:buffersizeOverrides the default transfer buffer size of 4096.
host Specifies the host name or IP address of the remote
host to connect to.
Notes:
- mget and mput commands take y/n/q for yes/no/quit.
- Use Control-C to abort commands.
另外我把ftp的參數檔存放在ftp.cfg,內容如下
remotehost username passwordremotehost:請改成您的遠端主機IP(需有啟動FTP Server)username:請改成您的FTP帳號password:請改成您的FTP密碼
執行完會在執行目錄下產生一個logfile,可供debug用。
最後,遠端伺服器需先建立好對應的帳號及密碼,如有修改請記得回來改ftp.cfg
參考文章 http://www.andowson.com/posts/list/42.page
頁:
[1]