Monday, June 21, 2010

Script to restore DB from backup

Here is a small snippet for speeding up the process of restoration of DB from backup gunzip files

db.sh

#!/bin/bash
FILE="$1"

MYSQL=$(which mysql)
MYSQLAD=$(which mysqladmin)
GZIP=$(which gzip)
MUSER="username"
MPASS="password"
MDB="database"

echo "Droping database..."
$MYSQLAD -u $MUSER -p$MPASS drop $MDB

echo "Recreating database..."
$MYSQLAD -u $MUSER -p$MPASS create $MDB

echo "Uncompressing data..."
$GZIP -d $FILE".gz"

echo "Inserting data..."
$MYSQL -u $MUSER -p$MPASS $MDB < $FILE



USAGE:
:~/ chmod u+x  db.sh
:~/ ./db.sh mysql-timestamp.sql (Remove the .gz extension while providing as a parameter)

I will add help and more parameterized functionality in due time.

Friday, January 22, 2010

SSH, Scp, RSync, SSHFS

From days i had thought of working over it. But i thought i would do it when it would be a real requirement. But alas, they were the most interesting, easiest and handy tools for me.

SSH: Makes you the owner of a SSH enabled system through network

:~/ ssh username@ipaddress -v  (and then give the password)
-v option gives you a verbose output which helps us to understand the background

For passwordless login you can generate a public key and install it in the host system.

:~/ ssh-keygen -t rsa -f filename

this generates a filename.pub in ~/.ssh directory. Then you need to copy the content of the file filename.pub into the host file .ssh/authorized_keys

SCP: This tool allows you to copy files over ssh

for a single file
:~/ scp sourcefile destination

or for a directory
:~/ scp -r sourcedirectory destination

RSync:  A fabulous tool to sync files between directories using ssh. Huge amount of options are there to enable or disable certain feature. Most used feature would be just updating on local or remote directory to another one.

for local directories
:~/ rsync -auv sourcedirectory destinationdirectory

 or else you can do between a remote location
:~/ rsync -auv user@ip:relativepath/sourcedirectory destinationdirectory

The above one's don't delete destination files which are not in the source.

Once we got max speed @ 12 MB/s in our 100 Mbps network while backing up a computer. Most of us were astonished as the content was multiple files. Have to research about rsync.

SSHFS: This tool enables us to mount the host system directory into our filesystem

:~/ sshfs user@ip:relativepath/sourcedirectory destinationdirectory

This enables us to treat the remote directory as a part of our system. Share your thoughts and also post info on some other ssh tools.