Friday, September 20, 2013

Disk space not used, but not free. Where did it go...

I recently pulled out almost all of my hair, searching for lost disk space on a Ubuntu box.

"df" showed that almost all space on disk was used, but i was not able for find the files taking up all the space. Of course I went through all the usual suspects (http://ubuntuforums.org/showthread.php?t=1122670), but in the end cause was very simple and yet not obvious.
Side story; the box runs a installation of MySQL that had been mangled when trying to import a database from another box. The MySQL files had been deleted, but I forgot to shutdown the MySQL daemon. So all files used by MySQL was still held by MySQL. This led to disk space being used, but not found when searching for files. the trick to discover the used space was the following:

lsof | grep "deleted"

I know it is a obvious error, but I use a few hours to find this.So heres the tip for you.

Monday, September 9, 2013

Simple copy of file between linux boxes with netcat

I am still pretty new all this Linux sysadmin stuff, so when I get new tricks to to help me I clap my hands and quickly write it down. So here goes.

If you need to copy large amounts between instances, you might not have access to copy between the specified instances via scp. Here netcat, nc, can help you. It opens a direct connection between the two instances, so you can stream bit from one to the other.

Example

I want to send file1.txt from instance A to instance B.

On instance B do:

$ tar -cf - . | nc A_ip 4000

On instance A do:

$ nc -l 4000 | tar xvf file1.txt

If you need to send over a lot of files you can use the following on instance A:

$ nc -l 4000 | tar xvf -

If you are sending a a huge amount of data, you might need to throw gzip and gunzip in to the mix:

$ tar -cf - . | gunzip | nc A_ip 4000
$ nc -l 4000 | gzip |tar xvf file1.txt

Of course you need to make sure the ports are open for communication, but most instances have at least one open port that can be used. Just make sure the port is not use by other programs.

Note that netcat syntax differs a bit between the original unix version and the OpenBSD version. The above examples are in the OpenBSD variant.

Reference

 - http://www.commandlinefu.com/commands/view/2536/using-netcat-to-copy-files-between-servers