Sunday, December 14, 2008

SSH login without password

If you want to make a bakup script like the one I showed you yesterday, you may need to set your machines to login remotely without asking for a password everytime. This is done by sharing between them the public rsa keys generated by the openssh server.

Login as the user you will use for the transfer script and run:

ssh-keygen -t rsa

Answer the questions but do not enter a password because otherwise you will still need to to pass it when the script runs. You should then have two new files in ~/.ssh, id_rsa and id_rsa.pub. Open id_rsa.pub with your favorite editor and copy everything (there is only one line actually) into the ~/.ssh/authorized_keys on the second machine (the one in which you need to copy).

Now try to login and if everything was set properly it won’t ask you for a a password anymore.

Simple script to backup virtual machines in linux

Currently I am using a small VMWare Server hosting 4 virtual machines, and i found my self in the position of backing them up. But the virtual machines need to be stopped before you can copy them somewhere else. Using the getstate() output of the vmware-cmd you can find the state of each virtual machine. using the following script you can set a cron job to backup your vitual machine:


#!/bin/sh
PATH="/srv/vmware-server/Contabilitate/Windows XP Professional.vmx"
CMD=$(vmware-cmd "$PATH" getstate)

case $CMD in
"getstate() = on")
vmware-cmd "$PATH" suspend
rsync -avz --stats --progress -e ssh /srv/vmware-server/Contabilitate root@192.168.0.3:/mnt/bak/04
rsync -avz --stats --progress -e ssh /srv/vmware-server/Contabilitate root@192.168.0.9:/var/bak/04
vmware-cmd "$PATH" start
;;
*)
esac

Export CSV from MySql Database via SSH

There is a time when you need to export a specific table from a mysql database. Through SSH this is made very easy using the following command:

echo “select * from table_name;” | mysql -u root -pyourpassword database_name | sed -e ’s/^Mn/r/g’ > /home/exported.csv

Of course you can make a small script that adds the date or other usefull information to the filename:

#!/bin/bash
#This scripts adds date to the exported CSV
NOW=$(date +”%m_%d_%Y_%H_%M_%S”)
echo “select * from table_name;” | mysql -u root -pyourpassword database_name | sed -e ’s/^Mn/r/g’ > /home/exported_$NOW.csv

Save this script as export_csv.sh and make it executable, and that’s it.
Your Ad Here