<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Backup script in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/backup-script/m-p/4019350#M93477</link>
    <description>In your script:&lt;BR /&gt; &lt;BR /&gt;&amp;gt; mkdir /tmp/mysqlback&lt;BR /&gt;&lt;BR /&gt;There's no test to see if mkdir was successful. If it fails, your script goes on assuming that the directory does indeed exist. Check the return code:&lt;BR /&gt; &lt;BR /&gt;mkdir /tmp/mysqlback&lt;BR /&gt;ERR=$?&lt;BR /&gt;[ $ERR -ne 0 ] &amp;amp;&amp;amp; echo "mkdir failed error $ERR" &amp;amp;&amp;amp; exit 1&lt;BR /&gt; &lt;BR /&gt;Now this won't fix your problem, but it is a minimum when you are writing production script. Always test that each task is successful (or assume that everything fails until proven successful) &lt;BR /&gt; &lt;BR /&gt;&amp;gt; for var in `find /var/lib/mysql/ -type d | \&lt;BR /&gt;&amp;gt; sed -e "s/\/var\/lib\/mysql\///"`; do&lt;BR /&gt;&amp;gt; echo $var&lt;BR /&gt;&amp;gt; mysqldump -u root --password=$PASS --opt --all $var &amp;gt; $kat/$var.sql done&lt;BR /&gt; &lt;BR /&gt;For reliability, it is important to test each step before moving on. It appears that you are using sed to strip off the leading /var/lib/mysql path and keep what is left. So if that directory has no directories in it, the command fails unexpectedly. The sed expression will return subdirectories too as in:&lt;BR /&gt; &lt;BR /&gt;echo "/var/lib/mysql/a/b/c"| sed -e "s/\/var\/lib\/mysql\///"&lt;BR /&gt;a/b/c&lt;BR /&gt; &lt;BR /&gt;This may also be a failure that must be tested.&lt;BR /&gt; &lt;BR /&gt;I would rewrite this so you can see each step:&lt;BR /&gt; &lt;BR /&gt;MYFIND=$(find /var/lib/mysql -type d)&lt;BR /&gt;[ "$VAR" = "" ] &amp;amp;&amp;amp; echo "\nNo dirs found\n" &amp;amp;&amp;amp; exit 2&lt;BR /&gt;for VAR in "$MYFIND"&lt;BR /&gt;do&lt;BR /&gt;DIR=$(echo $VAR | ed -e "s/\/var\/lib\/mysql\///")&lt;BR /&gt;[ "$DIR" = "" ] &amp;amp;&amp;amp; echo "\nNull DIR value" &amp;amp;&amp;amp; exit 3&lt;BR /&gt;echo $DIR&lt;BR /&gt;mysqldump -u root --password=$PASS --opt --all $DIR &amp;gt; $kat/$DIR.sql&lt;BR /&gt;ERR=$?&lt;BR /&gt;[ $ERR -ne 0 ] &amp;amp;&amp;amp; echo "\nmysqldump failed on DIR=$DIR\n" &amp;amp;&amp;amp; exit 4&lt;BR /&gt;done&lt;BR /&gt; &lt;BR /&gt;Now you can see each step when you run it with set -x at the top of the script.</description>
    <pubDate>Wed, 13 Jun 2007 11:51:09 GMT</pubDate>
    <dc:creator>Bill Hassell</dc:creator>
    <dc:date>2007-06-13T11:51:09Z</dc:date>
    <item>
      <title>Backup script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/backup-script/m-p/4019346#M93473</link>
      <description>Hi there&lt;BR /&gt;&lt;BR /&gt;Im logged as a root, the script is in my home location /home/myuser/myscript&lt;BR /&gt;&lt;BR /&gt;I'm getting this output:&lt;BR /&gt;&lt;BR /&gt;: command not found&lt;BR /&gt;: command not found&lt;BR /&gt;: command not found&lt;BR /&gt;: command not found&lt;BR /&gt;'sqlback: line 24: syntax error near unexpected token `do&lt;BR /&gt;'sqlback: line 24: `      sed -e "s/\/var\/lib\/mysql\///"`; do&lt;BR /&gt;&lt;BR /&gt;when I'm trying to run this script on my linux machine:&lt;BR /&gt;&lt;BR /&gt;#!/bin/sh&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;PATH="/bin:/usr/bin:/usr/sbin:/sbin:/bin/bin:/usr/local/sbin:/usr/local/bin"&lt;BR /&gt;data=`date +%Y-%m-%d`&lt;BR /&gt;&lt;BR /&gt;kat=/home/peterkirklewski/msqlbackup     #backup dir&lt;BR /&gt;tempdir=/tmp/mysqlback&lt;BR /&gt;&lt;BR /&gt;if [ -e $kat ]; then&lt;BR /&gt;    exit&lt;BR /&gt;else&lt;BR /&gt;    mkdir $kat&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;PASS="Zxxxxx"&lt;BR /&gt;&lt;BR /&gt;rm -rf /tmp/mysqlback&lt;BR /&gt;&lt;BR /&gt;mkdir /tmp/mysqlback&lt;BR /&gt;&lt;BR /&gt;for var in `find /var/lib/mysql/ -type d | \&lt;BR /&gt;      sed -e "s/\/var\/lib\/mysql\///"`; do&lt;BR /&gt;        echo $var&lt;BR /&gt;         mysqldump -u root --password=$PASS --opt --all $var &amp;gt; $kat/$var.sql done&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;How to get rid of this problem ?&lt;BR /&gt;&lt;BR /&gt;Cheers</description>
      <pubDate>Wed, 13 Jun 2007 10:22:47 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/backup-script/m-p/4019346#M93473</guid>
      <dc:creator>Piotr Kirklewski</dc:creator>
      <dc:date>2007-06-13T10:22:47Z</dc:date>
    </item>
    <item>
      <title>Re: Backup script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/backup-script/m-p/4019347#M93474</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I would write some 'echo' commands in your script (at the beginning, several in the middle), so you can see where the ': command not found' message appears.&lt;BR /&gt;&lt;BR /&gt;Also echo your variables you have set.&lt;BR /&gt;&lt;BR /&gt;Volkmar&lt;BR /&gt;</description>
      <pubDate>Wed, 13 Jun 2007 10:32:31 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/backup-script/m-p/4019347#M93474</guid>
      <dc:creator>V. Nyga</dc:creator>
      <dc:date>2007-06-13T10:32:31Z</dc:date>
    </item>
    <item>
      <title>Re: Backup script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/backup-script/m-p/4019348#M93475</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;One problem is that you appear to have too many "/" and/or no ";" or newline before the 'done':&lt;BR /&gt;&lt;BR /&gt;...&lt;BR /&gt;for var in `find /var/lib/mysql/ -type d | \&lt;BR /&gt;sed -e "s/\/var\/lib\/mysql\//"`; do&lt;BR /&gt;echo $var&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;</description>
      <pubDate>Wed, 13 Jun 2007 10:37:53 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/backup-script/m-p/4019348#M93475</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-06-13T10:37:53Z</dc:date>
    </item>
    <item>
      <title>Re: Backup script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/backup-script/m-p/4019349#M93476</link>
      <description>use the shell's trace option. man sh for explanation. For example insert the statement&lt;BR /&gt;&lt;BR /&gt;set -x&lt;BR /&gt;&lt;BR /&gt;as the first line in your script.</description>
      <pubDate>Wed, 13 Jun 2007 10:51:47 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/backup-script/m-p/4019349#M93476</guid>
      <dc:creator>John Kittel</dc:creator>
      <dc:date>2007-06-13T10:51:47Z</dc:date>
    </item>
    <item>
      <title>Re: Backup script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/backup-script/m-p/4019350#M93477</link>
      <description>In your script:&lt;BR /&gt; &lt;BR /&gt;&amp;gt; mkdir /tmp/mysqlback&lt;BR /&gt;&lt;BR /&gt;There's no test to see if mkdir was successful. If it fails, your script goes on assuming that the directory does indeed exist. Check the return code:&lt;BR /&gt; &lt;BR /&gt;mkdir /tmp/mysqlback&lt;BR /&gt;ERR=$?&lt;BR /&gt;[ $ERR -ne 0 ] &amp;amp;&amp;amp; echo "mkdir failed error $ERR" &amp;amp;&amp;amp; exit 1&lt;BR /&gt; &lt;BR /&gt;Now this won't fix your problem, but it is a minimum when you are writing production script. Always test that each task is successful (or assume that everything fails until proven successful) &lt;BR /&gt; &lt;BR /&gt;&amp;gt; for var in `find /var/lib/mysql/ -type d | \&lt;BR /&gt;&amp;gt; sed -e "s/\/var\/lib\/mysql\///"`; do&lt;BR /&gt;&amp;gt; echo $var&lt;BR /&gt;&amp;gt; mysqldump -u root --password=$PASS --opt --all $var &amp;gt; $kat/$var.sql done&lt;BR /&gt; &lt;BR /&gt;For reliability, it is important to test each step before moving on. It appears that you are using sed to strip off the leading /var/lib/mysql path and keep what is left. So if that directory has no directories in it, the command fails unexpectedly. The sed expression will return subdirectories too as in:&lt;BR /&gt; &lt;BR /&gt;echo "/var/lib/mysql/a/b/c"| sed -e "s/\/var\/lib\/mysql\///"&lt;BR /&gt;a/b/c&lt;BR /&gt; &lt;BR /&gt;This may also be a failure that must be tested.&lt;BR /&gt; &lt;BR /&gt;I would rewrite this so you can see each step:&lt;BR /&gt; &lt;BR /&gt;MYFIND=$(find /var/lib/mysql -type d)&lt;BR /&gt;[ "$VAR" = "" ] &amp;amp;&amp;amp; echo "\nNo dirs found\n" &amp;amp;&amp;amp; exit 2&lt;BR /&gt;for VAR in "$MYFIND"&lt;BR /&gt;do&lt;BR /&gt;DIR=$(echo $VAR | ed -e "s/\/var\/lib\/mysql\///")&lt;BR /&gt;[ "$DIR" = "" ] &amp;amp;&amp;amp; echo "\nNull DIR value" &amp;amp;&amp;amp; exit 3&lt;BR /&gt;echo $DIR&lt;BR /&gt;mysqldump -u root --password=$PASS --opt --all $DIR &amp;gt; $kat/$DIR.sql&lt;BR /&gt;ERR=$?&lt;BR /&gt;[ $ERR -ne 0 ] &amp;amp;&amp;amp; echo "\nmysqldump failed on DIR=$DIR\n" &amp;amp;&amp;amp; exit 4&lt;BR /&gt;done&lt;BR /&gt; &lt;BR /&gt;Now you can see each step when you run it with set -x at the top of the script.</description>
      <pubDate>Wed, 13 Jun 2007 11:51:09 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/backup-script/m-p/4019350#M93477</guid>
      <dc:creator>Bill Hassell</dc:creator>
      <dc:date>2007-06-13T11:51:09Z</dc:date>
    </item>
    <item>
      <title>Re: Backup script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/backup-script/m-p/4019351#M93478</link>
      <description>Oops, copy+paste dropped "s" from sed:&lt;BR /&gt; &lt;BR /&gt;DIR=$(echo $VAR | sed -e "s/\/var\/lib\/mysql\///")</description>
      <pubDate>Wed, 13 Jun 2007 11:54:55 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/backup-script/m-p/4019351#M93478</guid>
      <dc:creator>Bill Hassell</dc:creator>
      <dc:date>2007-06-13T11:54:55Z</dc:date>
    </item>
    <item>
      <title>Re: Backup script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/backup-script/m-p/4019352#M93479</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;- There is no need to diddle with backslash-escapes in the sed, just don't use '/' as delimiter.&lt;BR /&gt;- If no variables are involved in search/replace - strings I recommend to use single quotes, if needed.&lt;BR /&gt;- To overcome the directory-hierarchie-problem, drop the '/' in your result string.&lt;BR /&gt;So try:&lt;BR /&gt;..&lt;BR /&gt;for var in $(find /var/lib/mysql/ -type d |&lt;BR /&gt;sed -e s,/var/lib/mysql/,, -e s,/,-,,)&lt;BR /&gt;do&lt;BR /&gt;...&lt;BR /&gt;&lt;BR /&gt;mfG Peter&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 13 Jun 2007 12:35:35 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/backup-script/m-p/4019352#M93479</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2007-06-13T12:35:35Z</dc:date>
    </item>
  </channel>
</rss>

