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.
- 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.
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.
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
option transfer binary
get /testremote* f:\backups\testlocal\*
synchronize local f:\backups\testlocal testremote
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.
In the right column, click the Create Basic Task button.
Name and describe your task, and then click Next.
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.
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.
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).
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!