SEARCH

How-To Geek

How to Automatically Backup Your Web Server Files With WinSCP over FTP

winscp_lead

You’ve heard it time and time again: back up your data. There are plenty of backup solutions, but nothing is better than an easy and free solution. So with a few lines of code and a very helpful program called WinSCP, we’re going to set up an automatic sync between your FTP server and your home computer.

FTP (File Transfer Protocol) is an Internet protocol that allows users to transfer files between computers. Despite its old age (think pre-TCP/IP era), FTP and its sister protocol SFTP (Secure File Transfer Protocol) are still very popular today and are viewed as an easy way to transfer files locally and over the Internet. It also happens to be a very good way to keep a remote backup of important files you keep on your FTP server.

If you don’t want to pay for a third-party FTP solution, check out this article on how to build your own FTP server written by the fine folks over at Lifehacker.

Requirements

  • An FTP server and access credentials
  • A Windows computer
  • A copy of WinSCP (Windows only)

Setting Up WinSCP

Once you’ve downloaded your copy of WinSCP, install the .exe on your computer. It also wouldn’t be a bad idea to start remembering your FTP address, username, and password while WinSCP is installing.

winscp1

Now that we have WinSCP installed, we’re going to create and save a new session profile for easier access later on. From the login screen, enter your host name (i.e. ftp.howtogeek.com), username, and password. You can use either FTP or SFTP, but be sure to change the port number the appropriate port your FTP host has provided you. Then click Save to save the profile. Open the profile to log into your FTP server. This will verify that you typed the right credentials in the profile.

winscp2

Creating The Script

We’re going to create a simple script that logs into your FTP server, tells WinSCP where to download the missing files to on your computer, and then creates a log file that records FTP activity. Go ahead and open Notepad on your computer. Copy the following script template into Notepad:

option batch on
option confirm off
open patrickbisch@ftp.howtogeek.com
cd backups
option transfer binary
get /testremote* f:\backups\testlocal\*
synchronize local f:\backups\testlocal testremote
exit

Here’s a brief explanation of what’s going on in the script. WinSCP will answer all prompts negatively to avoid holding up the script. Then it will automatically overwrite files when prompted. It then logs into your FTP server using the profile we created earlier, changes directories (if needed), and transfers binary (as opposed to ASCII). Finally, it reads the remote FTP directory and transfers files to the specified local directory. For even more options, check out the official WinSCP scripting page.

Now before you modify or run this script, we suggest creating test directories on both the remote and local targets. The last thing you want is to accidentally wipe your FTP server without any backups. So create a directory on your computer called “testlocal” (we created it under f:\backups) and another called “testremote” on your FTP server (we created it at the root). Once you run the script and it executes successfully, save it as “sync.txt” to your computer (we saved ours in our testlocal folder). Now you’re ready to automate the script.

Automating The Script

We’re going to let Windows handle the automation by using its built-in Task Scheduler. In Windows 7, start by opening Control Panel > System and Security > Administrative Tools > Task Scheduler.

winscp3

In the right column, click the Create Basic Task button.

winscp4

Name and describe your task, and then click Next.

winscp5

The next option will determine how often the task will run. We chose to run the script every time we turn our computer on. You can choose a less frequent option like Weekly to run it less often.

winscp6

On the next screen, choose “Start a program” and click Next. You’ll be prompted to browse for a program or script. Click the Browse button and navigate to “C:\Program Files\WinSCP” to select the WinSCP.exe. Underneath, add “/console /script=f:\backup\sync.txt /log=f:\backup\log.txt “ to Add arguments. Be sure to change the arguments if your sync.txt is in a different location and if you want to generate the log file in a different location. Click Next.

winscp7

You’ll see a summary of your task. If it looks correct, click Finish. Finally, to ensure the task runs properly, we’re going to run it. Make sure you have at least one file in your testremote directory (i.e. “thisisatest.txt”). Highlight your newly created task and click Run in the right column. You should see a command prompt appear, connect to your FTP server, and then sync the file(s).

winscp8

Your test task should have completed, and your test file should now be in the specified local folder. If you run into any errors, check the log file to find out why.

That’s all there is! You now have a free backup solution that will keep all your FTP files synced to a local folder. Should your FTP server ever crash or you accidentally delete a file, you can recover the copy from your computer!

On the south side of Chicago born and raised. On the computer is where I spent most of my days. Nerding out, haxing, maxing my CPU. And all writing some How-To's is now what I do.

  • Published 12/21/11

Comments (4)

  1. Usman

    Great tutorial but in my opinion WinSCP is very slow as compared to any standard FTP software. It takes a huge amount of time to download data from the FTP server.

  2. Milo

    This is an amazing idea! I will most likely compress things before upload and stuff.. I do want to find a good way to do incremental backups though.

  3. Adamboy7

    Well Usman, I would assume it is slow because it is free. Free survices arent usually the best quality survices out there. I myself havent tried any others however, so I am flying blind here. Do you know of any better ways of doing this for free?

  4. Andrew Lam

    You can set WinSCP to use the FTP protocol instead of SFTP.
    SFTP is the default and a bit slower than FTP due to the security.

    WinSCP is a quality program and as good as any other client (as many other free software are too).

Enter Your Email Here to Get Access for Free:

Go check your email!