You are on page 1of 3

Batch Programming .

Scenario: Backing Up Files via Copying and FTP Upload


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. For the backups I use four batch files but of course it is possible to do it only with two but in this case im 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. 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. Put the 7za.exe in your 7zip installation folder. So create a first batch called 7-zip-batch.bat. @echo off CD G:\7-zip\ 7za.exe a -t7z "C:\Documents-Backup-Upload.7z" G:\Documents -mx=9 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. Now another batch is created: @echo off REM firstly, call 7zip-batch and compress files CALL "G:\7z-batch.bat" REM This batchfile copies data from a local drive to a mounted USB flash drive if exist "C:\Documents-Backup-Upload.7z" ( echo File does exist! Lets start the copy process! xcopy "C:\Documents-Backup-Upload.7z" k:\ /S /P /F /-Y xcopy "C:\Documents-Backup-Upload.7z" "G:\Backup - Folder" /S /P /F /-Y CALL G:\2ndcopybatch.bat ) else ( echo File isnt located in the specified directory. Please check the correct location of your files! goto ENDIT ) :ENDIT echo. echo.

echo. echo Press a key to exit. pause >nul 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. 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. OPEN "IP-address" port USER "username" "password" BINARY PUT "C:\Documents-Backup-Upload.7z" BYE * Write the IP-Address, the username and the password without quotes. Dont 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). 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. PUT is obviously used to put a file on a ftp server, GET can be used to download a file. BYE disconnects the user from the server. Now you need another batch file (ftpbatch.bat here) that starts that ftp-upload-file (ftp.ftp). ftp -n -i -s:G:\ftp.ftp The last batch file is called 2ndcopybatch.bat here: @echo off REM second copy batch CALL "G:\ftpbatch.bat" This file just calls the ftpbatch.bat, nothing else.

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. Its best to keep all the batchfiles in one folder.

You might also like