Take Your Linux Workspace Anywhere: Backup for Nerds

How many times have you sat down at a new Linux computer and felt like you were starting all over again? All of those aliases, every little helper file and script — gone. This new computer is a shell of its former self (pun intended). Your heart warms as you think back to the comfort and productivity that came with your Linux workstation at home. If only there were a way to take everything you know and love on the go…

Thankfully, there is!

If you don’t feel like setting it up yourself, you can clone the pre-finished skeleton I made on Github.

All of your Tools, Mobile

Essentially, we will be creating a set of common files and scripts that will enable you to clone any code repositories you may have stored online. All of it will be version-controlled with Git, meaning that you can track any changes made.

There are two parts to this little code contraption. The first is sync-home.bash. This script provides a way to track changes for important files in your home area, such as .bashrc, .vimrc, and the like. You simply edit these files from the home folder within the repository and commit/push your changes. This keeps all of your files neatly tracked. Meanwhile, sync-home.bash will copy them into place whenever you want so that you can start using them.

The second script packs a punch: clone-repos.py is what makes this all work . As input, it uses a comma-separated list of repositories stored in repos.csv. From this list, it clones these repositories one-by-one into the wkdir directory (shorthand for working directory).

This is a smooth solution if you frequently find yourself writing code or developing software of any kind. For me, the most time consuming part of making this happen was organizing all of my existing files into Git repositories and pushing them to websites like Bitbucket and Github.

Going Remote

This is why it is so important to get into the habit of working within a Git repository (or some other kind of version control). Even if what you’re doing isn’t particularly structured, you can always set up a general-purpose practice repository where anything goes. The main idea is to get any kind of code that you touch into a repository so that you can upload it to Github, Bitbucket, or wherever else you may store code online.

Once it’s been pushed to one or more sites, the files are available in multiple locations, so you can rest assured that they’re safe. When there’s a fire, your laptop is stolen, or you finally chuck the poor thing across the room in anger, you’ll be glad that all of your precious code is backed up the smart way.

Setup for Success

First things first — as mentioned above, you’ll want to make sure all of the repositories that you want access to in your workspace are uploaded to some kind of external source code hosting service, whether it’s self-hosted or one of the big names.

Handling Home Files

#!/bin/bash
WARNED=false
for f in `ls -A home`; do
if
[ -e ~/$f ] && ! $WARNED; then
echo Warning: Files will be overwritten if you continue.
while true; do
echo -n "Continue? (y/n): "
read user_in
if [ "$user_in" = "y" ]; then
WARNED=true
break
elif [ "$user_in" = "n" ]; then
exit 0
fi
done
fi
echo Copying home/$f into place...
cp home/$f ~
done
echo Done

This script doesn’t do a whole lot. Basically, it loops through every file in the home directory of your workspace. For each of these files, it first checks if the file already exists in your real home area. If it does, it warns you that your files could be overwritten. If you understand the risks and would rather have the script run to completion without asking for permission, just comment out lines 4–16 above.

After checking in that you’re okay with files being overwritten, the script will copy everything in the linux-workspace/home folder into your actual Linux home area (~). With this, all of those little files, like .bashrc and .vimrc, will be instantly available. This means all of your personal customizations to the shell, such as shortcuts, aliases, and other related features, will now be available to you.

Lasso your Repos

First, we will create a list of repositories. You can format this list however you want, but any structural changes must be reflected in the script we’re about to write that actually performs the sync. For mine, I just created two columns in a CSV format. The first column is the local location where the code will live within your linux-workspace. The other is the URL to the remote source of the repository, where we will be cloning from. Here’s a sample from mine:

github/stephengrice, git@github.com:stephengrice/linux-workspace

In the above example, the local directory github/stephengrice will be created, and linux-workspace will be cloned into it from the remote address.

Now we just need a script to sync things up.

Sync Simply

Once you’ve done that, push every repository — and not just the master branch. Use git push --all to ensure that everything on your local computer is also on the remote site. When this is the case, we can regenerate all of our local repositories with our sync script. After that, we can delete the originals, since the newly cloned repositories are identical.

With all of that said, here’s our sync script:

#!/usr/bin/env python3
import csv, os
WORKING_DIRECTORY_NAME = 'wkdir'
if __name__ == "__main__":
with open('repos.csv') as file:
csv_data = csv.reader(file, delimiter=',')
for row in csv_data:
remote_path = row[1]
local_path = row[0]
if os.path.exists('%s/%s' % (WORKING_DIRECTORY_NAME, local_path)):
print('Already exists: %s' % local_path)
else:
print('Cloning repository...')
command = 'git clone %s wkdir/%s' % (remote_path, local_path)
print(command)
os.system(command)

My college programming professors would have a heart attack. No comments! What kind of monster have I become?

Not to worry, professor — the script is actually fairly clear cut. Allow me to document it in the next few paragraphs.

The top of the script is defining which Python interpreter to use, importing libraries, and setting constants. The real magic begins when we open repos.csv. You may recall that this file contains two columns - first, where we want to store the repo locally, and second, the address of the remote repository we’ll be cloning from. For each of the repositories in the file, it checks if it exists locally, and if it doesn’t, it clones the remote.

If you get tired of typing out your password every single time, it may be a good idea to copy your public key file (located in ~/.ssh/id_rsa.pub) into your Github and/or Bitbucket profile. You may also be interested in pushing out your SSH keys if you have local Git servers.

I decided to use Python, because it’s quick, straightforward, and a lot more powerful than Bash (no offense to die-hard shell-scripters!). Feel free to make your own script in whichever language tickles your fancy.

Alias Setup

While you can use whatever name you want for this alias, I named it wkdir. To get this alias working, just edit your .bashrc file in the home directory of the repository. Add this line:

alias wkdir='cd ~/linux-workspace/wkdir'

Type the following to finish settings things up:

cd ~/linux-workspace
./sync-home.bash
source ~/.bashrc

Now, try it out! Just type wkdir, anytime, anywhere.

Pulling It All Together

Initial Setup

  1. Set up repos.csv. This defines where you want each repository to be stored on your local machine. This should only be necessary one time, unless you add new repositories.

Use

  1. Run sync-home.bash to add all of your home files and aliases to this computer, if you defined any. If you want immediate access to your aliases, you can run source ~/.bashrc. This way, you won’t need to restart or reopen your terminal session.
  2. Enjoy your new work environment! Switch to your work area, choose a repository, and get coding.

With all these tips in mind, you can move away from cloud storage for code repositories and Linux files. You now have the power to forge your own path, and customize the way you do work to your heart’s content. Happy computing!

Do you enjoy learning about programming and computer engineering? If so, Line by Line Code is the place for you! Click here to visit.

Originally published at linebylinecode.com on November 24, 2018.