DEV Community

Cover image for 5 Tips for avoiding mistakes while working with server
cuongnp
cuongnp

Posted on

5 Tips for avoiding mistakes while working with server

Have you ever accidentally deployed code to the wrong environment, sending shivers down your spine? Or maybe you've hit Enter too quickly on a rogue command, leaving you scrambling to fix the aftermath? We've all been there, fellow developers. But fear not! Here are some practical tips to streamline your server workflow and minimize those heart-stopping mistakes.

1. Theme Up Your Terminals:

Setting up the different colored themes for your development, staging, and production environments can act as a visual cue, preventing you from mistakenly running a production command in your dev terminal. I love using iTerm2 and setting up profiles for each environment.

5 Tips for avoiding mistakes while working with server-2

5 Tips for avoiding mistakes while working with server-2

Ref: Setting up theme colors

2. Backup is Your Best Friend:

Before any major server changes, backups are crucial. This includes your codebase, configuration files, and database data. Utilize version control systems like Git for code and consider automated backup solutions for other critical data. Remember, "it's better to have it and not need it than need it and not have it." Here are simple commands you can try to back up everything.

cp -ip /yourfile /.backup/youfile.20240123 
Enter fullscreen mode Exit fullscreen mode

3. Be careful with special symbol:

When copying commands from online sources, be cautious of accidental newlines (\n) embedded within the command. These can trigger immediate execution, potentially causing unforeseen issues. Always double-check your pasted commands before hitting Enter. Here's where a code editor like VS Code. It not only helps you avoid newline headaches but also offers features like syntax highlighting and code completion that can prevent errors caused by misplaced quotation marks (", ') or other special characters.

4. Double-Check Before You Hit Enter:

It's a good habit to verify every command, even routine ones like copying or renaming files. This extra step can prevent accidental data loss. Most terminal emulators offer command history features, allowing you to review previously executed commands easily. My preferred approach is to use ll (long listing) or ls -al . (list all with detailed information in the current directory) to confirm file details before proceeding. Additionally, you can directly specify the file name for verification, like:

ll file-name
ls -al file-name
Enter fullscreen mode Exit fullscreen mode

5. Tame the Long SSH Strings:

Tired of fingers cramping over lengthy server IP addresses? Unlock the power of SSH configuration files to assign friendly aliases to your servers. With a simple SSH [server_alias] command, you can effortlessly connect without any typing marathons or typo-induced headaches. Here's how to unlock this efficiency boost: The config file is usually located in /Users/username/.ssh/config open it and define server aliases

 ~/.ssh ❯ cat config

 # Server production
 Host production
    HostName 18.18x.19x.1xx
    User ec2-user
    IdentityFile key/server-key.pem


# Staging dev
Host dev
    HostName 13.xxx.15x.13x
    User ec2-user
    IdentityFile key/server-key.pem
Enter fullscreen mode Exit fullscreen mode

Thanks for Reading!

I hope these tips help you navigate the server environment more confidently and efficiently. If you have any tips or alternative approaches, feel free to leave a comment below.

Top comments (0)