<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Le Vosgien du Net &#187; bash</title>
	<atom:link href="http://levosgien.net/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://levosgien.net</link>
	<description>Parce que la vérité n'est pas toujours bonne à dire</description>
	<lastBuildDate>Thu, 11 Feb 2010 18:39:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Onliner Batch Rename Files with Bash</title>
		<link>http://levosgien.net/2008/12/13/onliner-batch-rename-files-with-bash/</link>
		<comments>http://levosgien.net/2008/12/13/onliner-batch-rename-files-with-bash/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 12:39:35 +0000</pubDate>
		<dc:creator>Pierre</dc:creator>
				<category><![CDATA[Geekitude]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[gentoo]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[sysa]]></category>

		<guid isPermaLink="false">http://levosgien.net/?p=142</guid>
		<description><![CDATA[	Because I&#8217;m fed of searching for this kind of useful command :
$ for F in *; do E=${F:(-4)}; B=${F/$E/}; mv $F 'Wonderful.Serial.FileName.S03E'$B.'VOSTFR.HDTV.XViD.GKS'$E; done

	Explanations :
E = ${F:(-4)} extracts the file extension.
B = ${F/$E/} returns the original filename without extension.
	These two variables are then used to build the new filename  
	[EDIT]
	Another one, using sed and regular [...]]]></description>
			<content:encoded><![CDATA[	<p>Because I&#8217;m fed of searching for this kind of useful command :</p>
<code>$ for F in *; do E=${F:(-4)}; B=${F/$E/}; mv $F 'Wonderful.Serial.FileName.S03E'$B.'VOSTFR.HDTV.XViD.GKS'$E; done
</code>
	<p>Explanations :<br />
<code>E = ${F:(-4)}</code> extracts the file extension.</p>
<code>B = ${F/$E/}</code> returns the original filename without extension.
	<p>These two variables are then used to build the new filename <img src='http://levosgien.net/wp/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
	<p><strong>[EDIT]</strong></p>
	<p>Another one, using sed and regular expressions :</p>
<code>$ for A in *.avi; do mv "$A" `echo $A | sed -r 's/^Name To Remove &lt;ins&gt;([0-9]&lt;/ins&gt;(-[0-9]+)?)\.avi$/\1.avi/'`; done</code>
	<p>Were we build a simple &#8220;mv&#8221; command with the original filename ($A) and a new filename by catching the output of <code>echo $A | sed</code> enclosing it with backquotes (`).</p>
	<p>sed -r allows to use extended regular expressions.</p>
	<p>s/you/me/ is the sed command to Subsitute you by me.</p>
	<p>s/^Name To Remove <ins>([0-9]</ins>(-[0-9]+)?)\.avi$/\1.avi/</p>
	<p>We match filenames like :
	<ul>
		<li>Name To Remove 123.avi (as many spaces as you want between the textual part of the name and the file number)</li>
		<li>Name To Remove 123-456.avi</li>
	</ul></p>
	<p>We catch the subexpression between parenthesis (123 or 123-456 in this poor example) and we use it to build the new filename (thanks to &#8221;\1&#8221; in the third part of the sed command.)</p>

 ]]></content:encoded>
			<wfw:commentRss>http://levosgien.net/2008/12/13/onliner-batch-rename-files-with-bash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Firebird backup script</title>
		<link>http://levosgien.net/2008/09/25/firebird-backup-script/</link>
		<comments>http://levosgien.net/2008/09/25/firebird-backup-script/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 09:29:59 +0000</pubDate>
		<dc:creator>Pierre</dc:creator>
				<category><![CDATA[Geekitude]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[firebird]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://levosgien.net/?p=123</guid>
		<description><![CDATA[	This is my first released backup script for Firebird RDBMS. It automagically search for all Firebird databases from a central directory, uses gfix to validate them, then backup + gzip them to a central backup directory keeping only 5 last successful backups.
	It may be used as a cron script to automate Firebird databases backup.


#!/usr/bin/env bash
#
# [...]]]></description>
			<content:encoded><![CDATA[	<p>This is my first released backup script for Firebird RDBMS. It automagically search for all Firebird databases from a central directory, uses gfix to validate them, then backup + gzip them to a central backup directory keeping only 5 last successful backups.</p>
	<p>It may be used as a cron script to automate Firebird databases backup.<br />
<span id="more-123"></span></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/env bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#   Validate and Backup Firebird databases using Firebird</span>
<span style="color: #666666; font-style: italic;"># standard tools</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#   You can adjust FB_ROOT, FB_BIN, FB_DATA, Fb_BACKUP and </span>
<span style="color: #666666; font-style: italic;"># BACKUP_COUNT variables to fit your own requirements. </span>
<span style="color: #666666; font-style: italic;"># For instance, if you use an official package of Firebird for your</span>
<span style="color: #666666; font-style: italic;"># ditribution, you may have to change FB_ROOT to /usr/bin.</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># The contents of this file are subject to the Initial </span>
<span style="color: #666666; font-style: italic;"># Developer's Public License Version 1.0 (the &quot;License&quot;); </span>
<span style="color: #666666; font-style: italic;"># you may not use this file except in compliance with the </span>
<span style="color: #666666; font-style: italic;"># License. You may obtain a copy of the License from</span>
<span style="color: #666666; font-style: italic;"># the Firebird Project website, at </span>
<span style="color: #666666; font-style: italic;"># http://www.firebirdsql.org/index.php?op=doc&amp;id=idpl.</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Software distributed under the License is distributed </span>
<span style="color: #666666; font-style: italic;"># on an &quot;AS IS&quot; basis, WITHOUT WARRANTY OF ANY KIND, </span>
<span style="color: #666666; font-style: italic;"># either express or implied. See the License for the </span>
<span style="color: #666666; font-style: italic;"># specific language governing rights and limitations under </span>
<span style="color: #666666; font-style: italic;"># the License.</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># The Original Code is licenced under IDPL</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># The Initial Developer of the Original Code is </span>
<span style="color: #666666; font-style: italic;">#   Pierre Yager (pierre@levosgien.net).</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Portions created by ______________________</span>
<span style="color: #666666; font-style: italic;"># are Copyright (C) ______ _________________.</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># All Rights Reserved.</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Contributor(s): _________________________.</span>
<span style="color: #666666; font-style: italic;">#</span>
&nbsp;
<span style="color: #007800;">FB_ROOT</span>=<span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>firebird
<span style="color: #007800;">FB_BIN</span>=<span style="color: #800000;">${FB_ROOT}</span><span style="color: #000000; font-weight: bold;">/</span>bin
<span style="color: #007800;">FB_DATA</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>firebird
<span style="color: #007800;">FB_BACKUP</span>=backup
<span style="color: #007800;">BACKUP_COUNT</span>=<span style="color: #000000;">5</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># You shouldn't have to make any changes below</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">id</span> -u<span style="color: #000000; font-weight: bold;">`</span> = <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> You are not root...
  <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">67</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Loads the ISC_USER and ISC_PASSWORD environment variables</span>
<span style="color: #7a0874; font-weight: bold;">source</span> <span style="color: #800000;">${FB_ROOT}</span><span style="color: #000000; font-weight: bold;">/</span>SYSDBA.password
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> ISC_USER
<span style="color: #7a0874; font-weight: bold;">export</span> ISC_PASSWORD
&nbsp;
<span style="color: #666666; font-style: italic;">#  Returns the filename without extension, whatever the </span>
<span style="color: #666666; font-style: italic;">#+ extension is...</span>
<span style="color: #000000; font-weight: bold;">function</span> namename<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #7a0874; font-weight: bold;">local</span> <span style="color: #007800;">name</span>=<span style="color: #800000;">${1##*/}</span>
  <span style="color: #7a0874; font-weight: bold;">local</span> <span style="color: #007800;">name0</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${name%.*}</span>&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${name0:-$name}</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Cleanup old backup files</span>
<span style="color: #000000; font-weight: bold;">function</span> cleanup<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #7a0874; font-weight: bold;">local</span> <span style="color: #007800;">PREFIX</span>=$<span style="color: #000000;">1</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># list files by time in reverse order </span>
  <span style="color: #7a0874; font-weight: bold;">local</span> <span style="color: #007800;">FILES</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-rt</span> <span style="color: #800000;">${FB_BACKUP}</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${PREFIX}</span><span style="color: #000000; font-weight: bold;">*</span>.fbk.gz<span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># count the previous files list</span>
  <span style="color: #7a0874; font-weight: bold;">local</span> <span style="color: #007800;">FILES_COUNT</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-lrt</span> <span style="color: #800000;">${FB_BACKUP}</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${PREFIX}</span><span style="color: #000000; font-weight: bold;">*</span>.fbk.gz <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">wc</span> -l<span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$FILES_COUNT</span> <span style="color: #660033;">-gt</span> <span style="color: #007800;">$BACKUP_COUNT</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
  <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #666666; font-style: italic;"># Iterate the files list</span>
    <span style="color: #000000; font-weight: bold;">for</span> F <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$FILES</span>
    <span style="color: #000000; font-weight: bold;">do</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$FILES_COUNT</span> <span style="color: #660033;">-gt</span> <span style="color: #007800;">$BACKUP_COUNT</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
      <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #666666; font-style: italic;">#  And remove first files as soon as we have only </span>
        <span style="color: #666666; font-style: italic;">#+ BACKUP_COUNT files remaining</span>
        <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #007800;">$F</span>
        <span style="color: #007800;">FILES_COUNT</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>FILES_COUNT - <span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
      <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #7a0874; font-weight: bold;">break</span>
      <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">done</span>
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Uses gfix to validates the database</span>
<span style="color: #000000; font-weight: bold;">function</span> validate<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #800000;">${FB_BIN}</span><span style="color: #000000; font-weight: bold;">/</span>gfix <span style="color: #660033;">-validate</span> <span style="color: #660033;">-full</span> $<span style="color: #000000;">1</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$?</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Uses gbak to backup the database then compress it...</span>
<span style="color: #000000; font-weight: bold;">function</span> backup<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #7a0874; font-weight: bold;">local</span> <span style="color: #007800;">DIR</span>=<span style="color: #800000;">${1%/*}</span>
  <span style="color: #7a0874; font-weight: bold;">local</span> <span style="color: #007800;">DB</span>=<span style="color: #000000; font-weight: bold;">`</span>namename <span style="color: #007800;">$F</span><span style="color: #000000; font-weight: bold;">`</span>
  <span style="color: #7a0874; font-weight: bold;">local</span> <span style="color: #007800;">TS</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #ff0000;">&quot;%Y%m%d_%H%M%S&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
  <span style="color: #7a0874; font-weight: bold;">local</span> <span style="color: #007800;">BACKUP</span>=<span style="color: #800000;">${FB_BACKUP}</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${DIR}</span>_<span style="color: #800000;">${DB}</span>_<span style="color: #800000;">${TS}</span>.fbk
  <span style="color: #800000;">${FB_BIN}</span><span style="color: #000000; font-weight: bold;">/</span>gbak <span style="color: #660033;">-b</span> $<span style="color: #000000;">1</span> <span style="color: #007800;">$BACKUP</span>
  <span style="color: #c20cb9; font-weight: bold;">gzip</span> <span style="color: #007800;">$BACKUP</span>
  cleanup <span style="color: #800000;">${DIR}</span>_<span style="color: #800000;">${DB}</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">pushd</span> <span style="color: #800000;">${FB_DATA}</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-d</span> <span style="color: #800000;">${FB_BACKUP}</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #800000;">${FB_BACKUP}</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#  Avoid */*.fdb to be expanded if there are no files in the </span>
<span style="color: #666666; font-style: italic;">#+ databases repository</span>
<span style="color: #7a0874; font-weight: bold;">shopt</span> <span style="color: #660033;">-s</span> nullglob
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> F <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">*/*</span>.fdb
<span style="color: #000000; font-weight: bold;">do</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">`</span>validate <span style="color: #007800;">$F</span><span style="color: #000000; font-weight: bold;">`</span> = <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
  <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #666666; font-style: italic;"># Only backup valid databases</span>
    backup <span style="color: #007800;">$F</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">#  Future enhancements : write a message to stderr so that</span>
    <span style="color: #666666; font-style: italic;">#+ cron can send a mail to the DBA when databases are not </span>
    <span style="color: #666666; font-style: italic;">#+ valid...</span>
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">popd</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null</pre></div></div>

	<p>Hope this will be useful. You can also <a href="http://levosgien.net/files/fb-backup.sh">download the script</a></p>

 ]]></content:encoded>
			<wfw:commentRss>http://levosgien.net/2008/09/25/firebird-backup-script/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
