Saturday, September 17, 2011

Reusing/relabeling media with bacula

A few weeks ago I reinstalled my backup server with bacula. I have a small machine (2x700MHz, 1GB, Solaris 10, Bacula 5.0.3) with 3 DDS-3 tape drives (and other stuff to play around). Now I want to reuse my old tapes which were already labeled. So I have to do some kind of relabeling. When I try to normally label the prior used tapes I get the following error:

*label
The defined Storage resources are:
     1: File
     2: DDS-3-01
     3: DDS-3-02
     4: DDS-3-03
     5: Jaz
Select Storage resource (1-5): 3
Enter new Volume name: t08
Defined Pools:
     1: File
     2: DDS-3-01
     3: Jaz
     4: DDS-3-02
     5: DDS-3-03
Select the Pool (1-5): 4
Connecting to Storage daemon DDS-3-02 at bck01:9103 ...
Sending label command for Volume "t08" Slot 0 ...
3920 Cannot label Volume because it is already labeled: "t08"
Label command failed for Volume t08.
Do not forget to mount the drive!!!

Obviously the tape can not be labeled because it is already labeled. So I tried the relabel command:

*relabel
The defined Storage resources are:
     1: File
     2: DDS-3-01
     3: DDS-3-02
     4: DDS-3-03
     5: Jaz
Select Storage resource (1-5): 3
Defined Pools:
     1: File
     2: DDS-3-01
     3: Jaz
     4: DDS-3-02
     5: DDS-3-03
Select the Pool (1-5): 4
No results to list.
Enter *MediaId or Volume name: t08
sql_get.c:1062 Media record for Volume "t08" not found.

Relabeling the tape does not work because the media was not found in the database (I have also reinstalled the database). Labeling or relabeling within bacula does not work, so I have to use mt now. But first unmount the media:

*unmount
Automatically selected Catalog: MyCatalog
Using Catalog "MyCatalog"
The defined Storage resources are:
     1: File
     2: DDS-3-01
     3: DDS-3-02
     4: DDS-3-03
     5: Jaz
Select Storage resource (1-5): 3
3002 Device "DDS-3-02" (/dev/rmt/1l) unmounted.

Then connect as root user on the media server and use the mt command to delete the label on the tape:

# mt -f /dev/rmt/1l rewind
# mt -f /dev/rmt/1l weof 
# mt -f /dev/rmt/1l rewind

The -f option will specify the device file for the tape drive. The weof without any count will delete the mark for the numbers of records on the tape. If you have three tar files stored on a tape then then the EOF mark is set to 2. You can forward the tape to the last record with the following command:

# mt -f /dev/rmt/1l fsf 2

The weof command should work for reusing the the tapes. Go back to your bacula console and try to label the tape again:

*label
Automatically selected Catalog: MyCatalog
Using Catalog "MyCatalog"
The defined Storage resources are:
     1: File
     2: DDS-3-01
     3: DDS-3-02
     4: DDS-3-03
     5: Jaz
Select Storage resource (1-5): 3
Enter new Volume name: t08
Defined Pools:
     1: File
     2: DDS-3-01
     3: Jaz
     4: DDS-3-02
     5: DDS-3-03
Select the Pool (1-5): 4
Connecting to Storage daemon DDS-3-02 at bck01:9103 ...
Sending label command for Volume "t08" Slot 0 ...
3000 OK label. VolBytes=64512 DVD=0 Volume="t08" Device="DDS-3-02" (/dev/rmt/1l)
Catalog record for Volume "t08", Slot 0  successfully created.
Requesting to mount DDS-3-02 ...
3001 Mounted Volume: t08
3001 Device "DDS-3-02" (/dev/rmt/1l) is mounted with Volume "t08"

Finally I can reuse all my old tapes again. Print Friendly and PDF

Thursday, September 8, 2011

Creating mp4 for Android with ffmpeg

I am traveling to work by public transportation and I thought it would be a cool idea to have some movies with me during the train ride. Of course I have to encode them first. Here is a way to do this with ffmpeg:

$ ffmpeg -i input.avi \
    -s 480x320 -aspect 3:2 -vcodec mpeg4 -vb 400000 -r 13 \
    -acodec aac -strict experimental -ac 1 -ar 64000 -ab 64000 \
    output.mp4

That's all. If you want to know what the options are, continue reading:

-i input.avi: the input file
-s 480x320: this will resize the video to 480 px x 320 px in size
-aspect 3:2: keep the aspect ratio, so the images of the movie won't be stretched
-vcodec mpeg4: the video codec, mp4
-vb 400000: video bitrate (in bits/s), in this case 400kb
-r 13: the framerate, 13 frames/s is enough
-acodec aac: the audio codec, aac
-strict experimental: aac is experimental, without strict ffmpeg will not encode
-ac 1: audio channel, 1 for mono, 2 for stereo
-ar 64000: audio sample rate
-ab 64000: audio bit rate
output.mp4: output file

When you want to copy more movies to your Android device then you can play with the bitrates to decrease the video size:

$ ffmpeg -i input.avi \
    -s 480x320 -aspect 3:2 -vcodec mpeg4 -vb 200000 -r 13 \
    -acodec aac -strict experimental -ac 1 -ar 16000 -ab 16000 \
    output.mp4

In the example above I reduce the vb, ar, and ab switches to create a low quality output file which will consume less space. Enclosed a small script so you don't have to remember the complete commandline:

# vi /usr/local/bin/mov2mp4.sh
#!/bin/bash

function Usage {
  echo "Usage: mov2mp4.sh -i <input> [-q <quality>] [-o <output>]"
  echo "        Quality: l=low, n=normal (default), h=high"
  exit
}

# PARSE ARGUMENTS
while getopts ':i:o:q:h' option; do
  case $option in
  i)
    input="$OPTARG"
    ;;
  o)
    output=$OPTARG
    ;;
  q)
    quality=$OPTARG
    ;;
  h)
    Usage
    exit
    ;;
  esac
done

if [[ $input == "" ]]; then
  Usage
fi

if [[ $output == "" ]]; then
  output=`echo "$input" | awk 'BEGIN {FS="/"} {print $NF}' | awk 'BEGIN {FS="."} {print $1"-android.mp4"}'`
fi

if [[ $quality == "l" ]]; then
  vb="200000"
  ar="32000"
  ab="32000"
fi

if [[ $quality == "n" ]] || [[ $quality == "" ]]; then
  vb="300000"
  ar="48000"
  ab="64000"
fi

if [[ $quality == "h" ]]; then
  vb="400000"
  ar="64000"
  ab="128000"
fi

ffmpeg -i "$input" -s 480x320 -aspect 3:2 -vcodec mpeg4 -vb $vb -r 13 -acodec aac -strict experimental -ac 1 -ar $ar -ab $ab -y "$output" > /tmp/mov2mp4.log 2>&1

echo "Finished, see /tmp/mov2mp4.log for details"

Make it executable:

# chmod 755 /usr/local/bin/mov2mp4.sh

And run it:

# mov2mp4 -i movie.avi -q h -o movie.mp4

The options a very easy:
-i: Input file
-q: Quality - l=low, n=normal (default), h=high
-o: Output file

The higher the quality the bigger the file will get. Print Friendly and PDF