Browsing the archives for the Examples category.

Python Parse XML Document

Examples

python_logo2

Parsing an XML document in Python is easy. Below is a simple example that loads an XML document from a file, parses and prints:

from xml.dom import minidom
dom = minidom.parse('test.xml')
print dom.toxml()

For more information, see the Python minidom documentation.

1 Comment

Format The Date Command In Bash

Examples

Formatting the date output in bash is simple:

date + "%Y-%m-%d";

An example of assigning to a variable and using:

now=`date + "%Y-%m-%d"`;
cp /etc/hosts ~/hosts_${now};

For more information, see the manual.

No Comments

Create Amazon EBS

Examples, Tools

aws

The Amazon Web Services blog has a good post on how to setup an EBS volume using ElasticFox:

Amazon EBS (Elastic Block Store) – Bring Us Your Data

I do not see a way to schedule snapshots through the UI so, you may have to write a script to accomplish this feat.

Once you have created your EBS volume you still have to mount the new device. First add the following line to /etc/fstab:

/dev/YOUR_DEVICE_NAME  /data  ext3  defaults 0 0

E.g.

/dev/sde  /data  ext3  defaults 0 0

Next, you need to format the device. This is accomplished by executing:

mkfs.ext3 /dev/YOUR_DEVICE_NAME

E.g.

mkfs.ext3 /dev/sde

This post assumes you are using the ext3 filesystem. Next, you need to mount the device. This is done by issuing:

mount -t ext3 /dev/YOUR_DEVICE_NAME /DIRECTORY_TO_MOUNT_IN

E.g.

mount -t ext3 /dev/sde /data

After you mount the device you should be good to go. If possible, reboot your instance to make sure the process mounted properly on a restart.

1 Comment

MySQL Update Table Limit Rows

Examples

Updating a block of rows in MySQL can be accompished by using the “limit” function:

update TABLE_NAME set COLUMN_NAME=VALUE limit MAX;

E.g.

update products set state=false limit 10;
No Comments

Rebuild An InnoDB Table Index

Examples

Rebuilding an InnoDB table index is easy in MySQL 5.0. Simply type:

mysql> ALTER TABLE table_name ENGINE=InnoDB;

Warning: When dealing with large databases, this can place a lot of load on your DB.

No Comments

Subversion – Checkout Older Revision

Examples

The SVN syntax for reverting to a previous version of a file is less than obvious. If you want to pull down a previous version of a file type you must first choose a version:

svn log FILE_NAME

This will list all the changes to the file. Next, you pick the revision you are interested in and then type:

svn up -r REVISION_NUMBER FILE_NAME

This will pull down the older version/revision of the file. Next, we are going to rename the file:

mv FILE_NAME FILE_NAME.good

The next step is to pull down the current version of the file:

svn up FILE_NAME

After that is done we are move the “good” file on top of the latest version:

mv FILE_NAME.good FILE_NAME

Now commit the change:

svn ci FILE_NAME

It seems like there should be an easier way without merging so if anyone knows an easier way, please post it!

No Comments

Linux Process Thread Count

Examples

On Linux, to get a thread count for particular process, issue:

ps UH p PID_OF_PROCESS | wc -l

Removing the pipe to the “| wc -l” yields more information about the running threads.

No Comments

MySQL Increase Column Size

Examples

An example of modifying a MySQL table to increase a VARCHAR column size:

alter table TABLE_NAME modify COLUMN_NAME VARCHAR(1024) NOT NULL;

This increases COLUMN_NAME to 1024 characters.

No Comments

MySQL Drop Unique Constraint

Examples

In MySQL 5.0 it is possible to drop unqiue constraints but, the syntax is not incredibly intuitive.

alter table TABLE_NAME drop index UNIQUE_CONSTRAINT_NAME;

Code Ghar provides a more thorough description.

1 Comment

Linux – Programmatically Change Password

Examples

If you need to script a password change you have two main options:

  1. Write a script that interacts with the command line and executes passwd
  2. Use the usermod command

Option one is a bit of a mess and option two requires an encrypted version of the password. At first, we couldn’t figure out how to encrypt the password but then we realized that you could use the Perl crypt function.

An example of number two:

#!/bin/bash

usermod -p `perl -e 'print crypt("test", "salt")'` root;

That’s all there is to it!

2 Comments
« Older Posts
Newer Posts »