winscp ftp_如何通过WinSCP通过FTP自动备份Web服务器文件
winscp ftp
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.
您一次又一次听到它:备份数据。 有很多备份解决方案,但是没有什么比简单,免费的解决方案更好。 因此,使用几行代码和一个非常有用的程序WinSCP,我们将在FTP服务器和家用计算机之间建立自动同步。
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.
FTP(文件传输协议)是允许用户在计算机之间传输文件的Internet协议。 尽管有很长的历史(想想在TCP / IP之前的时代),FTP及其姐妹协议SFTP(安全文件传输协议)在今天仍然非常流行,并且被视为一种在本地和Internet上传输文件的简便方法。 这也是对保存在FTP服务器上的重要文件进行远程备份的一种很好的方法。
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.
如果你不想支付第三方FTP解决方案,看看这个文章就如何建立以优良的乡亲在Lifehacker的写你自己的FTP服务器。
要求 (Requirements)
- An FTP server and access credentials FTP服务器和访问凭据
- A Windows computer Windows计算机
A copy of WinSCP (Windows only)
WinSCP的副本(仅Windows)
设置WinSCP (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.
下载完WinSCP副本后,在计算机上安装.exe。 在WinSCP安装过程中开始记住您的FTP地址,用户名和密码也不错。
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.
既然已经安装了WinSCP,我们将创建并保存一个新的会话配置文件,以便以后访问。 在登录屏幕上,输入您的主机名(即ftp.howtogeek.com ),用户名和密码。 您可以使用FTP或SFTP,但请确保将端口号更改为FTP主机为您提供的适当端口。 然后单击“保存”以保存配置文件。 打开配置文件以登录到您的FTP服务器。 这将验证您在配置文件中键入了正确的凭据。
创建脚本 (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:
我们将创建一个简单的脚本,该脚本登录到您的FTP服务器,告诉WinSCP在哪里将丢失的文件下载到您的计算机上,然后创建一个记录FTP活动的日志文件。 继续并在计算机上打开记事本。 将以下脚本模板复制到记事本中:
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
选项批处理打开选项确认关闭打开patrickbisch@ftp.howtogeek.com cd备份选项传输二进制文件获取/ testremote * f:\ backups \ testlocal \ *同步本地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.
这是脚本中发生的情况的简要说明。 WinSCP将否定地回答所有提示,以避免搁置脚本。 然后,系统会在提示时自动覆盖文件。 然后,它使用我们之前创建的配置文件登录到您的FTP服务器,更改目录(如果需要),并传输二进制文件(与ASCII相反)。 最后,它读取远程FTP目录并将文件传输到指定的本地目录。 有关更多选项,请查看官方的WinSCP 脚本页面 。
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.
现在,在修改或运行此脚本之前,建议您在远程和本地目标上创建测试目录。 您要做的最后一件事是不小心擦除FTP服务器而不进行任何备份。 因此,在您的计算机上创建一个名为“ testlocal”的目录(我们在f:\ backups下创建),并在您的FTP服务器上创建另一个名为“ testremote”的目录(我们在根目录下创建)。 一旦运行脚本并成功执行,将其另存为“ sync.txt”到您的计算机(我们将其保存在testlocal文件夹中)。 现在您可以自动化脚本了。
自动化脚本 (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.
我们将让Windows使用其内置的Task Scheduler来处理自动化。 在Windows 7中,首先打开“控制面板”>“系统和安全性”>“管理工具”>“任务计划程序”。
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.
在下一个屏幕上,选择“启动程序”,然后单击“下一步”。 系统将提示您浏览程序或脚本。 单击浏览按钮,然后导航到“ C:\ Program Files \ WinSCP”以选择WinSCP.exe。 在下面添加“ / console /script=f:\backup\sync.txt /log=f:\backup\log.txt”以添加参数。 如果您的sync.txt位于其他位置,并且要在其他位置生成日志文件,请确保更改参数。 点击下一步。
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).
您会看到任务摘要。 如果看起来正确,请单击“完成”。 最后,为了确保任务正常运行,我们将运行它。 确保您的testremote目录中至少有一个文件(即“ thisisatest.txt”)。 突出显示您新创建的任务,然后单击右列中的“运行”。 您应该看到一个命令提示符出现,连接到FTP服务器,然后同步文件。
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!
这就是全部! 现在,您有一个免费的备份解决方案,它将所有FTP文件同步到本地文件夹。 如果您的FTP服务器崩溃或不小心删除了文件,则可以从计算机中恢复副本!
winscp ftp
更多推荐



所有评论(0)