Showing posts with label Bash. Show all posts
Showing posts with label Bash. Show all posts

Tuesday, 14 October 2014

MySQL data sample

Hi,

so today I didn't find any (well at least without iterating through mysqldump) way to export a subset of a database tables so I wrote the following bash script:

partial-mysqldump
  1. usage() { echo "Usage: $0 [-h <host>] [-u <user>] [-p <pass>] [-d <database>] [-t [nametables]] [-f <output_file>] [-l <limit>]"1>&2exit 1}
  2. while getopts ":t:f:d:l:p:u:h" opt; do
  3.     case "${opt}" in
  4.         f)
  5.             f=${OPTARG}
  6.             ;;
  7.         d)
  8.             d=${OPTARG}
  9.             ;;
  10.         p)
  11.             p=${OPTARG}
  12.             ;;
  13.         h)
  14.             h=${OPTARG}
  15.             ;;
  16.         u)
  17.             u=${OPTARG}
  18.             ;;
  19.         l)
  20.             l=${OPTARG}
  21.             ;;
  22.         t)
  23.             t+=("$OPTARG")
  24.             ;;
  25.         *)
  26. echo
  27. echo "[ERROR]: Unknown, ignoring ${OPTARG}"
  28.             usage
  29.             ;;
  30.     esac
  31. done
  32. shift $((OPTIND-1))
  33. if [ -z "${h}" ]then
  34.     h='127.0.0.1'
  35. fi
  36. if [ -z "${l}" ]then
  37.     limit=''
  38. else
  39.     limit="where=\"1 limit $l\""
  40. fi
  41. if [ -z "${d}" ] || [ -z "${t}" ] || [ -z "${f}" ]then
  42.     usage
  43. else
  44.    for val in ${t[@]}do
  45.      mysqldump \
  46.        -h $h \
  47.        -u $u \
  48.        -p$p \
  49.        --no-create-info \
  50.        --databases $d \
  51.        --table $val \
  52.        --lock-tables=false \
  53.        $limit \
  54.      >> $f
  55.    done
  56.    gzip -9 -c $f > $f.gz; rm $f
  57. fi


so now, i can just limit my data and export what I want.

./partial-mysqldump -u <user> -h <host> -p <password> -d <database> -t <table1> <table2> <table3> -f test -l 1000

will dump first 1000 rows from table1, table2 and table3.

The problema with mysqldump --where="1 limit <yourlimit>" option is that it counts the overall result set and not a limit for each table.

Cheers
Tiago






Wednesday, 8 October 2014

Productivity boost

So, how can I boost my productivity today?

let's try some bash shortcut with "fasd"

Fasd (pronounced similar to "fast") is a command-line productivity booster. Fasd offers quick access to files and directories for POSIX shells. It is inspired by tools like autojumpz and v. Fasd keeps track of files and directories you have accessed, so that you can quickly reference them in the command line.


$ brew info fasd
fasd: stable 1.0.1
https://github.com/clvv/fasd

$ brew home fads

## INSTALL

$ brew install fasd
$ echo eval "\$(fasd --init auto)" >> ~/.bash_profile
$ source ~/.bash_profile
$ cd ~/Desktop/
$ cd -
$ a
$ z desk

see the point now?


just check https://github.com/clvv/fasd and a lot more will come from there






Wednesday, 2 March 2011

First Post

This blog started today because a friend asked "what is /dev/null ?".

So, why start a blog after that question? First of all, there is so much "garbage" information on the Web nowadays. Information that should be redirected to the null device :-P
Secondly, I've started talking with Pedro Gomes about the idea of create a blog with useful geek information. Like useful hacks, informational stuff, programming stuff, etc.

Besides sharing useful information with the world, it is a good repository!

Oh, btw /dev/null it's a virtual file of Unix-like operating systems. This special file discards all the information written to it but can be used for reporting successful writing of data.
It can be used to redirect the unwanted output of a program to it. It's called the black hole in computer jargon.

Psst, if you log in a machine and you don't want that your commands are logged, just redirect .bash_history to the black hole ;-)