Monday 13 July 2020
Powerful Terminal Commands to have in your Toolbox
When deploying applications to servers, most of the time you'll be working in the Terminal, or command-line. Yes, the scary (or cool) black screen, which looks like the computers of 50 years ago.
Although the Terminal looks very outdated, it is actually much more powerful than the GUI (Graphical User Interface), as you have much more commands and powers.
Here are some recent commands I used in my work.
Copying Files Between Servers
If you want to copy a file from your server to your computer, you can't just drag it from your Terminal onto your desktop, as that only works in the GUI. Get ready to meet the much more powerful scp command. scp stands for Secure Copy.
Here are some examples:
copy a file from a server to your local machine
scp [email protected]:/path/to/foo.txt ~/Desktop/foo.txtcopy an entire folder (recursively) to your local machines' Desktop folder
scp -r [email protected]:/path/to/foo ~/Desktop/copy a file from server-1 to server-2 (AMAZING!)
scp -3 user@server1:/path/to/file.txt user@server2:/path/to/file.txtOf course, you need to have access to the servers (e.g. SSH keys) for the command to work.
Now try copying files from an AWS server to a Digital Ocean droplet with a GUI 🤓 ...
Copying MySQL Databases between Servers
Today I moved an application from a cheap Azure server to a bigger & better Digital Ocean droplet (just a fancy name for a server). The problem was that the client already added some content in the application, that was saved to the MySQL database on the Azure server. So I needed to copy the database to the new server.
Luckily it's very easy to do:
- Create a dump of the database on the old server:
mysqldump database-name table-name > dump.sqlYou can omit table-name to dump the whole database.
- Copy the
dump.sqlfile to the new server (usingscpmentioned above 😎). - Import the database on the new server:
mysql database-name < dump.sqlDone!
By the way, the dump.sql that mysqldump generates is very human readable & nicely formatted!
This is it for now, I might add more to this post in the future!