2015-12-16

Pulling Single DB from Mysqldump's --all-databases backup files

When backing up a MySQL DB, it's often times quick and easy to do a simple:
mysqldump --all-databases --single-transaction | bzip2 > <filename>.sql.bz2

But then someone comes along and asks you "hey, you know that one blog out of 50 that you host? Well, I messed it up and need a restore." You don't want to restore the full dump, but you know you have the data. Here's a quick way to rip out of that file, exactly what you need (after, of course, you extract it from whatever compression you use):

#!/bin/bash
head -n40 $2 | sed -n '/^-- MySQL/,/^-- Current Database:/p'|grep SET > $1-dump.sql
sed -n '/^-- Current Database: `$1`/,/^-- Current Database: `/p' $2 >> $1-dump.sql
$1 = DB Requested
$2 = Input Filename
That script will leave you with a file that has the SET lines before and after the dump, as well as the contents of just the DB you're looking for. Then, just run 'mysql [database_name] < [file_name]' and you'll have your data back to what they wanted!

2015-12-10

How to add custom file extension support in Visual Studio Code for Linux

Microsoft has released Visual Studio Code for Linux (and OSX and Windows) for free (https://code.visualstudio.com/). It's not a bad editor over-all, but I had one thing that urked me. I do a lot of PHP work here and sometimes the include files have the extension .inc instead of .php ... Well, VSCode doesn't seem to think those are php and therefore doesn't highlight them. You'd think it'd read that from the <?php at the top, or infer it from the format, or have some sort of menu to tell it 'treat this as php', but it doesn't. However, we can fix that, and NOT just for PHP!

Open up your favorite text editor and go to your VSCode install directory. Doing an 'ls' should look something like this:
$ ls
Code            content_shell.pak  icudtl.dat        libgcrypt.so.11  libnode.so  
libnotify.so.4  locales            natives_blob.bin  resources        snapshot_blob.bin

Now, 'cd' into your resources/app/extensions/ directory and you'll see all sorts of extensions. For me, it looks a little something like this:
$ ls
bat            ini            perl               theme-monokai
clojure        jade           php                theme-monokai-dimmed
coffeescript   java           powershell         theme-quietlight
cpp            javascript     python             theme-red
csharp         less           r                  theme-solarized-dark
csharp-o       lib.core.d.ts  ruby               theme-solarized-light
css            lua            rust               theme-tomorrow-night-blue
declares.d.ts  make           shaderlab          tsconfig.json
docker         markdown       shellscript        typescript
fsharp         mono-debug     sql                vb
go             node-debug     swift              vscode-api-tests
groovy         node.d.ts      theme-abyss        xml
html           objective-c    theme-kimbie-dark  yaml

Now, most of those are directories, and they have files under them... the one we care about is /package.json ... Open that up in your favorite text editor (like vim) and follow the json object looking for the line under the node "contributes"->"languages" called "extensions"... For instance, Python says:
"extensions" : [ ".py", ".rpy", ".pyw", ".cpy", ".gyp", ".gypi" ], 
php says:
"extensions": [ ".php", ".phtml", ".ctp" ],

etc... Just add in your desired extension (for me, I added the text ',".inc" ') in the object and save it. Restart VSCode and you've got nicely syntax highlighted text!

Update!

As of VSCode 1.1.0+, you can actually add these things in your settings.json. To get there, go to "Preferences->User Settings". It'll open up a big (or small) json object it reads in for your settings. You'll want to add the lines in:

{
  ...
  "files.associations": {
        "*.inc": "php",
    }
}

Save it and you're good to go! (you may need to restart VSCode)