Monday, May 28, 2018

Backup and Restore Your Linux System and Home Directory

It's a good idea to regularly back up the files on your computer in case there is ever a hard drive failure.  OSX and Windows comes with basic backup software that is very easy to use.  Simply connect and external USB drive to your computer and enable the backup software.  Google even offers the ability to back up to your Google drive so your data is stored at a different physical location.

Linux system can be backed up very simply through the use of a command line tool called rsync and with a little scripting the backup process can be automated just like it is on OSX and Windows.

Encrypting the Backup Drive

Before backing up your files, it is a good idea to encrypt the external drive where you'll be storing your backups.  This is to prevent someone from taking the external drive and accessing your files.  Use the Disks utilities and select the drive and change it to encrypt the partition.  Note that this means that in the future only a Linux system (along with the password) can access the external drive.

Backing Up the Home Directory

I create two separate backups:  system and home.  The system is to backup my system configuration while the home directory contains the users data.  There are a lot of files and directories in the home directory that doesn't need to be backed up that might be consuming a lot of disk space.  These include cached files, trash files, etc.  It's best to exclude these files and directories. 

I found this file as a good starting point for things to exclude from the home directory.  With this files (modified to your liking), use rsync to back up the home directory (this example is to back up just your own home directory):

rsync -aP --exclude-from=[exclusion file] /home/$USER [path to backup location]
Make sure that you've mounted the external drive and have the path correct.  Use the 'n' option (e.g. rsync -naP ...) to do a dryrun where it it doesn't actually copy any files so you can test first.

If it all works correctly, a crontab to automatically repeat the process can be made although you still need to mount the drive with the encryption passphrase unless you set it up where the cron job has access to the passphrase.  I just create a bash script that I run manually.

Backing Up the System

Backing up the system is very similar except there are some additional directories to exclude and the starting point is "/" instead of the home directory.  

1.  Create a file similar to the one for excluding home directories (see above).  This file is more simple since it only contains directories:
/dev
/proc
/sys
/tmp
/run
/mnt
/media
/lost+found
/home
Then you'd run (again, use -n first to do a dry run):

sudo rsync -aAXv / --exclude-from=[file of excluded directories] [path to backup location]
Note that this time it uses the "sudo" command since some systems files requires SUDO privileges. 

Sunday, May 20, 2018

WebAssembly (WASM) with Go

This is the first time that I've tried to compile the Go compiler so I figure that I'll document it here for others who might be wanting to try WASM with Go before the official Go release in August.

The next release of Go (1.11) is scheduled for Aug and it will be first time WebAssembly (WASM) will be supported.  WASM will be a build target much like how one builds a binary for other machine and operating systems except that in this case the machine is a "virtual" machine that runs inside the browser.

I've been very excited for WASM and I've been eagerly waiting for the time when I can use Go to write WASM.  The branch for the 1.11 release has is now locked for only bug fixes so I figured that it might be a good time to try writing a WASM code with Go.

Note that this first release for WASM isn't going to be "production" ready.  I don't think it'll be very optimized and WASM itself is only at its MVP release it doesn't allow WASM code direct access to the DOM or the browser APIs so all calls is still going through JavaScript.

Unfortunately, as of this writing, the code in the Go repo doesn't work for WASM.  The work on WASM for Go is primarily being done by Richard Musiol (neelance), the author of GopherJS, so I decided to get the version Go from this GitHub repo.

First, you will need a version of Go on your system to compile the source.  The easiest way is to download and install the binary distribution from the main Go website.

Until 1.11 is released to get WASM is to build from source.

git clone https://go.googlesource.com/go
cd go
Once you have the source, it's time to compile it.

cd src
./all.bash
If everything is built correctly then there will be a new "go" binary in ../bin.  Also, there are two files to help you get started with loading future WASM files in ../misc/wasm.

Create a test.go file:

package main

func main() {
    println("hello, wasm")
}
Compile to WASM with:

GOOS=js GOARCH=wasm go build -o test.wasm test.go
Make sure you're running the go command that you've just built and not the version that you installed originally.

In the same directory with your test.wasm file, copy over wasm_exec.html and wasm_exec.js from misc/wasm.

The browser will expect the test.wasm file to be served with a MIME of application/wasm so you'll need to add the following to your /etc/mime.types file:

application/wasm wasm
To run it in your browser, you'll need to have a web server.  Creating one with Go is easy, or you can also use Python with the following command.

python -m SimpleHTTPServer

Point your browser to your web server and load wasm_exec.html.  Click on the button and you should see "hello, wasm" appear in the browser's console.

I've been experimenting with WASM here.