Home › Category Archives › Linux

Backing up MySQL database by database and table

Backing up mysql databases using mysqldump is pretty quick and painless:
mysqldump -u username -ppassword -h hostname db_name > backup_file.sql
or
mysqldump -u username -ppassword -h hostname --all-databases > backup_file

The only problem is that it becomes quite cumbersome recover a particular table.

I wrote a little script to backup each table in each database to its own file:

#!/bin/bash

if [ $# -lt 2 ]
then
        echo "Usage: backup_all_tables username password [host]"
        exit
fi

mysql_bin="/usr/bin/mysql"
mysqldump_bin="/usr/bin/mysqldump"
backup_path="/opt/backups/www/"

if [ $# -eq 3 ]
then
        host=$3
else
        host="localhost"
fi

user=$1
pass=$2

for db in `echo "show databases" | $mysql_bin -u $user -p$pass -h $host | grep -v Database | grep -v information_schema`
do
        for table in `echo "show tables" | $mysql_bin -u $user -p$pass -h $host $db | grep -v "Tables_in"`
        do
                echo "Backing up $host.$db.$table to $backup_path${host}_$db.$table.sql"
                $mysqldump_bin -u $user -p$pass -h $host $db $table > ${backup_path}${host}_$db.$table.sql
        done
done

save the script as “backup_all_tables.sh”, edit the $backup_path variable to suit, do a chmod +x backup_all_tables to make it executable, and run it using:
./backup_all tables username password hostname
If the database is on your local machine you can omit the hostname.

This will backup the files in the format “host_db.table.sql”, ie: “localhost_mysql.user.sql”

Share Button

Huge sendmail queue – clearing it out

There are numerous reasons that the mail queue can get over full, ranging from general network problems, to having a user repeatedly sending an email with a large attachment to an incorrectly typed address.

The first thing you likely want to do is get some information on what’s in the queue:
To find out how many messages are in the queue:
mailq | tail -1
This will give you the total number of emails in the queue, ie:
Total requests: 6592
You might also want to know how old or new they are, so:
ls -lt | tail -10
and
ls -ltr | tail -10
will show you the oldest 10 and the newest 10 respectively.
To have sendmail retry the emails, simply run (as root):
sendmail -q -v
If you are in a situation where it has filled up the disk and need to fix it FAST, move the directory to another partition and then have sendmail process it from there (stopping sendmail with “/etc/init.d/sendmail stop” is recommended:
mkdir /root/tmpq
mv /var/spool/mqueue/* /root/tmpq
sendmail -q -v -oQ/root/tmpq

(if you get an error like “Argument list too long” instead run “mv /var/spool/mqueue /root/tmp” with sendmail stopped, then restart it)

Share Button

Recursively Change Text in Files

Handy little Perl one liner example to find and replace text across multiple files/directories… as an example, to change every .php file recursively from the current directory, changing “find this” to “replace with this”:

find . -name '*.php' -print0 | xargs -0 perl -pi -e 's/find this/replace with this/g'

Share Button

Support for JACK Audio in Adobe Flash

I use JACK quite frequently for playing guitar and recording etc, and while it is quite flexible, one thing I was unable to do was this: Get Flash audio to route through JACK. Most commonly this is when I want to play a Youtube video, and I find I have to stop jackd, which requires shutting down most JACK-aware applications, play the video, restart JACK, blah blah blah. Not really functional.

 

A quick search and I found libflashsupport-jack – and was excited! Look, something to make Flash work with JACK! So I checked out the git repo at git://repo.or.cz/libflashsupport-jack.git but it wouldn’t compile. Looked into it, and the major problem was that it includes a deprecated file in the kernel headers: linux/videodev.h, but alas, as of kernel version 2.6.38 the header file linux/videodev.h is no longer included in the kernel.

 

Long story short: I got the file, put it in with the libflashsupport source, hacked around with the code a bit, and TADA! I can now play Flash when JACK is running!

 

Even more importantly, for you that is, I’ve made a fork of the old repo, and have made the new working version available!

 

I’ll set up a proper homepage for this little project, but for now I’ll just link to the github repo and some instructions Note: if you go to http://github.com/pvint/libflashsupport-jack2 there is also a link to download a zipfile):

git clone git://github.com/pvint/libflashsupport-jack2.git
cd libflashsupport-jack2
sh bootstrap.sh
make
make install

(You must be root to do the make install, of course)
On a 64bit platform, you’ll likely need to create a symlink as well to get Flash to use this:

ln -s /usr/local/lib/flashsupport.so /usr/lib64

 

Now, fire up your JACK and your browser (You may need to restart the browser if it’s running), and try it out, it should just work!

 

If you have any issues, feel free to drop me a not here or by email.

Share Button

Connecting multiple sound devices with JACK and ALSA

The problem: JACK connects to one (generally the default) sound device, but you may have a second (third, fourth, etc) device that you want to record from or playback on.

Example situation (Actually, my situation):
Device #1: The NVidia sound card built in to the PC/Laptop.
Device #2: Guitar amplifier connected via USB
Device #3: USB headset/microphone.

Software I use:
(Note that this is all free software for Linux, versions may exist for other operating systems)

  • QJackCtl – essentially to start/stop JACK
  • Patchage – Used for making the connections between devices
  • alsa_in/alsa_out – Used to add the extra sound devices for JACK. If you installed JACK, you should have these command line programs
  • Ardour – for recording the tracks
  • aplay and arecord – part of the alsa-utils package

First Steps

Before starting anything, connect the devices, and use aplay and arecord to list what our devices are:
To list the playback devices:

$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: NVidia [HDA NVidia], device 0: CONEXANT Analog [CONEXANT Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: NVidia [HDA NVidia], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: LX3000 [Microsoft LifeChat LX-3000], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

And the capture devices:

$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: NVidia [HDA NVidia], device 0: CONEXANT Analog [CONEXANT Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: LX3000 [Microsoft LifeChat LX-3000], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: Amplifier [Mustang Amplifier], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Now, to translate this into standard ALSA lingo, the NVidia built-in device is card0, so we will refer to it as HW0, The M$ LX-3000 headset is card 1, thus referred to as HW1, and the Fender Mustang Amp is HW2.

Okay, so now running QJackCtl and clicking “Start” should get JACK running on HW0, the built-in sound device (Assuming it’s still at default settings). If you now start Patchage, you should see something like the following:
patchage_ss1
Now for the fun part; let’s get the input from the amplifier connected. Open up a terminal and run “alsa_in -j Amplifier -d HW:2″ and you should see something like:

$ alsa_in -j Amplifier -d hw:2
selected sample format: 16bit
delay = 4094
delay = 1019

Don’t worry if you get a couple of those “delay” messages when it first starts up.
Now, let’s get the mic on the headset connected:

$ alsa_in -j Mic -d hw:1
WARNING: chennel count does not match (requested 2 got 1)
selected sample format: 16bit
delay = 3255

Note the message about channel count – that’s just because it defaults to 2, if you don’t like seeing the message you can add a “-c 1″ to the command.

Now the last thing, the headset speakers:

$ alsa_out -j Headphones -d hw:1 -r 44100
selected sample format: 16bit

Note that I added a “-r 44100″ – in my situation everything else defaults to 44100, but the headset defaults to 48000 and sounds like crap.

Now, let’s check on what it looks like in Patchage now:

patchage_ss2

Now we’re getting somewhere!

So now, I’ll fire up Ardour and add a couple tracks – 1 stereo track for the guitar and one mono track for the vocals. (I normally also use Hydrogen for a drum track as well, but I’ll skip that in the interest of keeping this shorter) Once that’s done, we’ll make the connections in Patchage by right-clicking on items that are connected automatically that we don’t want and selecting “Disconnect All”, then dragging the mouse from one device to another to “wire them up”.

Once it’s done, you should see something like the below:
patchage_ss3

I know the connections in Patchage might look a bit confusing, but play around with it a bit and you’ll find it’s pretty intuitive.

And now, you’re ready to Rock!

Share Button

Automatically Enable USB Headset

I, along with many others, would like a USB headset to be the default sound device in ALSA whenever it’s connected, and it seems to be a tough thing to find out how to do.

Short answer: Create UDEV rules to invoke a script that will modify your ~/.asoundrc file.

Long answer:

Assumptions:

  1. You’re using Linux kernel >=2.6
  2. You’re using ALSA.
  3. You’re using UDEV
  4. You already have the headset working

First, we need to know what device we are looking for, so with the headset unplugged, fire up a terminal and type:
ls /dev/snd/
You’ll see something similar to the below:

localhost ~ # ls /dev/snd
by-path  controlC0  hwC0D0  hwC0D3  pcmC0D0c  pcmC0D0p  pcmC0D3p  seq  timer

Now, plug in the headset and check it again, ie:

localhost ~ # ls /dev/snd
by-id    controlC0  hwC0D0  pcmC0D0c  pcmC0D3p  pcmC1D0p  timer
by-path  controlC1  hwC0D3  pcmC0D0p  pcmC1D0c  seq

Notice the new items, in my case, the ones we are interested in are pcmC1D0p and pcmC1D0c. Note: The suffixes "p" and "c" indicate "Playback" and "Capture". For the purposes of this article, I’ll ignore the capture device, but the same process applies for it as for the playback.

Knowing the device name, we can now create the UDEV rules. Rules files are kept in /etc/udev/rules.d on my Gentoo system – other distros should be similar. You may have no files in there, or there may be many. I made a new file called /etc/udev/rules.d/99-audio.rules. the 99- prefix indicates the order in which the rules files will be applied, and I simply chose 99 to make this one run last.

My /etc/udev/rules.d/99-audio.rules file:

KERNEL=="pcmC1D0p", ACTION=="add", DRIVERS=="usb", PROGRAM="/home/pvint/headsetToggle.sh '%E{ACTION}'"
KERNEL=="pcmC1D0p", ACTION=="remove", DRIVERS=="usb", PROGRAM="/home/pvint/headsetToggle.sh '%E{ACTION}'"

Rough explanation: When UDEV sees a kernel message about the device "pcmC1D0p" via USB, and the action that occurred was that it was "add", it runs the script specified with the ACTION as a parameter (in this case, "add". Note that you’ll need to be root to do this.

Now for the script that actually changes the device, I should put it somewhere better, but I created /home/pvint/headsetToggle.sh with the following contents:

#!/bin/sh
# Set default ALSA device appropriately when USB  headset is plugged/unplugged
if [ "$1" = "add" ]
then
        echo 'defaults.pcm.card 1' > /home/pvint/.asoundrc
else
        echo 'defaults.pcm.card 0' > /home/pvint/.asoundrc
fi

Note that if you had anything in the .asoundrc file, it’ll be clobbered. This could easily be avoided by creating two dummy files like .asoundrc_headset and .asoundrc_normal and having the script copy the appropriate one to .asoundrc. The above was simple and suited my purpose just fine.

That should do it, so restart UDEV (ie: /etc/init.d udevd restart) and try it out.

Notes:

  • Check your /var/log/messages to ensure there were no errors with the UDEV rules
  • Some applications need to be restarted before this takes effect.
  • The script will be run as root, so be careful
Share Button