TShopping

 找回密碼
 註冊
搜索
查看: 479|回復: 0

[教學] 如何定時備份windows 2008 Server網站目錄到遠端伺服器

[複製鏈接]
發表於 2015-9-1 15:05:02 | 顯示全部樓層 |閱讀模式
 
Push to Facebook Push to Plurk Push to Twitter 
設計理念
1.先將要備份的目錄壓縮起來(節省傳輸時間)
2.然後再ftp到遠端的伺服器去(建立日期目錄)
3.定時自動執行(如每日凌晨三點)

首先,3可用Windows附屬應用程式=>系統工具=>排定的工作來完成,我們最後設定即可。
1和2則需要寫支DOS batch程式來完成,假設叫做remote_backup_web.bat。底下是該bat檔案的內容

  1. rem ---------------------------------------------------------------------------
  2. rem Backup web application to remote host
  3. rem
  4. rem $Id: remote_backup_web.bat 2015-09-01 14:24:43Z woff $
  5. rem ---------------------------------------------------------------------------


  6. @echo off
  7. rem Find out web server's IP address
  8. ipconfig | findstr "IPv4" > ip1.txt
  9. for /f "tokens=1-2 delims=:" %%i in (ip1.txt) do echo %%j > ip2.txt
  10. for /f "tokens=1-2 delims= " %%i in (ip2.txt) do (set ip=%%i)

  11. del ip1.txt
  12. del ip2.txt

  13. rem Get today's year, month, day
  14. for /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set year=%%a& set month=%%b& set day=%%c)

  15. rem Modify the following line for your environment

  16. set TARGET=C:\Intel
  17. set TARGETNAME=serverweb
  18. set BACKUPDIR=D:\backup
  19. set BACKUPFILE=%BACKUPDIR%\%year%\%TARGETNAME%_%ip%_%year%%month%%day%.zip
  20. set LOGFILE=%TARGETNAME%_backup.log

  21. echo. | date | findstr "現在日期" >> %LOGFILE%
  22. echo. | time | findstr "現在時間" >> %LOGFILE%

  23. rem Compress the target directory before ftp

  24. winrar A %BACKUPFILE% %TARGET%

  25. if not exist %BACKUPDIR%\%year% mkdir %BACKUPDIR%\%year%

  26. if not exist %BACKUPFILE% goto nofile
  27. goto ftpsession

  28. :nofile
  29. echo [FAILURE] can't find %BACKUPFILE% >> %LOGFILE%
  30. echo.>> %LOGFILE%
  31. goto end

  32. :ftpsession
  33. for /f "tokens=1-3 delims= " %%i in (ftp.cfg) do (set FTPSERVER=%%i&set USERNAME=%%j&set PASSWORD=%%k)
  34. echo open %FTPSERVER% > ftpscript.txt
  35. echo user %USERNAME% %PASSWORD% >> ftpscript.txt
  36. echo cd letsgoapp >> ftpscript.txt
  37. echo bi >> ftpscript.txt
  38. echo put %BACKUPFILE% >> ftpscript.txt

  39. echo bye >> ftpscript.txt

  40. echo ---=== ftp session begins ===--->> %LOGFILE%
  41. echo ftp %BACKUPFILE% to %FTPSERVER% >> %LOGFILE%

  42. ftp -i -n < ftpscript.txt >> %LOGFILE%

  43. echo ---=== ftp session ends ===--->> %LOGFILE%
  44. echo. |time | findstr "現在時間">> %LOGFILE%
  45. echo.>> %LOGFILE%

  46. del ftpscript.txt

  47. set TARGET=
  48. set TARGETNAME=
  49. set BACKUPDIR=
  50. set BACKUPFILE=
  51. set LOGFILE=
  52. set FTPSERVER=
  53. set USERNAME=
  54. set PASSWORD=

  55. :end
複製代碼
其中zip指令(取自
發行之免費軟體)並非Windows內建的指令,請由本站下載,放到C:\Windows目錄下


Windows內建的FTP指令說明可以透過執行ftp --usage指令得到:

  1. ftp --usage

  2. Transfers files to and from a computer running an FTP server service
  3. (sometimes called a daemon). Ftp can be used interactively.

  4. FTP [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-w:windowsize] [-A] [host]

  5.   -v             Suppresses display of remote server responses.
  6.   -n             Suppresses auto-login upon initial connection.
  7.   -i             Turns off interactive prompting during multiple file
  8.                  transfers.
  9.   -d             Enables debugging.
  10.   -g             Disables filename globbing (see GLOB command).
  11.   -s:filename    Specifies a text file containing FTP commands; the
  12.                  commands will automatically run after FTP starts.
  13.   -a             Use any local interface when binding data connection.
  14.   -A             login as anonymous.
  15.   -w:buffersize  Overrides the default transfer buffer size of 4096.
  16.   host           Specifies the host name or IP address of the remote
  17.                  host to connect to.

  18. Notes:
  19.   - mget and mput commands take y/n/q for yes/no/quit.
  20.   - Use Control-C to abort commands.
複製代碼

另外我把ftp的參數檔存放在ftp.cfg,內容如下


  1. remotehost username password
複製代碼
remotehost:請改成您的遠端主機IP(需有啟動FTP Server)
username:請改成您的FTP帳號
password:請改成您的FTP密碼

執行完會在執行目錄下產生一個logfile,可供debug用。

最後,遠端伺服器需先建立好對應的帳號及密碼,如有修改請記得回來改ftp.cfg




 

臉書網友討論
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2016-12-11 00:59 , Processed in 0.060829 second(s), 22 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表