Thursday, November 22, 2007

select statement to csv

Simply put this at the end of your select statement

INTO OUTFILE '/tmp/report.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'

Tuesday, September 4, 2007

Chained Replication MySQL

After an hour of banging my head trying to implement chain replication I found what I was missing on Kevin Minnick's blog.

To allow a slave to act as a master to another slave use the
log-slave-updates option in my.cnf, this along with the other standard settings for master-slave configuration i.e. log-bin=YOUR_BINARY_LOG (in your master my.cnf)

Monday, September 3, 2007

Search and replace string in multiple files

egrep -rl OLD_STRING * | xargs perl -pi -e "s/OLD_STRING/NEW_STRING/g;"

Thursday, August 16, 2007

remove file extensions

for file in *.txt; do mv $file `basename $file .txt`; done

Thursday, August 9, 2007

Finding Broken Symlinks

find . -type l | (while read FN ; do test -e "$FN" || ls -ld "$FN"; done)

Wednesday, August 8, 2007

Ghost your LVM to new drive

1.) Using fisk setup "partitions" on new drive to match the old drive, make sure id and bootablle flags are set as before.
2.) Use dd to copy the data across, make sure the drives are in the correct order or you'll loose everything
prompt>: dd if=/dev/oldhda of=/dev/newhdc bs=446 count=1
prompt>: dd if=/dev/oldhda1 of=/dev/newhdc1
prompt>: dd if=/dev/oldhda... of=/dev/newhdc...
prompt>: dd if=/dev/oldhdaN of=/dev/newhdcN

Shutdown and put the new drive in the old drives place.
New drive should now boot

Wednesday, August 1, 2007

Must have pine???

rpm -ivh http://rpm.livna.org/livna-release-4.rpm
su -c 'yum install pine'

Wednesday, July 18, 2007

lower2upper convert case of filename

Put this in a for loop to convert a directory of files

Using tr
mv filename `echo filename | tr '[:lower:]' '[:upper:]'`

Using sed
mv filename `echo filename | sed -e 's/[a-z]/\U&/g'`

Wednesday, June 27, 2007

Debugging

strace -f -p $(cat /path/to/application.pid) 2>&1|tee trace.out

Wednesday, June 20, 2007

ServerTokens Prod, ServerSignature Off

You don't want apache to display the server version on error pages, or other pages it generates, also you should only return the server name "Apache" in each reply header so add the following 2 directives in httpd.conf

ServerSignature Off
ServerTokens Prod

While it doesn't make your server any more secure, it may make it less of a target.

Tuesday, May 22, 2007

find and print to file with modification

find ./ -type f -print | sed 's/.*/MODIFICATION &/' >> outputfile

Thursday, May 17, 2007

Can't su to root and sure that your pam is configured properly?????

Took me an age to work this out, hopefully this will help.

chmod u+s /bin/su

Thursday, May 10, 2007

Simple Bash For Loop

#!/bin/bash
for

file in $( ls -Q *\ \ * );
do
target=$(echo $file | sed -e "s/\ \ /\ /")
mv $file $target
done

Tuesday, May 8, 2007

Restoring MySQL database

Restore using mysql

If you have to re-build your database from scratch, you can easily restore the mysqldump file by using the mysql command. This method is usually used to recreate or rebuild the database from scratch.

Here's how you would restore your custback.sql file to the Customers database.

mysql -u sadmin -p pass21 Customers <>

Easy isn't it ? Here's the general format you would follow:

mysql -u [username] -p [password] [database_to_restore] < [backupfile]

Now how about those zipped files? You can restore your zipped backup files by first uncompressing its contents and then sending it to mysql.

gunzip <>

You can also combine two or more backup files to restore at the same time, using the cat command. Here's how you can do that.

cat backup1.sql backup.sql | mysql -u sadmin -p pass21

Moving Data Directly Between Databases

How would you like to replicate your present database to a new location? When you are shifting web hosts or database servers, you can directly copy data to the new database without having to create a database backup on your machine and restoring the same on the new server. mysql allows you to connect to a remote database server to run sql commands. Using this feature, we can pipe the output from mysqldump and ask mysql to connect to the remote database server to populate the new database. Let's say we want to recreate the Customers database on a new database server located at 202.32.12.32, we can run the following set of commands to replicate the present database at the new server.

mysqldump -u sadmin -p pass21 Customers | mysql --host=202.32.12.32 -C Customers

Thursday, May 3, 2007

Generate PEM certs

In this case its for everyones best friend........ Monit

  1. Generation of a "pemfile"

    First generate an openssl configuration (or if you have one use it). It might look like this... it is just an example! (-:

    ----- BEGIN:monit.cnf -----
    # create RSA certs - Server

    RANDFILE = ./openssl.rnd

    [ req ]
    default_bits = 1024
    encrypt_key = yes
    distinguished_name = req_dn
    x509_extensions = cert_type

    [ req_dn ]
    countryName = Country Name (2 letter code)
    countryName_default = MO

    stateOrProvinceName = State or Province Name (full name)
    stateOrProvinceName_default = Monitoria

    localityName = Locality Name (eg, city)
    localityName_default = Monittown

    organizationName = Organization Name (eg, company)
    organizationName_default = Monit Inc.

    organizationalUnitName = Organizational Unit Name (eg, section)
    organizationalUnitName_default = Dept. of Monitoring Technologies

    commonName = Common Name (FQDN of your server)
    commonName_default = server.monit.mo

    emailAddress = Email Address
    emailAddress_default = root@monit.mo

    [ cert_type ]
    nsCertType = server
    ----- END:monit.cnf -----

    In order to generate the actual pemfile just run these commands:

    # Generates the private key and the certificate
    /usr/local/bin/openssl req -new -x509 -days 365 -nodes \
    -config ./monit.cnf -out /var/certs/monit.pem \
    -keyout /var/certs/monit.pem

    # Generates the Diffie-Hellman Parameters
    /usr/local/bin/openssl gendh 512 >> /var/certs/monit.pem

    # Prints out the certificate information
    /usr/local/bin/openssl x509 -subject -dates -fingerprint -noout \
    -in /var/certs/monit.pem

Vi Search and replace

Advanced Search and Replace

  1. Use :s/foo/bar/ to replace the first occurrence of the word foo on the current line with the word bar.
  2. Use :s/foo/bar/g to replace all occurrences of the word foo on the current line with the word bar.
  3. Use :%s/foo/bar/g to replace all occurrences of the word foo in the current file with the word bar. Leaving off the g at the end only replaces the first occurrence of foo on each line of the current file.
  4. Use :%s/foo//g to delete all occurrences of the word foo in the current file. Leaving off the percent sign (%), of course, only does this for the current line.
  5. Use :%s/foo/bar/gc to have Vi query you before each attempt to replace the word foo with the word bar.

Wednesday, April 18, 2007

Find All Items In a subtree

find directories:
find ./ -type d | wc -l

find files:
find ./ -type f | wc -l

Remove All Empty Directories

prompt:>> perl -MFile::Find -e"finddepth(sub{rmdir},'.')"

Its safe, rmdir won't delete a directory with anything in it