defect122

Windows batch - Ftp with batch file

Feb 16th, 2012
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Batch – Programming .
  2.  
  3. Scenario: Backing Up Files via Copying and FTP Upload
  4.  
  5. Backing up files is pretty important for many pc users and this is a How To on how to backup files via batch – programming. A batch file is a good way to make small and easy backups and you can even use your ftp server or remote drives and realize it with a batch.
  6.  
  7. For the backups I use four batch files but of course it is possible to do it only with two but in this case i´m going to  use four.  Lets assume you got a folder that you want to backup and save it to another drive, a USB flash drive and finally upload it to an ftp server.
  8. Firstly, i want to compress the folder. Here 7zip is used. To use 7zip in a batch you need the 7za.exe, so that you can execute it from the command line. The 7za.exe can be found via google search.
  9. Put the 7za.exe in your 7zip installation folder.
  10.  
  11. So create a first batch called 7-zip-batch.bat.
  12.  
  13. @echo off
  14.  
  15. CD G:\7-zip\
  16.  
  17. 7za.exe a -t7z "C:\Documents-Backup-Upload.7z" G:\Documents -mx=9
  18.  
  19. The final compressed archive is called "Documents-Backup-Upload.7z" but you can name it whatever you want. The source folder is G:\Documents. Compression here is 9, can also be changed  to a different level, f.e. -mx=5.
  20.  
  21. Now another batch is created:
  22.  
  23. @echo off
  24.  
  25. REM firstly, call 7zip-batch and compress files
  26.  
  27. CALL "G:\7z-batch.bat"
  28.  
  29. REM     This batchfile copies data from a local drive to a mounted USB flash drive
  30.  
  31. if exist "C:\Documents-Backup-Upload.7z" (
  32.     echo File does exist! Lets start the copy process!
  33.     xcopy "C:\Documents-Backup-Upload.7z" k:\ /S /P /F /-Y  
  34.     xcopy "C:\Documents-Backup-Upload.7z" "G:\Backup - Folder" /S /P /F /-Y
  35.     CALL G:\2ndcopybatch.bat
  36. ) else (
  37.     echo File isn´t located in the specified directory. Please check the correct location of your files!
  38.     goto ENDIT
  39. )
  40.  
  41. :ENDIT
  42. echo.
  43. echo.
  44. echo.
  45. echo Press a key to exit.
  46. pause >nul
  47.  
  48. This batch is used to call the 7z-batch and copy the compressed folder to the mounted USB flash drive and to a logical drive of your local pc. Make sure that you adjust the path name of the batch, so that it fits.  It then calls a second batch. The if-stricture is used here as well, but thats not necessary, Its just to check if the compressed file is at the right, specified location.
  49. Next, lets move on to the ftp batch. For the ftp upload the integrated ftp.exe client of windows found in windows/system32 can be used. Create a file called ftp.ftp, open it with your editor and just put the following simple commands in it to make the upload work.
  50.  
  51. OPEN "IP-address" port
  52. USER "username"
  53. "password"
  54. BINARY
  55. PUT "C:\Documents-Backup-Upload.7z"
  56. BYE
  57.  
  58. * Write the IP-Address, the username and the password without quotes. Don´t forget to put the right port number after the ip-address. So it looks something like: 192.168.0.55 21 (21 is the port number here).
  59. This is what the file could look like. Only a few commands are used to upload one file. BINARY means that you are uploading an executable file, which is the case here. Its an compressed archive. You can also put text files using the ASCII command.
  60. PUT is obviously used to put a file on a ftp server, GET can be used to download a file.
  61. BYE disconnects the user from the server.
  62. Now you need another batch file (ftpbatch.bat here) that starts that ftp-upload-file (ftp.ftp).
  63.  
  64. ftp -n -i -s:G:\ftp.ftp
  65.  
  66. The last batch file is called 2ndcopybatch.bat here:
  67.  
  68. @echo off
  69.  
  70. REM second copy batch
  71.  
  72. CALL "G:\ftpbatch.bat"
  73.  
  74. This file just calls the ftpbatch.bat, nothing else.
  75.  
  76.  
  77.  
  78.  
  79. If you put it all together correctly, it should work. And as mentioned before this can all be done using only two or three batches. Its a very easy way to make multiple backups work by using batchfiles. Perfect for documents and something small.
  80.  
  81. Its best to keep all the batchfiles in one folder.
Advertisement
Add Comment
Please, Sign In to add comment