Hulp nodig? Wij helpen graag

How do I use FTP mirror with SSH?

FTP Mirror is a set of SSH commands to copy the file and folder structure from one server to another server as an exact copy. You can use this to transfer files to your cPanel account, very useful when moving websites. FTP Mirror is executed in a SSH session using the lftp command. In this article we explain which commands are commonly used or are useful when using FTP Mirror.

LFTP

The following commands can be used when using FTP Mirror.

LFTP commands

LFTP

We use the lftp command to start the FTP program to FTP mirror.

set ftp: ssl-allow

With the command set ftp: ssl-allow you determine whether the SSL certificate verification should be applied or not. You use this command by following it with a space followed by true or false.

Open

With the open command you start the connection to the server using the hostname or the address used for this. Usually this hostname is ftp.domain where domain is your domain name. You use this command by entering open followed by a space and the (sub) domain that you are connecting to.

user

With the user command you specify with which user account you want to log into the server, this is your FTP user. You usually enter it at the same time (on a new line) with the open command. You can use the user command by following it with a space and the username.

mirror

The mirror command is what we need to mirror the data between the two locations. First, we navigate through the server using the commands that we highlight in this article. You can use the mirror command by following it with a space followed by the source folder followed by a space and then the target folder on the target server.

Example: Your files are currently on server 1 and you want them to be on server 2. On server 1 the files are in the folder named example.com and on server 2 these should be in the public_html.

Your command will look like this: mirror example.com public_html

Parameters LFTP mirror

-c

You can use the --continue or -c parameter to indicate that in the event of an unexpected network error your mirror must continue. The use of this is recommended.

-n

You can use the --only-newer or -n parameter to only transfer files that are newer. Only files that are newer than the existing files will be transfered. Files that do not exist yet will also be transfered.

-R

You can use the -R parameter (with capital letter R) to change the mirror in the opposite direction. Your target server becomes the source and the source server becomes the target.

Navigation commands

CD

The cd command stands for Change Directory, so you can use this to navigate through the folder structure. You can use this by typing the cd followed by a space and the path you want to go to. Instead of using a full file or folder path, you can also use the cd command to navigate to a file or folder in the current directory. Use the ls command discussed further on in this article to see the current directory and it's contents.

cd ..

If you want to go to the parent folder in your folder structure then use your cd followed by a space and 2 dots.

So you get: cd ..

ls

With the ls command you request a list of folders and files that are visible on the current path. Your terminal will show these after executing the ls command.

exit

When you have finished transferring everything and you want to close the connection, use the exit command.

Example steps

In this example we outline how you can make use of FTP mirror, for this we use an SSH connection that is started from your Terminal or SSH application. The order of using commands and bundling certain commands we discuss might not work on all servers, contact your host if a certain command or command set does not work as expected.

Also read: How do I use SSH?

Step 1. Log in to SSH with the login details of the cPanel account on the server where the copy of the files should go (target server).

Step 2. Determine in which folder you want to place the files, with the ls command you can see which folders are at the current level (this will be the root of your account). In this example, that will be the public_html that is visible at this level. If you want to navigate to lower level folders and view their contents, use the aforementioned CD commands in combination with ls to display their contents.

Step 3a. We are now going to set up an lftp connection from the target server to the source server, the server currently containing the files which we want to copy. In a word processor in plaintext, we prepare the following 4 commands directly below each other:

lftp
set ftp: ssl-allow false
open DOMAIN
user USER

Step 3b. Replace the following information:
DOMAIN: here you enter the ftp server address of the source server. Usually this is something like ftp.domain where domain is the domain name. For example ftp.examplehost.com.
USER: This is the username that is required to set up an FTP connection, for example v000001.

Step 4. Copy the 4 commands and paste them into your SSH window to execute them one after the other. You will now be asked for the password of the FTP user to log in to the source server. Enter the password and press enter.

Step 5. You are now connected to the server where the files are stored on. From here you can use the aforementioned navigation commands to find the folder that contains the files that you want to copy to the target server. When you are in the upper folder of the location you want to copy you can start with the next step.

In this example we want the public_html which is placed in a folder called example.com which is in a folder called domains. The path of this fictional folder is therefore /domains/example.com/public_html . In this case we do not open the public html_but remain in the above example.com folder from where we can see this folder via the ls command.

Step 6. In your SSH window now enter the mirror command followed by -c directly followed by the folder that you have just located on the source server and the name / location of the folder on the target server. Because in this example both locations are called public_html we insert them both, it could be that this is different for your situation. In this case the command is: mirror -c public_html public_html

Step 7. If everything went well, you will now see a progression in transferring the content from the source to the target server. You can then close the connection again via the exit command.

Terminal Example

The example below gives you an idea of what your ssh terminal can look like after following the above steps (we have simplified the data and rights to make it easier to read).

[voorbe1q@premium11 ~]$ ls
access-logs etc logs mail public_ftp public_html ssl tmp www
[voorbe1q@premium11 ~]$ lftp
lftp :~> set ftp:ssl-allow false
lftp :~> open ftp.examplehost.com
lftp ftp.examplehost.com:~> user v000001
Password:
lftp v000001@ftp.examplehost.com:~> ls
-rw-r--r-- 1 80 www 36809419 Jun 24 23:59 tmp
-rw-r--r-- 1 80 v000001 36809419 Jun 28 13:17 domains
lftp v000001@ftp.examplehost.com:/> cd domains
lftp v000001@ftp.examplehost.com:/domains> ls
-rw-r--r-- 1 80 www 36809419 Jun 21 13:11 conf
-rw-r--r-- 1 80 v000001 36809419 Jun Oct 7 13:07 error_docs
-rw-r--r-- 1 80 v000001 36809419 Jun 13:13 .htaccess
-rw-r--r-- 1 80 v000001 36809419 Jul 11:11 example.com
lftp v000001@ftp.examplehost.com:/domains> cd example.com
lftp v000001@ftp.examplehost.com:/domains/example.com> ls
-rw-r--r-- 1 80 v000001 36809419 Jun Oct 7 13:07 index.html
-rw-r--r-- 1 80 v000001 36809419 Feb 3 10:20 public_html
lftp v000001@ftp.examplehost.com:/domains/example.com> cd public_html
lftp v000001@ftp.examplehost.com:/domains/example.com/public_html> ls
drwxr-xr-x 5 v000001 pemcln 4096 Nov 1 06:09 Contact
drwxr-xr-x 5 v000001 pemcln 4096 Dec 22 02:01 Downloads
drwxr-xr-x 5 v000001 pemcln 4096 Oct 7 15:22 Home
-rw-r--r-- 1 v000001 pemcln 258 Jan 11 05:50 index.php
drwxr-xr-x 2 v000001 pemcln 4096 Nov 8 21:12 mobile
drwxr-xr-x 5 v000001 pemcln 4096 Jun 3 03:09 News
drwxr-xr-x 5 v000001 pemcln 4096 Nov 12 12:01 About-us
-rw-r--r-- 1 v000001 pemcln 557 Dec 22 06:50 page.php
-rw-r--r-- 1 v000001 pemcln 79 Jan 11 05:50 robots.txt
-rw-r--r-- 1 v000001 pemcln 1953 Jan 11 05:50 sitemap.xml
drwxrwxrwx 2 v000001 pemcln 4096 Feb 3 10:15 admin
drwxr-xr-x 5 v000001 pemcln 4096 Dec 31 02:01 Webshop
lftp v000001@ftp.examplehost.com:/domains/example.com/public_html> cd ..
lftp v000001@ftp.examplehost.com:/domains/example.com> mirror -c public_html public_html
Total: 888 directories, 6660 files, 0 symlinks
New: 6659 files, 0 symlinks
Modified: 1 file, 0 symlinks
165147829 bytes transferred in 123 seconds (1.33M/s)
To be removed: 1 directory, 0 files, 0 symlinks
lftp v000001@ftp.examplehost.com:/webspace/httpdocs> exit

Troubleshooting

The authenticity of host ...

When you connect to a server for the first time, you may be asked if you trust the host. This will look something like this (in this example we have replaced the server, the ip and the ECDSA key with the relevant data):

The authenticity of host '[SERVER]:4000 ([IP]:4000)' can't be established.
ECDSA key fingerprint is SHA256:ECDSAKEY.
Are you sure you want to continue connecting (yes/no)?

You can answer this question with yes followed by an enter to add it to the list of known hosts.

 

This knowledgebase article was last updated on: 7 October 2019

Heeft dit artikel je geholpen?

Status

Ga naar onze statuspagina voor een overzicht van recente storingen en onderhoud.

Openingstijden

Maandag — vrijdag 9:00 — 17:00