Milan Dinic's blog
This blog is about software development and tech problems and solutions. It is not restricted to any technology, although I guess it will contain more posts related to java and java based technologies, since I'm quite a fan of java.

Wednesday, March 23, 2011

Automate ssh login and copy

Expect is a unix tool that can be used for automating ssh operations such as login, switch user, secure copy etc.

expect tool can be installed with command:

sudo apt-get install expect


Expect tool has own commands that can be used in its scripts, here is the list of the ones that are essential:
spawn - to execute another program
expect - to wait for text on the screen
send - simulate typing on keyboard, e.g. typing password
interact - leave the expect mode and allow user to continue work

Expect tool can be used with input file, and it ca be called like this:

expect -f script_name

Here is the example of script for remote login:

set user "your_user_name"
set pass "secret"
spawn ssh ${user}@${server}
expect ": $"
send "$pass\n"
interact


And here is the example of script for secure copy of several files to a remove machine that are in different directories:

set timeout 1200
set src "/home/user/workspace/"
set user "your_user_name"
set pass "secret"
set paths {path1/file1.war path2/file2.war}
set server 192.168.0.100
set spath :~/

foreach {p} $paths {
spawn scp ${src}/${p} ${user}@${server}${spath}
expect ": $" {
send "$pass\n"
}
expect ":~"
puts "\r\n"
}

Sunday, February 20, 2011

Speed up MySql on Ubuntu Linux for devs

MySql can usually be fast enough for development, but when project become huge and you have a lot of tests that require database access in their execution, you might consider moving mysql datadir to a ram drive. Anyway, who does not want a faster development environment.

Ubuntu linux, by default has a ram drive (shared memory drive) that can be found at /dev/shm. This drive is mounted as read/write drive, with permission to execute programs. More information about shared memory drive can be found on ubuntu community site.

Here is a detailed how to move data folder on mysql, on ubuntu linux.
the only step that I would skip is removing original data directory, here's why.

The problem with this kind of setup is that each time you reboot or shutdown the OS, data from database will be gone and next time you start the OS, mysql will not work at all.
The solution is to make a startup script that creates copy of original data directory on ram drive. All startup and shutdown scripts should be created in /etc/init.d directory.

content of my2shm script
cp -R -p /var/lib/mysql /dev/shm

my2shm can be set to run on system startup by executing:
sudo update-rc.d my2shm defaults

Now, only one more thing is left to do, save data from ram drive to hard drive on shutdown. Here is the content of the script that does this work:

content of my2original script:
cd /var/lib/mysql
rm -rf *
cp -R -p -f /dev/shm/mysql /var/lib/

Script my2original can be set to run on system shutdown by executing:
sudo update-rc.d my2original defaults 21

By defaults update-rc.d will set priority to 20, that is why shutdown script needs to have higher value e.g. 21.