Aug 13 2009

How to Drop all Tables on a MySQL database

Tag:tepezcuintle @ 19:17

Shark

Let’s say you need to drop all tables in a mysql database. How do you do that?

You could use a gui, but that’s not fun.

You’re a shell jockey so you want a commandline:

mysql -u uname dbname -e "show tables" | grep -v Tables_in | grep -v "+" | \
gawk '{print "drop table " $1 ";"}' | mysql -u uname dbname

(that’s all one line, but if I do it as a line then it screws up my theme - go figure).

This assumes that you are running in passwordless mode. See “man mysql” for tips on how to pass in passwords in another manner.

What this does is

1.connect to a specific mysql database and execute the command for showing tables
2.find lines that match “Tables_in” and not show them
3.find lines that match the + character and not show them
4.use gawk to print out the words “drop table” followed by the table name (which is in $1) and then a semicolon
5.pipe all of that back to the database you got the list from to drop those tables
Fun stuff and very handy!


May 01 2009

A Simple Monitoring Script to monitor devices or hosts

Tag:tepezcuintle @ 21:38

Monitor
At work we have a few routers, switches and servers that if they go down we want to know right away. The last thing we want is the CEO coming to our desk asking why the mail server is down or why the webserver it not responding.

I wrote a little script a very simple one that given a list of hosts and IP addresses will ping a host and if it is detected as down it will send you an email right away.

########## Instructions on how to use this monitor script ############

Make a directory called scripts
mkdir /scripts then inside create using your favorite text editor the following script


#!/bin/bash
# Ping Alert Monitor by Marco Maldonado V 1.0
# Create a hosts file and save it somewhere on your server
# specify a path and an email address where you want to send alerts
#
# Note: Your hosts file should have the IP address of the host followed by a space and the name
# of the server: Example Below
#
# 69.147.76.15 www.yahoo.com
# 209.85.165.147 wwww.google.com
#

# Path to your hosts file

HOSTS="/scripts/hosts"

# Email address where to send alerts, you can add more addresses followed by a comma

EMAIL="yourname@yourdomain,someaddres@gmail.com"

### Do not edit anything below ############

for myHost in `cat $HOSTS | cut -d' ' -f1` ; do
ping -c 1 $myHost > /dev/null
if [ $? -ne 0 ] ; then
echo “Server `grep $myHost $HOSTS` is down reported down at `date`” | mail -s “Ping Alert Server `grep $myHost $HOSTS` might be down” $EMAIL
fi ;
done

Continua”A Simple Monitoring Script to monitor devices or hosts”


Dec 22 2008

Sed Introduction and a nice tutorial

Tag:tepezcuintle @ 2:44

Found at http://www.grymoire.com/Unix/Sed.html

Thanks for making this the number #1 page in Google on ’sed’!

Sed - An Introduction and Tutorial

Bruce Barnett


Last update: Sun Jul 20 21:59:55 EDT 2008

Thanks to Keelan Evans, Fredrik Nilsson, and Kurt McKee for spotting some typos.

Thanks to Wim Stolker and Jose’ Sebrosa as well.

Thanks to Olivier Mengue.

Thanks to Andrew M. Goth.

Thanks to David P. Brown.

Thanks to Axel Schulze for some corrections

A big thanks for Fazl Rahman for spotting dozens of errors.

Table of Contents

Continua”Sed Introduction and a nice tutorial”


Dec 21 2008

Find CPU hog processes on a Red Hat Machine

Tag:tepezcuintle @ 23:55

I found this command useful in finding out which top 10 processes are hogging my CPU resources. Note that this command is specific to the Red Hat flavor of Linux. See the man page for ps for the correct output format to use for your specific platform:
Continua”Find CPU hog processes on a Red Hat Machine”


Dec 21 2008

Perl Module Remover script

Tag:tepezcuintle @ 23:47

Have you ever installed a perl module that later didn’t work with your system or broke things, or maybe you want to remove a new module and install an older one.
Continua”Perl Module Remover script”


Dec 21 2008

Centos 5.0 Install all available Perl Modules

Tag:tepezcuintle @ 23:28

This Sunday I was building another server where I can run some Chatbots but the cpan setup kept failing. A lot of modules were broken and they didn’t work. On a blog I read that you need to make sure you have the majority of perl modules available on the Centos 5.0 repo. I didn’t know the list of all the available perl Modules but here is how you can install all of them.

Continua”Centos 5.0 Install all available Perl Modules”


Oct 14 2008

Vi searching and replacing text

Tag:tepezcuintle @ 15:43

A>Searching and replacing text
vi has a number of search command. You can search for individual
charaters through to regular expressions.

The main two character based search commands are [ f ] and
[ t ] .

         fc         Find the next character c. Moves RIGHT to the next.
         Fc         Find the next character c. Moves LEFT to the preceding.
         tc         Move RIGHT to character before the next c.
         Tc         Move LEFT to the character following the preceding c.
                    (Some clones this is the same as Fc)
         ;          Repeats the last f,F,t,T command
         ,          Same as ; but reverses the direction to the orginal command.

Continua”Vi searching and replacing text”


Sep 18 2008

Lynx user Guide

Tag:tepezcuintle @ 2:39

Lynx

Lynx Users Guide v2.8.5

Lynx is a fully-featured World Wide Web (WWW) client
for users running cursor-addressable, character-cell display devices (e.g.,
vt100 terminals, vt100 emulators running on PCs or Macs, or any other
character-cell display). It will display Hypertext Markup Language
(HTML) documents containing links to files on the local system, as
well as files on remote systems running
Continua”Lynx user Guide”


Sep 09 2008

How to backup your MySQL databases with Hotcopy and bash

Tag:tepezcuintle @ 4:45

Dolphin
Backing up your MySQL databases the easy way.

This is an easy script that can back up all your mysql databases, provided you are using MYISAM tables and not INNODB

Cut and paste this shell script and dump it on your /etc/cron.daily folder
then chmod 700 /etc/cron.daily/marco-db.sh


#!/bin/bash
#
# Marco Maldonado MySQL Backup
# Version 1.0 September 9 2008
# comments marco@penguincares.no-ip.org

MYSQL=`which mysql`
HOTCOPY=`which mysqlhotcopy`
BACKUPS=/backups/marco-db
userpassword=" --user=root --password=yourpassword"
dbs=`$MYSQL -u root -pyourpassword -Bse 'show databases'`

for db in $dbs
do
rm -rf $BACKUPS/$db.3
mv $BACKUPS/$db.2 $BACKUPS/$db.3
mv $BACKUPS/$db.1 $BACKUPS/$db.2
mv $BACKUPS/$db.0 $BACKUPS/$db.1
mkdir $BACKUPS/$db.0
$HOTCOPY $userpassword $db $BACKUPS/$db.0
done

Once you have saved this script you need to create a backup directory. I created my directory inside the folder /

mkdir /backups
mkdir /backups/marco-db

Remember you can change the path to the backup directory to any directory that you like, but
please edit the script to reflect the changes.

Ok so this is what will happen, the first time it runs it will create a backup of your databases inside the /backups/marco-db folder the first backup will have all the databases in this format.

drwxr-xr-x 3 root root 4096 Sep 9 00:19 mysql.0
drwxr-xr-x 3 root root 4096 Sep 9 00:19 rachelcloud.0
drwxr-xr-x 3 root root 4096 Sep 9 00:19 security_blog.0
drwxr-xr-x 3 root root 4096 Sep 9 00:19 techmodeals.0
drwxr-xr-x 2 root root 4096 Sep 9 00:19 test.0

then the next day that it runs again it will create another backup and the backup from the day before will be renamed with a 1 at the end.

drwxr-xr-x 3 root root 4096 Sep 10 00:19 security_blog.0
drwxr-xr-x 3 root root 4096 Sep 9 00:19 security_blog.1
drwxr-xr-x 3 root root 4096 Sep 10 00:19 techmodeals.0
drwxr-xr-x 3 root root 4096 Sep 9 00:19 techmodeals.1

the script will run until it creates 4 backups of your databases and then it will start removing the 4th oldest backup. so you will have 4 days worth of backups of your database.

You can change the script so that you only have 2 day backups or 3 days. You can also modify the path to the backups for example /backups/weekly and then drop this script inside the
/etc/cron.weekly and you will have weekly backups of your database.

That way you can have a full weekly backup and then daily backups of your database just in case.

I hope this helps someone who is having problems backing up their databases.

Warning: These scripts are free and there is no guarantee. While I have tried everything to make sure that everything works you have to make sure to test it first before you use it in a production environment.

Later Penguins.


May 30 2008

Cool things you can do with the openssl command for troubleshooting

Tag:tepezcuintle @ 16:04

Focas

OpenSSL is more than just an open source SSL library implementation. It can be used to create, request, sign, and revoke certificates and can also be used to perform other cryptographic operations such as creating hashes for files, testing SSL connections, and more. This week, we’ll take a look at some of the interesting things that can be done with the openssl command-line program.

To test SSL connections to a mail server, use the openssl command with the s_client parameter:
Continua”Cool things you can do with the openssl command for troubleshooting”


Next Page »