Saturday, August 30, 2008

selective table restore from mysqldump

I recently had to restore a corrupted table that had resulted from a full disk. I had been using automysqlbackup as my backup script. Each database had a corresponding backup file. It would have been easy to use a text editor to edit out the tables that you didn't want to restore but this database had grown to 1G.

STEP 1: Restore table structure

If the table structure is corrupted, you need to retrieve the CREATE TABLE statements from the backup. To do this you can use grep:

zcat backup.sql.gz | grep -A 20 'CREATE TABLE `tablename1`' > tablestructure.sql

This command will create tablestructure.sql containing the CREATE TABLE statement and 20 lines after that. You may have to change the parameter from 20 to whatever to get the entire statement.

Once tablestructure.sql had been edited to only contain the proper CREATE TABLE statement, you may now restore the table structure.

mysql -u user -p dbname < tablestructure.sql

STEP 2: Extract and restore the table data

This command will create a sql file for the table.

zcat backup.sql.gz | grep 'INSERT INTO `tablename1`' > tabledata.sql

If the resulting sql file is small enough, you may load the table straight back into the database.

mysql -u user -p dbname < tabledata.sql

OPTIONAL: If you have a huge table and want to partially restore or batch things, you may want to split the table data into multiple lines.

split -l 100 tabledata.sql

This will create multiple files of 100 lines each named xaa, xab, xac, etc. You may then load each file separately.

If some rows are already on the table and you just don't want the INSERT to fail on a duplicate primary key, you can use sed to add the IGNORE option to the INSERT statement. The command below simply does a search and replace on the source file and writes it to a new file.

sed 's/INSERT/INSERT IGNORE/' < tabledata.sql > newtabledata.sql

That should do the trick.

Wednesday, August 13, 2008

Setting up Corporate Ubuntu Desktops

This guide has been updated for Ubuntu Hardy 8.04.1. The following is the official installation guide:

[WWW] https://help.ubuntu.com/8.04/installation-guide/i386/index.html

The preseed method will enable a more automated install. You will want to enter the proxy a local apt-cacher server if you have one to speed up the updates and installs of new packages.

Post Installation Tasks

Updating and Installing Packages

  • In Synaptic, configure proxy as needed. If you have an apt-cacher service running on the proxy server, it will make your updates much faster if the packages have already been downloaded by a prior machine.

  • Click on Reload

  • Click on Upgrade

  • Install ubuntu-restricted-extras

  • Install ttf-liberation

  • Apply

Configure Desktop to Remove Annoying Stuff

  • In System -> Administration -> Login Window -> Accessibility, uncheck the sounds

  • In System -> Preferences -> Sounds -> Sounds, uncheck Play System Sound to remove annoying sounds

If you want to change the desktop wallpaper for everyone:

  1. In the desktop, type Alt-F2

  2. gksudo gconf-editor

  3. Enter your password if prompted

  4. Choose /desktop/gnome/background from the tree on the left

  5. Change the picture_filename key to (e.g.) usr/share/backgrounds/ubuntu-calendar.png

  6. Right click and Set As Mandatory or Set As Default. Mandatory means users cannot change the wallpaper.

You will also want to change the default fonts to the liberation fonts. It makes the menus and fonts in your applications more compact.

The scripts below will set the default fonts and settings without going through the GUI. Just change the mandatory to defaults if you want to allow users to change it.

sudo gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory --set /apps/nautilus/preferences/desktop_font --type string "Liberation Sans 9"
sudo gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory--set /desktop/gnome/interface/document_font_name --type string "Liberation Sans 9"
sudo gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory--set /desktop/gnome/interface/font_name --type string "Liberation Sans 9"
sudo gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory--set /desktop/gnome/interface/monospace_font_name --type string "Monospace 9"
sudo gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults --set /desktop/gnome/sound/event_sounds --type bool "false"

Adding a New User

  1. System -> Administration -> Users and Groups, Unlock (You will asked for your password.)

  2. Add User as Desktop User Profile. This user will not be able to install new packages.

Adding a Printer

  1. System -> Administration -> Printing

  2. New Printer -> HP JetDirect

  3. Enter the IP of the network printer

  4. Change Printer Options to a default media size of Letter instead of A4 if you are in the Philippines.