One-liner to duplicate database over network

Here is a handy little bash pipeline that I used to transfer a MariaDB database from a development system (in the USA) to the production system (in Europe).  It’s really a one liner, but this website isn’t wide enough to display it all on one line:

mysqldump -u root --password=<password> --databases <databases> |
ssh <user@target> "mysql -u root --password=<password>"

Note that this will drop any existing database on the target system with the same name as the one being duplicated.

If you use public-key cryptography rather than passwords for ssh authentication, then this will run with no user input necessary at all.

For a demonstration, use the following shell scripts:

Script to create example database:

#!/usr/bin/env sh
mysql -u root -p <<quit
create database lemon;
use lemon;
create table lime (field1 VARCHAR(32), field2 int);
insert into lime values ("zesty", 42);
quit

Script to copy example database to another system:

#!/bin/bash

# Prompts the user for input, stores input in variable, uses existing value if
# user provides no input.
# Parameters: prompt, default value, variable name, [optional: silent?]
function prompt {
        local def line silent
        def="$2"
        [ -z "$def" ] && def=`eval echo -n '$'"$3"`
        [ -z "$def" ] && val="" || val=" ($def)"
        echo -n "$1$val: "
        [ "$4" ] && silent="-s" || silent=""
        read $silent line
        [ -z "$line" ] && eval $3="$def" || eval $3="$line"
        [ "$silent" ] && echo ""
}

# Default values
DBNAME=lemon
DBUSER=root
DBPASS=
SSHOST=

# Get input from user
prompt "Database name" "$1" DBNAME
prompt "Database username" "$2" DBUSER
prompt "Database password" "" DBPASS "-s"
prompt "SSH target host" "$3" SSHOST

# A nice one/two-liner (well one-line if you replace the variables with useful
# values, and ditch the above code)
mysqldump -u "$DBUSER" --password="$DBPASS" --databases "$DBNAME" |
ssh "$SSHOST" "mysql -u \"$DBUSER\" --password=\"$DBPASS\""

This script assumes that an SSH server is enabled on the target machine, and that the MySQL root passwords are the same on both systems.

Then to see the copied database (run on the target system):

echo "select * from lemon.lime;" | mysql -p

Or if you can’t be bothered opening an interactive shell on the target system:

echo "select * from lemon.lime;" | ssh <user@target> "mysql -p"

Javascript: hierarchy and inheritance in a prototypal paradigm

While seen by many as some messy little web page language, Javascript has a fairly unique elegance to it when put alongside other object-orientated programming languages (C#/Delphi/Python/Java/C++).  This uniqueness is part of the reason for the generally negative stigma towards the language. The prototypal inheritance model is unintuitive to those (such as myself) who have come from a classical inheritance background, and the weak dynamic typing allows beginners with little OOP knowledge to rapidly produce working but extremely messy, inconsistent, hackish code that reciprocally convinces them (and others) that Javascript is a messy, inconsistent, hackish language.

One only has to look at the booming node.js movement to see that this strange little language must have some positive side though.  To illustrate the object model and inheritance in Javascript, I put together a little script (on repl.it) to demonstrate the prototypal model by example (specifically, by emulating a traditional hierarchical paradigm), which may be viewed and run here.  When run, the script creates a list of Javascript built-in classes, in addition to some classes “derived” within the script.  A tree of these is then displayed in the console, showing the inheritance hierarchy:

Output

Output

Source

LaTeX neural networks

A LaTeX/TikZ/PGF package for drawing directed graphs, such as neural network schematics.

I started a project to create an open-source mid-level Machine Learning textbook, based on some notes from a Caltech course and a Coursera course. The contributions from the community were of poor quality and laden with mistakes, so I eventually terminated the project (having rejected all public submissions). I will work on the book as an occasional project for my own benefit, but it is not a priority any more. I have stopped working on the book.

Neural network implementation of an exclusive-or (XOR) logic gateTo rapidly produce neural network illustrations in the book, I created a LaTeX package to wrap all the TikZ/PGF clutter. The result is a set of LaTeX macros that allow high quality neural-network graphs to be drawn rapidly, and I have since made the package publicly available via my github repository.

Available on CTAN: /graphics/pgf/contrib/neuralnetwork
Download:
LaTeX neural networks package
Latest version (github): battlesnake/neural

Neural network with two hidden layers