Copying files between two Forge servers
It’s not as complicated as you would think so
Recently I had to copy over some files from one of my server to a new one. They are both Digital Ocean VPS servers and managed by Laravel Forge. (They are both great services, so I highly recommend them. Don’t worry, the links are not affiliate links).
Since you can’t log in with username and password to these servers by default, copying the files was a little bit tricky, but not impossible.
So let’s go ahead, and do some setup to copy the required files directly from Server A to Server B.
At first step, you have to enable the login for the source server’s (Server A) user to the target server (Server B). To do so, I generated an ssh key with the following command on Server A:
ssh-keygen -t rsa -b 4096 -C “email@example.com” -f id_ssco
This command will generate a ssh key pair (named as id_ssco and id_ssco.pub). Now you may add your public key to Laravel Forge to enable the authentication.
The following command will display the content of your public key:
You should see something similar:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCdAAnIBX+ZQdTdKX4FfI/q/zvBYK94SZV2G70RfserIe9VV4039LY1Q5kMAO1vZjyP0C7xG3l7rLje3muI+KD5jd/NhcWivLzTK+UFLJDfOHMS5WYWsEBC26k4Mq36XSiXn8/Dxbklsi0HKD0QbGaHyOEquCCGfXpQy1u5QCR7GCFZwmCiYW0vDIhTX7oNqKbuCpfTYFHbR85Tx77d9RTMXuD9lsNqaaZBhSTxwUgrWj1MoYVXIco6Mxqt9wxCD1PDLFGkll2CifCZ6WpQJ7AyCJYfhe4Eh+jeqaJy/mz05OAvNxAvFBg0XEHVfOnaQ3jOQRqE7/t58hgF51X9rHvDjCynNZSF2kZS1O2rXhxLfz6LZfOvy8cjMkvxtu98EIqptJh96sQNG/RJ5WYfJ/qTW9CGZfjhpBK4C0/ZGQxaC0ESmJUun6Qv9WB2jJKLJGj9Vt/iMx2Cdot9qXOkSpi4ETWOrI6ZQ0GQkreST9OVDfdIFosvCAHjiNjY7J2xVxdrdIw8Bx5eZKzqNUTkYeKfEN+vLFeiJ2JOA/dqhrqIYs7TuOwr9u4LVUnc2LrSEWLqoqcw5uKV5PDn/4Sbh3yOueHKlawYaHa/EqjkejMuqoO4TtZ8SbeQBpzaFtTXHruPJcrdMCWXg3oGrhSt0bjLncNBuX165uVrrOJxj3ow== firstname.lastname@example.org
Select and copy it to your clipboard, log in to your Forge account, select your server and choose the SSH Keys tab.
Select a name and paste your public key. Forge will install this key for you on the target server. After the installation is finished, you can test it from your source server:
Just replace the remoteserver.com in the example above to your server’s host name or ip address and you should be able to log in to your server, directly from your other one. Note, at this moment this is one way street, so you can log in from the source server to the target server, and not the other way around. But it’s ok, we are ready to copy the files back and fort using scp or rsync.
Let’s test the login:
ssh -i "/home/forge/.ssh/id_ssco" email@example.com
With the -i option you may define the ssh key to be used for the connection. This should point to the generated private key (not to the public).
Now, let’s assume, you’d like to copy a single file with scp. Your command would look like something like this:
scp -i "/home/forge/.ssh/id_ssco" example.com/public/index.php firstname.lastname@example.org:/home/forge/example.com/public/
Again, you have to use your target server’s (Server B) hostname or ip address in the command above.
Lets copy over the entire folder using rsync:
rsync -avz -e "ssh -i /home/forge/.ssh/id_ssco" example.com/public email@example.com:/home/forge/example.com
As you can see, it’s not rocket science, you just have to do some initial set up to connect your servers and be able to move / copy files between them.