— tomauger.com

Archive
Unix / Linux

In continuing my tradition of mySQL / bash shell one-liners (and posts with arguably SEO-friendly but embarassingly long titles) here’s one that just popped up today. I wanted to dump only selected tables from my database (full disclosure: all the tables relating to a particular WordPress plugin). Knowing I had done something similarly, I popped over to this post and modified it accordingly.

mysql -uUSERNAME -pPASS DBNAME --skip-column-names -e "select table_name from information_schema.tables where table_schema not like 'information_schema' and table_name like 'wp\_visual%';" | xargs -t -I {} mysqldump -uUSERNAME -pPASS DBNAME {} | gzip > dump_VFB-only_2012-11-20.sql.gz

Xargs and I are still dating on-and-off, (well, more off than on), but when I need her special kind of loving to get my freak on, there’s really no other game in town. Don’t be confused by the -t output. I was. It looked like 3 separate files, and I was basically expecting only the last of the tables to actually find its way into my gzip (that sounds ruder than I intended. Excellent) but that was not the case.

If you find yourself vaguely offended or disturbed by my thinly-veiled sexual references to Unix shell commands, then you do not love your code. Or sex. Not sure which is worse.

Read More

If you’re using DreamWeaver without its built-in Subversion support, or for whatever reason need to add newly-created files (that may still be checked out) to the repository, this one liner will do the trick:

svn st | grep "^\?" | grep -v ".LCK$" | awk '{print $2}' | xargs svn add

Let’s break it down, by pipes (reading left to right):

  1. Give me a subversion status: a list of all files that have been added, modified, or otherwise
  2. Using grep, only select those that start with a “?”, namely those files added to the working copy, but not yet under version control
  3. Using reverse grep (the -v switch), exclude any of those records where the filename ends with “.LCK” (the DreamWeaver lock file that indicates something is checked out)
  4. Using AWK, only output the second field (which translates to the filename without all the SVN flags in front of it)
  5. Using XARGS, pipe this (now filtered) list of files to the svn add command.
Read More

For those of you that just want the straight goods, here’s how we delete everything EXCEPT the ‘backup’ directory:

find . -maxdepth 1 ! -name 'backup' ! -name '.*' | xargs rm -rf

Here’s a bit of a (simplistic) explanation:

  1. ‘find’ is the magic sauce here. We’re using it to recursively search through all the files (and directories) starting at ‘.’ (the current directory)
  2. the ‘!’ is the negation operator, which tells find that the operator that follow (-name) should actually perform a negative match (match everything that does NOT match this criteria)
  3. we also need to set up a negative match for ‘.’, ‘..’ etc, since find returns those files as well. Do note that one side effect here is that it won’t delete, for example, ‘.htacess’. You may need to modify this if you want to kill those ‘hidden’ files as well
  4. find is recursive, so even if it doesn’t attempt to delete the ‘backup’ directory, it will still traverse the ‘backup’ directory and delete all the files inside it, leaving an empty directory! To avoid this, we use -maxdepth 1 which effectively turns recursion off. We then make sure we recursively delete the files in the OTHER directories by using the -r flag on ‘rm’
  5. xargs is one way to ‘do’ something useful with the list of files and directories returned by find (find also has an -exec operator which will be able to do almost exactly the same thing, but I like xargs’ syntax better, personally). We pipe the output to xargs and then follow it with the command that we would want to run once per file/directory, in this case ‘rm -rf’

Have fun! And please backup your shit before trying this, cause one mis-step and you can trash a whole lotta stuff!

Read More

There’s probably a better way, but I do most of my svn commands from the root of the project (eg: svn update). I really hate fully qualifying a path when I want to add a new file to version control, when that file clearly has a unique name. Is it a sign of hacker laziness that I would rather type 30 extra characters of shell code rather than type 25 characters of path name?

 

find -name 'my_unique_file_name_to_add.php' | xargs svn add
Read More

Okay, this has been a holy grail for me for a while, and just came to a head today as I accidentally copied a database containing about 15 separate WordPress installations (with different table prefixes) into what was supposed to be a dedicated single WP database. Ouch.

A little while ago I blogged about how you can avoid this situation altogether by only mysqldumping selected tables based on a pattern match of their prefix. Well, guess who didn’t drink his own milk (is that even an expression)?

So there I was, with a database full of wp_ and wpclientA_ wpclientB_ zg_ etc. All I wanted was to keep the original wp_ tables and just drop the hell out of the rest of ‘em.

Read More

Here’s a quickie. For those of you finger hackers out there, this one-liner will look through all available logs (including backups) and come up with a count of unique visitors. Just in case your built-in server stats aren’t working and you haven’t signed up with Google Analytics.

zcat access.log*| awk '{print $1}' | sort | uniq -c |sort -n | wc -l
Read More

Depending on your web hosting situation, you may find yourself having to share a database among multiple WordPress installations, or perhaps other tables that have nothing to do with WordPress whatsoever. This is usually not a significant issue for your day-to-day because WordPress allows you to prefix your WP tables to be anything you like.

The challenge comes with backup and migration, particularly if you’re doing it manually via the shell command line.

Suppose you have a whole bunch of WP installations on a staging server awaiting review, all sharing a database. Now client XYZ has approved the site and it’s time to go live. We’ll take a mysqldump of just those tables and migrate that to the new server.

mysqldump takes, in addition to the authentication (-u and -p – and possibly -h) and database arguments, can take a list of tables to be included in the export. Once you supply even one table, any table not named is ignored. The problem is, particularly if you use plugins that create their own tables, the fixed list of tables can be a moving target. Can’t we select all tables with a given prefix?

Read More

Oh man, the title says it all. Or does it? There’s such a thing as trying too hard to Google-optimize your blog posts, I suppose.

The challenge: while Bourne shell scripting, you are executing a perl script or some other arbitrary command wherein you wish to use the backtick (`) character AS a backtick character. By default, in Bourne, the backtick executes a command.

When you put commands inside a heredoc quote (ie: do something << EOF … code goes here … EOF) apparently the entire contents of the heredoc are parsed. If it encounters a naked `, the code following that backtick will be evaled right away, not even in the context of the execution order of the heredoc. This can be problematic.  We need a way to escape that sucker so that it is NOT executed during the heredoc parse operation.

Read More