Tag Archives: scp

Ubuntu Terminal: Efficient Way to Copy Across a MySQL Database from One Server to Another CodeUnit 23 MAY 2011

I often need to pull down a copy of a live MySQL database instance for development work on my local machine. Because my access to bandwidth is limited, I need to come up with the most efficient way of achieving this, and my method for achieving this is chronicled below – mainly so that I can refer to it when I forget!

Anyway, the process is simple enough to explain. First, we ssh into the target server and we use mysqldump to create a SQL dump text file of the required MySQL database. Next, we compress it using the powerful 7-Zip to create a nice and compact 7z file. Exit ssh. To pull it down from the server, we use plain old scp and do a secure copy down to our local machine. Next we uncompress the newly copied 7z archive and once extracted, import it into our local MySQL server using the mysql command.


And a nice example for copy and paste purposes:


mysqldump -u #USERNAME# -p#PASSWORD# -c --add-drop-table --all --quick #DATABASE# > dump.sql 

7za a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on dump.7z dump.sql 


scp -v -P #PORT# #REMOTEUSERNAME#@#HOSTNAME#:/home/server/dump.7z /home/craig/dumpscp.7z 

7za e /home/craig/dumpscp.7z 

mysql -u #USERNAME# -p#PASSWORD# database < /home/craig/dump.sql

How to Run SCP when Copying From a Machine not using the Default SSH Port 21 CodeUnit 22 JAN 2011

Although port 21 is the default port used for SSH connections and thus SCP, most of the time you will find that in order to increase protection, the actual assigned port is far removed from the default 21. So how do you specify this when running a SCP copy operation?

Well SSH has the lowercase -p switch that allows you to specify which port to attempt to connect on, looking like this in practice:

ssh -p 6188 othermachine.com

However, SCP for some or other reason (most likely due to the prevalence of using -p as a password switch elsewhere in Linux) does not use the lowercase -p to specify port, opting rather to make use of a capital -P to specify the port to use.

This means your SCP call would look like this:

scp -P 6188 othermachine.com/file /home/file

And now you know. Nifty.

Ubuntu Terminal How to Copy a File to or from a Remote Machine using SCP Tips, Tricks and Tutorials 04 AUG 2010

SCP is a powerful utility that allows us to securely copy files between remote machines. What makes it even more powerful is the ability to add it to scripts and so for today’s quick tutorial I will walk you through how you can go about either copying files from or to a remote machine with the use of SCP.

Now normally when you copy files with SCP you will be prompted for the remote machine’s password in order to continue with the copy operation. However, obviously if we wish to script with SCP we will need to get around this restriction and so we take a quick dive into the of public key cryptography.

Basically what we are going to do now is generate a public/private key pair for your local machine which is going to run the SCP function and then hand out the public to the remote machine so that the two of them can communicate quite happily with one another.

To generate the key pair, enter:

ssh-keygen -t rsa

The machine will ask you to enter a file name in which to save the generated key – just hitting enter will force it to create the key file in its default spot. It will then ask for a passphrase, which you can happily skip by just pressing enter twice.

The system will then spit out its report and reveal to you the location where it saved the key file (which always ends .pub by the way)

You then need to put the generated key data into the ~/.ssh/authorized_keys file of the user which you will be ‘logging in’ as on the remote machine.

A simple way to do this is to use the ssh-copy-id function. Simply call:

ssh-copy-id -i ~/.ssh/id_rsa.pub username@host

Once done, you can test this by ssh’ing into the remote machine and checking the existence of the ~/.ssh/authorized_keys file.

Okay, and now we are ready for the actual SCP file copy bit!

To copy files to a remote machine, simply use:

scp -v /myfilestocopy username@host:/directorytosave

Alternatively, to pull down files from a remote machine, use:

scp -v username@host:/myfilestocopy /directorytosave

Note that the -v switch isn’t necessary – I just like seeing on the screen that something is actually happening!

And there you go, as simple as that. Nifty.

Related Link: http://www.howtogeek.com/