Winscp is a file transfer program similar to Filezilla. However it has scripting functionality we can use to set up a automated backup of a remote ftp server.
Step 1 – Install WinSCP
Download and install Winscp. You can find the download link Here
Once installed you can enter your servers ftp details and use it just like filezilla.
You can save the session as a site to easily get back to it by right clicking the tab on the top left and selecting “save session as site” from the dropdown.
Here you also have the option of saving passwords if your scrips stops to ask for a password you can try enabling this checkbox.
To edit a saved site you can right click the tab again and go to “sites > Site manager” from the dropdown.
If you encounter an error along the lines of “error could not get directory” and it doesn’t populate the directory. You can go to the site manger and on the bottom right select “advanced”
Then go to “Connection” on the left and uncheck “passive mode”
Step 3 – Setting up the scrips
Next it’s time to prepare the scrips. In this guide we’re going to use a .bat file to hook into windows task scheduler and a winscp script.txt file to script the backup. I’ll post the example/template scrips to show you how it should look and what does what.
You can either edit and use those or make your own. The .bat is very simple. All it has is 1 line “winscp.exe /console /script=NAMEOFYOURBACKUPSCRIPT.txt” all this does is launch winscp and pass the winscpscript.txt file to the console. You can place this file anywhere but for this guide
i’ll save it inside the winscp directory. C:/Program Files (x86)/WinSCP
.bat File
winscp.exe /console /script=7dtdbackupscript.txt
The next script will tell WinSCP what to do. For simplicity i’ll also save this file to the same directory. I also think it might have to be saved there anyway. If you’re unable to edit the script inside this directory, you can create it outside on the desktop for example and just copy it there when you’re done.
If you’re using the template scrips just replace all the capitalisations with your ftp details. There is also comments explaining everything there.
.txt Script File
# These are comments
option batch abort
option confirm off
# example { open naz@192.168.1.87 /log="C:\winscp.log" /loglevel=0 }
open YOURUSERNAME@FTPIPADDRESS /log="C:\winscp.log" /loglevel=0
#Winscp logs are found in C:\Users\YOURUSERNAME\AppData\Local\Temp by default if enabled (search by date modified or "winscp")
#{Alternate open line that allows you to change file protocol and enable/disable passive mode if needed} open ftp://USERNAME:PASSWORD@FTPIPADDRESS/ -passive=0
# Example { get -speed=1000 "/remoteftp*" "C:\backups\7DTD_Remote_Server_Backup.%TIMESTAMP#yyyy-mm-dd_hh-nn-ss%*" }
get -speed=1000 "/PATH TO THE REMOTE FOLDER YOU WANT TO BACKUP*" "LOCAL PATH YOU WANT TO SAVE THE BACKUPS TO\NAME OF THE BACKUP.%TIMESTAMP#yyyy-mm-dd_hh-nn-ss%*"
# -speed= is in kBps (Kilobyte per second) , 1000= 8 mbps (Megabit per second) or 1 Megabyte per second
exit
Step 4 – Setting up schedules to run the scrips
Once the scrips are all done we can set up the automated part using windows task scheduler. Hit start and search for “task scheduler” hit enter. On the right panel click “create basic task”
Give it a name and description if you want.
Select daily for the trigger. We will fine tune this more later.
we’re going to start a program as an action. For the “program/script” browse to where the simple .bat file is that we created earlier. For “Add Arguments” add this:
/log=c:\path\winscp.log /script=NAMEOFYOURSCRIPT.txt
For “start in” put the path to Winscp: C:\Program Files (x86)\WinSCP
Before we hit finish, check the “open the properties checkbox for this task when i click finish” then hit finish.
Go to the “triggers” tab and edit the daily trigger.
Under the advanced settings towards the bottom check “repeat task every” then type how often in hours to want it to run. Ideally you want a backup every hour, however it will depend how big your saves are and how fast your internet is, also how much space you have available. I added the -speed
option in the script so you can limit the speed to prevent your internet being unusable every couple hours for half an hour etc. I also like to change the start time to 0:00:00 so it will run 12 times everyday with 2 hours etc. Hit ok when your finished.
Step 5 – Test everything works ok
Now you can right click the task and click run. If it’s set up correctly you will see it transferring in a cmd window and it will run on it’s own every x hours depending on what you set it too.
Troubleshooting
If the window pops up and then disappears with nothing happening check the logs found in C:\Users\YOURUSERNAME\AppData\Local\Temp by default if enabled (search by date modified or “winscp”)
the directory paths can be a pain to figure out correctly and rented hosts sometimes save the servers save folders in different places. If you’re not sure where your servers saves are, ask your host and they should be able to tell you.
If you’re still having issues check the winscp docs found Here
If you have everything set up correctly and it still doesn’t work, the issue may be with your hosts ftp server. I had an issue with mine and after talking with their support they managed to fix the issue on their end. So it’s worth shooting them a message if it still won’t cooperate.