How to Copy Magento 2 Files From One Server to Another

So for those that know I am sure this is a stupid question however I have tried searching for the answer but failed.

I want to take a copy of my current production Magento 2 site and put it on a Digital Ocean development server (having followed Craig’s recent video) so I can test upgrading it to 2.3 (currently 2.2.6) as I have read it is not straight forward.

So far I have the server created in DO and the DB imported. Now I want to copy the files over, is there an easy way to do this using SSH or any other method.

Thanks in advance.

Hey @DizzyLizzy, great question. Migrating servers was something I wanted to cover this year. I just didn’t think I’d be talking about it so soon :slight_smile: There’s a couple of ways you can do this, but this is my preferred method.

I’m not going to go into too much detail here, because it’s Saturday, it’s late, and I want to catch up on The Punisher Season 2 :smiley:

Ok, so in this example, I’m going to make the following assumptions. Just swap out the variables where appropriate…

Original Server
IP: 123.123.123.123
Location of Magento files: /var/www/html/
SSH User: magento
SSH Pass: password1

New Server
IP: 456.456.456.456
Location of Magento files: /var/www/html/
SSH User: magento
SSH Pass: password1

Rsync
The method of which we will be transferring those files, is with the use of something called rsync. This is a pretty awesome tool that allows the secure transfer of files over SSH. Thus not requiring the need to download files from one server and upload them onto the other (which can take forever). More information on rsync

You can use rsync on either server, because you can either tell it to:

  • Copy local files to a remote location
  • Copy files from a remote server to a local directory

It doesn’t really matter which way around you do this. I guess it just comes down to either preference or convenience.

In my example, I will be logged into the New Server, therefore Copying Files from the Original Server to a local directory.

Step 1: Whitelisting
Before you can use rsync, you will need to whitelist your servers with one-another. Otherwise you won’t be able to connect. In my example, I’d have to allow 456.456.456.456 to connect to the Original Server over SSH (port 22). Hopefully, you know how to do that already.

Step 2: Log in
Next. we need to log into the New Server. If you log in as the superuser, don’t forget to switch to the magento user before running the rsync command.

It’s important to note that when the files are copied across, they will have the ownership of whomever you ran the command as. For example, if you run the rsync command whilst logged in as root, then the files will have those ownerships. And if you run the rsync command as magento, then obviously they’ll be written as magento.

If you mess up, it’s easy to fix. Once the files are copied over, just run the following command as a superuser to reset the permissions back to magento user: sudo chown -R magento:www-data /var/www/html/

Step 3: Navigate to web directory
What I like to do next, is navigate to my web directory. This isn’t necessary, but I think it’s a good habit to remember where you are on the server. So, in this case I’d simply do cd /var/www/html/

Step 4: Time to copy the files
Now, it’s time to use rsync to start transferring the files. Remember, we are transferring remote files to our local server:

rsync -avzhe ssh [email protected]:/var/www/html/ /var/www/html/
Breakdown: connect to Original Server as magento user and copy files from /var/www/html/ to /var/www/html/

or

rsync -avzhe ssh [email protected]:/var/www/html/ .
Breakdown: connect to Original Server as magento user and copy files from /var/www/html/ to the directory where I’m running the command from`

Running this command will take a little while, but you’ll see all of the files being transferred as it happens.

Step 5. Post transfer
Sometimes, after an rsync I get an issue with loading the site for the first time. If I remember correctly, I have to delete the following 2 folders. Don’t worry, they’ll regenerate next time you load the Frontend/Backend:

  • /var/www/html/pub/static/adminhtml
  • /var/www/html/pub/static/frontend

Also, I play-it-safe by running the pre-installation commands for resetting permissions (ref How to install Magento 2.3 post)

Then finally, I’ll clear the Magento cache with bin/magento c:c

Disclaimer: Always make backups before following any instructions or doing anything you’re not comfortable with

Final thoughts
There’s probably a good argument for zipping all of your files before transferring them. But I normally skip this step. Each to their own I guess.

Hopefully, this quick-guide helps. I know it’s a bit rushed and it’s easier to follow along on video - But this is the best I can do right now. :slight_smile:

I hope I didn’t miss anything. Don’t forget to let us know how you get on.

Good luck :+1:

1 Like

You could create a tarball (zipped file) using ‘tar’ on the magneto directory and its contents, then ‘scp’ (secure copy) the tarball to the new server.

tar - https://popoleodesigns.com/create-and-extract-a-tar-gz-archive-using-command-line/
scp - https://kb.iu.edu/d/agye

HTH

2 Likes

Wow @digitalstartup Craig that is a lot of detail for not too much detail :slight_smile: I will let you know how I get on tomorrow.

Many Thanks,
Liz

Hey @DizzyLizzy, how did you get on with the migration?

Hi @digitalstartup, I couldn’t get rsync to work as I couldn’t figure out whitelisting and kept getting blocked. I did end up doing it with tar which was actually quite fast for me so not so much of an issue. However I now can’t get the site to load but slowly working through all the issues. First issue was a password problem, now I have a php version problem.

I do have an issue on another DO server I created following your magento 2.3 video to the letter. It all works but when I go to any table in phpadmin I get this error, from what I gather its a phpadmin version issue with php 7.2

Warning in ./libraries/sql.lib.php#601
count(): Parameter must be an array or an object that implements Countable

That’s a shame :confused: I’m aiming to have a video out in a couple of weeks to cover migrating Magento from server to server using rsync. I’ll make sure to include whitelisting from the command line.

As for the phpMyAdmin error, you might be able to solve that by running the following commands as a superuser:

  1. sudo apt-get update
  2. sudo apt-get upgrade phpmyadmin

Hopefully, that will upgrade your version of phpMyAdmin to one that supports PHP 7.2.

EDIT: Fixed typo

Good news about the video, I will look forward to that.

Re phpadmin, unfortunately that doesn’t fix it as apparently I already have the latest version installed :frowning:
phpmyadmin is already the newest version (4:4.5.4.1-2ubuntu2.1).

The latest version of phpMyAdmin is 4.8.5.

It’s extremely likely that the Repository on the server has outdated libraries. That’s okay. That’s normal in fact. You simply need to update your repository, so that the next time your run sudo apt-get update, it finds the latest version.

So, I’ve had a quick look on my sandbox DO server, and mine it also stuck at 4.5.4.1. It’s not running PHP 7.2 yet which is why I don’t get the error. (It’s good to know that I’ll be running into this issue when I do upgrade though.)

I can’t find an up-to-date repository that will let me upgrade phpMyAdmin via the command line. But then again, I’ve not looked very long. And I’d rather not do it the long-winded way by downloading files and uploading them.

I’ll come back and visit this issue later in the week when I have a little more time. I’ll keep you posted with my findings. If I don’t update you by the weekend, give me a nudge because I might have forgotten :confused:

1 Like

Hey, @DizzyLizzy,

So, it doesn’t appear you can install that latest version of phpMyAdmin using the apt-get command. I’ve no idea why.

So, I did a little digging to see what the steps are to do it manually. I’ve streamlined the steps to make it a little easier. If you fancy giving it a go, these are the steps. Run as superuser:

  1. cd /usr/share/
  2. sudo mv phpmyadmin phpmyadminbk
  3. sudo wget https://files.phpmyadmin.net/phpMyAdmin/4.8.5/phpMyAdmin-4.8.5-all-languages.zip
  4. sudo unzip phpMyAdmin-4.8.5-all-languages.zip
  5. sudo mv phpMyAdmin-4.8.5-all-languages phpmyadmin
  6. sudo chown -R www-data:www-data /usr/share/phpmyadmin
  7. sudo cp phpmyadmin/config.sample.inc.php phpmyadmin/config.inc.php
  8. sudo nano phpmyadmin/config.inc.php
  9. Add a random string of numbers and text like the example below:

  1. Save your changes and exit by “Ctrl+X” then “Y” then Enter.
  2. sudo rm phpMyAdmin-4.8.5-all-languages.zip

Explanation of Steps:

  1. Navigate to /usr/share/ folder
  2. Create a backup of existing phpMyAdmin by renaming as phpmyadminbk
  3. Download the version 4.8.5 of phpMyAdmin direct from their website
  4. Unzip the file you just downloaded
  5. Rename the unzipped folder to phpmyadmin
  6. Update the ownership of the folder/files
  7. Create the phpMyAdmin config file
  8. Edit the config file you just created in the nano text editor
  9. Here, you are randomly generating a security key. Type anything.
  10. Save and exit the file
  11. Delete the zip file you originally downloaded. Just a bit of housekeeping

Summary
These instructions are the least amount I could put together (without stringing them together) to upgrade phpMyAdmin to 4.8.5. Which is the latest at time of writing. I tried this on my server, and it runs fine without any warnings.

I know it’s obvious, but please backup your server before following any instructions. Especially, as I haven’t retested the steps after writing them down. I don’t have time to rollback the server and retest right now.

EDIT: I’ll be covering this in an upcoming video.