<?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>Linux Admin Zone &#187; bash</title>
	<atom:link href="http://linuxadminzone.com/category/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://linuxadminzone.com</link>
	<description>Adding more reasons to celebrate Open Source.</description>
	<lastBuildDate>Wed, 09 May 2012 10:17:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>A very small bash script challenge</title>
		<link>http://linuxadminzone.com/a-very-small-bash-script-challenge/</link>
		<comments>http://linuxadminzone.com/a-very-small-bash-script-challenge/#comments</comments>
		<pubDate>Wed, 25 Apr 2012 10:35:52 +0000</pubDate>
		<dc:creator>jagbir</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[bash if]]></category>

		<guid isPermaLink="false">http://linuxadminzone.com/?p=501</guid>
		<description><![CDATA[*Kind Note*: This was written with a sense of humor to allow visitors quickly discover code anomaly and suggests fixes but if it is not up to your mark, please close your browser tab instead of making unnecessary noise. Thanks You! I am putting a damn small thing here regarding bash script for fun. Here [...]]]></description>
			<content:encoded><![CDATA[<p><strong>*Kind Note*: This was written with a sense of humor to allow visitors quickly discover code anomaly and suggests fixes but if it is not up to your mark, please close your browser tab instead of making unnecessary noise. Thanks You!</strong><br />
I am putting a damn small thing here regarding bash script for fun.</p>
<p>Here it goes: Need to create a bash script which asks for a word from user, say either &#8220;one&#8221;, &#8220;two&#8221; or &#8220;three&#8221; and then check in single if statement (no else if section) that if its not &#8220;one&#8221; or &#8220;two&#8221; or &#8220;three&#8221; then print Not OK otherwise print OK.</p>
<p>Further, here is a snippet, check and figure out why its not working as expected:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> Enter a word:
<span style="color: #c20cb9; font-weight: bold;">read</span> myword
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$myword</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;one&quot;</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #007800;">$myword</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;two&quot;</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #007800;">$myword</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;three&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</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> Not OK
<span style="color: #000000; font-weight: bold;">else</span>
 <span style="color: #7a0874; font-weight: bold;">echo</span> OK
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<p>Please put your suggestion/solution script in comments. </p>
]]></content:encoded>
			<wfw:commentRss>http://linuxadminzone.com/a-very-small-bash-script-challenge/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>Run scripts as daemon or through cron continuously</title>
		<link>http://linuxadminzone.com/run-scripts-as-daemon-or-through-cron-continuously/</link>
		<comments>http://linuxadminzone.com/run-scripts-as-daemon-or-through-cron-continuously/#comments</comments>
		<pubDate>Thu, 09 Feb 2012 11:23:08 +0000</pubDate>
		<dc:creator>jagbir</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[scripts]]></category>

		<guid isPermaLink="false">http://linuxadminzone.com/?p=466</guid>
		<description><![CDATA[How to run a script per second? Any easy way to check that no multiple instances of the script will run at a single time? I am cnvering 3 simple ways which helps you to fulfill such requirements: Using Cron If script needs to be executed every x minute or a frequency which is more [...]]]></description>
			<content:encoded><![CDATA[<p>How to run a script per second? Any easy way to check that no multiple instances of the script will run at a single time? I am cnvering 3 simple ways which helps you to fulfill such requirements:</p>
<ol>
<li><strong>Using Cron</strong><br />
If script needs to be executed every x minute or a frequency which is more than a minute, then it should run through cron only. You can also place it in cron when the script itself contains code to execute repeatedly. Make sure to redirect all its logs/errors to some log file for checking in case some issue occurs.</li>
<li><strong>Using Daemon</strong><br />
If script requires running on frequency less than a minute which is not supported by cron, then a wrapper script acting as daemon can be created to control the execution of our script. This wrapper shell script will run continuously and execute our script at desired internal after verifying that it should not be running already. Means no two instances should run at a time. What if our wrapper script get&#8217;s terminated somehow? Put it in inittab which will keep it running always. Let&#8217;s take an example.Suppose we need to execute /usr/local/myscripts/example.php every second. Here are the steps you should take to create its wrapper script and start execution:</li>
<ol>
<li>Create a wrapper shell script with same name as actual script (but with .sh extension), let&#8217;s say /root/example.sh:<br />
<code lang="bash">$ cat example.sh<br />
#!/bin/bash<br />
while [ true ]; do<br />
sleep 1 ## frequency at which script will be executed<br />
if [ -z "`ps ax | grep example.php| grep -v grep`"]; then ## do not run more than one instance of script<br />
`nohup /usr/bin/php /usr/local/myscripts/example.php`<br />
fi<br />
done<br />
</code></li>
<li>Put wrapper script entry in inittab to keep it running:<br />
<code lang="bash">$ vim /etc/inittab<br />
# Run xdm in runlevel 5<br />
..<br />
dm:234:respawn:/root/example.sh<br />
..<br />
</code></li>
<li>Finally, re-read inittab to start daemon which in result will start our script:<br />
<code lang="bash"><br />
$ init q<br />
</code><br />
You can verify by checking processlist that daemon and our scripts are running.</li>
</ol>
<li><strong> Using <a href="http://www.fatalmind.com/software/hatools/cron.html" title="hatool" target="_blank">hatool</a> </strong><br />
This is another way to run scripts. We will make use of two external tools: halockrun and hatimerun. halockrun will keep script running and keep a lock file to check avoid multiple instances while with hatimerun we can specify after how much time script can be terminated (because sometimes scripts become stale/defunct).<br />
Let&#8217;s take an example, here&#8217;s the example cron entry using these tools:<br />
<code lang="bash">* * * * hatimerun -t 300 halockrun -nc /tmp/example.lock /usr/bin/php /usr/local/myscripts/example.php </code><br />
In above example, hatimerun will terminate script after 300 ms and halockrun will again execute it keeping lock file to avoid multiple instances.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://linuxadminzone.com/run-scripts-as-daemon-or-through-cron-continuously/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>quickly check your mail server using telnet, mail or mutt</title>
		<link>http://linuxadminzone.com/quickly-check-your-mail-server-using-telnet-mail-or-mutt/</link>
		<comments>http://linuxadminzone.com/quickly-check-your-mail-server-using-telnet-mail-or-mutt/#comments</comments>
		<pubDate>Sun, 31 Oct 2010 13:03:00 +0000</pubDate>
		<dc:creator>jagbir</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[mail]]></category>
		<category><![CDATA[mutt]]></category>
		<category><![CDATA[smtp]]></category>
		<category><![CDATA[telnet]]></category>

		<guid isPermaLink="false">http://linuxadminzone.com/?p=254</guid>
		<description><![CDATA[There are of course various ways to check whether your mail server is now configured ok or not but what I found is that checking through telnet is quick and easy. let&#8217;s check our mail server now, it may be mail.youdomain.com or localhost depending on what you are using right now, here&#8217;s the full process: [...]]]></description>
			<content:encoded><![CDATA[<p>There are of course various ways to check whether your mail server is now configured ok or not but what I found is that checking through telnet is quick and easy. </p>
<p>let&#8217;s check our mail server now, it may be mail.youdomain.com or localhost depending on what you are using right now, here&#8217;s the full process:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># telnet localhost smtp</span>
Trying 127.0.0.1...
Connected to localhost.
Escape character is <span style="color: #ff0000;">'^]'</span>.
<span style="color: #000000;">220</span> mail.example.com ESMTP Postfix
mail from: me<span style="color: #000000; font-weight: bold;">@</span>example.com
<span style="color: #000000;">250</span> 2.1.0 Ok
rcpt to: other<span style="color: #000000; font-weight: bold;">@</span>example.com
<span style="color: #000000;">250</span> 2.1.5 Ok
data
<span style="color: #000000;">354</span> End data with <span style="color: #000000; font-weight: bold;">&lt;</span>CR<span style="color: #000000; font-weight: bold;">&gt;&lt;</span>LF<span style="color: #000000; font-weight: bold;">&gt;</span>.<span style="color: #000000; font-weight: bold;">&lt;</span>CR<span style="color: #000000; font-weight: bold;">&gt;&lt;</span>LF<span style="color: #000000; font-weight: bold;">&gt;</span>
Subject: Just a test. 
This is <span style="color: #7a0874; font-weight: bold;">test</span> mail using telnet.  
.
<span style="color: #000000;">250</span> 2.0.0 Ok: queued <span style="color: #c20cb9; font-weight: bold;">as</span> 6846838401D6
quit
<span style="color: #000000;">221</span> 2.0.0 Bye
Connection closed by foreign host.
<span style="color: #666666; font-style: italic;">#_</span></pre></div></div>

<p>here,</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># telnet localhost smtp</span></pre></div></div>

<p>We are trying connecting localhost on port 25 (smtp). It should get connected and ready to accept your next command</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mail from: me<span style="color: #000000; font-weight: bold;">@</span>example.com</pre></div></div>

<p>here you are specifying the sender mail id, it should be a valid mail account otherwise mail server can reject the sender address.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">rcpt to: other<span style="color: #000000; font-weight: bold;">@</span>example.com</pre></div></div>

<p>This is the recipient mail address.<br />
then write &#8216;data&#8217; and then in new line write &#8216;Subject: your subject&#8217;, press Enter and start writing contents of your mail. when you want to close, write a dot (.) and press Enter. message should be sent/queued in mail queue.<br />
Check the recipient mail address, if mail server is working ok, you should get this mail there. </p>
<p>Other than this method where you can quickly use mail command also, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># echo &quot;This is a test mail to check mail server.&quot; | mail - s &quot;This is test subject&quot; other@example.com</span></pre></div></div>

<p>This is a single line command but alas! we didn&#8217;t supply sender here which may trigger rejection from mail server. </p>
<p>You can also use mutt tool to facilitate this, if its there in your machine, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># mutt -s &quot;Test mail&quot; other@example.com &lt; message.txt</span></pre></div></div>

<p>here message.txt contains mail message. </p>
]]></content:encoded>
			<wfw:commentRss>http://linuxadminzone.com/quickly-check-your-mail-server-using-telnet-mail-or-mutt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ensuring secure access to Production Linux Servers</title>
		<link>http://linuxadminzone.com/ensuring-secure-access-to-production-linux-servers/</link>
		<comments>http://linuxadminzone.com/ensuring-secure-access-to-production-linux-servers/#comments</comments>
		<pubDate>Sun, 22 Aug 2010 06:04:38 +0000</pubDate>
		<dc:creator>jagbir</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[public/private keys]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://linuxadminzone.com/?p=245</guid>
		<description><![CDATA[I was amazed to hear from my friend that one of their server got hacked and reason may be that their part-time admin set password of root user as &#8216;admin&#8217;. Wow!! can&#8217;t believe it! They dont have right to cry about security attacks as they themselves keep their door opens I&#8217;ve suggested them some points [...]]]></description>
			<content:encoded><![CDATA[<p>I was amazed to hear from my friend that one of their server got hacked and reason may be that their part-time admin set password of root user as &#8216;admin&#8217;. Wow!! can&#8217;t believe it! They dont have right to cry about security attacks as they themselves keep their door opens <img src='http://linuxadminzone.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p> I&#8217;ve suggested them some points as per described below for ensuring secure access to servers. They have 5-6 Linux servers. This is obviously may not be the best way and I&#8217;m as always appreciate if you can give your suggestion in comments. My approach is that from 6 servers, we will be able to login only in 2 servers from remote through key based access and from these 2 server, we can access remaining. Here&#8217;s what we did:</p>
<p><strong>1. Disable root access</strong><br />
Completely disable root login access from remote. Period. Open <strong>/etc/ssh/sshd_config</strong> and add/remove comment from this line:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># vi /etc/ssh/sshd_config</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">PermitRootLogin no</pre></div></div>

<p><strong>2. Login only through non-root user </strong><br />
Create non-root user and create public/private key pair for it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ adduser loginu</pre></div></div>

<p>login to &#8216;loginu&#8217; user created above, or if you are in root, just su:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># su loginu</span>
$ <span style="color: #c20cb9; font-weight: bold;">ssh-keygen</span> <span style="color: #660033;">-t</span> dsa</pre></div></div>

<p>Enter details while generating keys, enter good passphares and always remember it. Now you can go ahead and disable password based access completely so user can only login by using keys but this may be too restrictive or problematic for them if they forget passphares etc. if you want to go ahead, make sure these statements are there in /etc/ssh/sshd_config file:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #666666; font-style: italic;"># vi /etc/ssh/sshd_config</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">PasswordAuthentication no
PubkeyAuthentication <span style="color: #c20cb9; font-weight: bold;">yes</span>
AuthorizedKeysFile      .ssh<span style="color: #000000; font-weight: bold;">/</span>authorized_keys</pre></div></div>

<p>copy the key you created earlier (there should be two files in ~loginu/.ssh/ directory: id_dsa, id_dsa.pub. so copy id_dsa) to your pc so from next time you can use this key to login into the server. </p>
<p>Just make sure you are able to login through &#8216;loginu&#8217; user before applying these ssh settings. Jump to terminal in your pc and try to login with key:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-i</span> id_dsa loginu<span style="color: #000000; font-weight: bold;">@</span>your.server.ip</pre></div></div>

<p>It will ask for passpahres and after supplying it you should be able to login into the server. Please make note that this is very confidential key and store it in good place/directory. Alternatively you can also generate keys in your own pc and store them at server to facilitate login. But if you want flexibility to have only one key (like carry it in your usb stick) and be able to login with it, I found this approach good to use server keys instead of pc keys. </p>
<p><strong>3. (Optional) Restrict login by IPs</strong><br />
Now come back in server. You can further strengthen security by allowing only select IPs to log in:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># vi /etc/ssh/sshd_config</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">AllowUsers loginu<span style="color: #000000; font-weight: bold;">@</span>aa.aa.aa.aa loginu<span style="color: #000000; font-weight: bold;">@</span>bb.bb.bb.bb loginu<span style="color: #000000; font-weight: bold;">@</span>cc.cc.cc.cc</pre></div></div>

<p>Here replace aa/bb/cc with actual IP addresses from where you want to allow access. </p>
<p>Going ahead, optionally, You can also change port for ssh from default 22 to other by using this <a href="http://linuxadminzone.com/quickly-change-your-ssh-port-from-default-22-to-something-higher/">guide</a> but as I think we are only allowing access through keys and from select remote places only, this you may skip. </p>
<p>Reload sshd daemon to apply settings which you have set in /etc/ssh/sshd_config by:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># service sshd reload</span></pre></div></div>

<p>Without closing current login session, try to login again from other terminal to check you are able to login into the server. </p>
<p><strong> 4. Secure other servers </strong><br />
As mentioned earlier, I preferred to treat first 2 server as &#8216;login&#8217; server in which we can login from anywhere using user &#8216;loginu&#8217; with key and then can login to other servers. So effectively other servers would not allow direct access from remote. Jump to server 3-6 and set following:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># vi /etc/ssh/sshd_config</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">AllowUsers loginu<span style="color: #000000; font-weight: bold;">@</span>aa.aa.aa.aa root<span style="color: #000000; font-weight: bold;">@</span>aa.aa.aa.aa loginu<span style="color: #000000; font-weight: bold;">@</span>bb.bb.bb. root<span style="color: #000000; font-weight: bold;">@</span>bb.bb.bb.bb</pre></div></div>

<p>here aa.aa/bb.bb indicates IP address of server #1 and #2 (login servers). So in this (#3) server we can login from those server(s) only. After making changes, reload ssh daemon to apply settings in all of these servers:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># service sshd reload</span></pre></div></div>

<p><strong> 5. Other services </strong><br />
I suggested to disable every service that we don&#8217;t need in servers. That&#8217;s the best approach to secure them <img src='http://linuxadminzone.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> . These servers has role of web servers and rsync process is there to sync files. In that case, created another non-root user:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># adduser rsyncuser</span></pre></div></div>

<p>generate keys (you can generate passphares less keys) for this user as well. Create same user in all other servers and put first server&#8217;s (from where rsync initiate) keys in them. Dont allow this user to login from remote but only from server where rsync initiate. I&#8217;ve documented rsync process <a href="http://linuxadminzone.com/script-to-sync-files-between-web-severs-having-plesk/">here</a>, if you want to go ahead and configure it. Similarly, if you need services like FTP then allow this only from selected IP address (by configuring /etc/hosts.allow) or firewall etc.</p>
<p><strong> 6. Configure DenyHosts </strong><br />
To further prevent attacks and block any IP address from which several failed login attempt originated, you should configure DenyHosts script ( I have documented howto on DenyHosts <a href="http://linuxadminzone.com/install-and-configure-denyhost/">here</a>) or equivalent.  </p>
<p>Other Most Read Articles:<br />
*  <a href="http://linuxadminzone.com/top-5-most-useful-commands-tools-for-linux-administrators/">Top 5 Linux commands for Administrators.</a><br />
* <a href="http://linuxadminzone.com/quick-howto-install-and-configure-munin-for-server-monitoring/">Install and configure Munin/Monitor for monitoring. </a><br />
* <a href="http://linuxadminzone.com/change-timezone-in-your-linux-server-quickly/ ">Change time zone in your Linux machine quickly.</a><br />
* <a href="http://linuxadminzone.com/detect-directory-or-file-changes-in-linuxunix/">Detect directory changes in Linux.</a><br />
* <a href="http://linuxadminzone.com/bash-script-to-backup-essential-log-files-of-linux-server/">Script to backup essential log files.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://linuxadminzone.com/ensuring-secure-access-to-production-linux-servers/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Bash script to backup essential log files of Linux Server</title>
		<link>http://linuxadminzone.com/bash-script-to-backup-essential-log-files-of-linux-server/</link>
		<comments>http://linuxadminzone.com/bash-script-to-backup-essential-log-files-of-linux-server/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 08:03:48 +0000</pubDate>
		<dc:creator>jagbir</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[backup]]></category>

		<guid isPermaLink="false">http://linuxadminzone.com/?p=171</guid>
		<description><![CDATA[Here&#8217;s small bash script to backup important log files from a server to a backup server. You should customize it per your environment. I&#8217;ve deployed this script in some hosts and its working fine for me but I&#8217;m not making any guarantee that this will work for you as well. Task: Two most important log [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s small bash script to backup important log files from a server to a backup server. You should customize it per your environment. I&#8217;ve deployed this script in some hosts and its working fine for me but I&#8217;m not making any guarantee that this will work for you as well. </p>
<p>Task: Two most important log files in any Redhat based distro is /var/log/secure and /var/log/messages. These are basic log files and there are more log files when your server perform additional roles such as a database server, web server, mail server etc. You can look log files of other installed softwares also and add them in this script to backup them. I have a separate backup server where I want to transfer my log files after compressing them. You can transfer them in some location in case you dont have a separate backup host or environment.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;">##</span>
<span style="color: #666666; font-style: italic;">## hostlogBackup.sh: perform backup of essential log files. Developed by Jagbir Singh (contact AT jagbir DOT info)</span>
<span style="color: #666666; font-style: italic;">## You are free to use or distribute it in whatever means but I'll be happy if you send me a copy of updated one. </span>
<span style="color: #666666; font-style: italic;">##</span>
&nbsp;
<span style="color: #666666; font-style: italic;">## create some varibales</span>
&nbsp;
<span style="color: #007800;">yesterDate</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;-1 day&quot;</span> +<span style="color: #000000; font-weight: bold;">%</span>d-<span style="color: #000000; font-weight: bold;">%</span>b-<span style="color: #000000; font-weight: bold;">%</span>g<span style="color: #000000; font-weight: bold;">`</span>  <span style="color: #666666; font-style: italic;">## yesterday's date</span>
<span style="color: #007800;">toDay</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>u<span style="color: #000000; font-weight: bold;">`</span>;   <span style="color: #666666; font-style: italic;">## day of week in numeric</span>
<span style="color: #007800;">bakServer</span>=<span style="color: #ff0000;">&quot;backup-user@server-ip&quot;</span> <span style="color: #666666; font-style: italic;">## backup server address user@hostname, use directory name if backup in same host </span>
<span style="color: #007800;">bakHost</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$bakServer</span>:/backup/host/firsthost&quot;</span> <span style="color: #666666; font-style: italic;">## specify directory where log files will be copied</span>
<span style="color: #007800;">bakHostDaily</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$bakHost</span>/daily/&quot;</span> <span style="color: #666666; font-style: italic;">## directory for daily backup files</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log <span style="color: #666666; font-style: italic;">## change directory where important log file resides </span>
&nbsp;
<span style="color: #666666; font-style: italic;"># compress messages log file</span>
<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cp</span> messages messages-log<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">`/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">tar</span> czf messages_<span style="color: #007800;">$toDay</span>.tgz messages-log<span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># compress secure log file</span>
<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cp</span> secure secure-log<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">`/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">tar</span> czf secure_<span style="color: #007800;">$toDay</span>.tgz secure-log<span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># compress mysqld log file. comment following 2 lines if you are not using mysql</span>
<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cp</span> mysqld.log mysqld-log<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">`/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">tar</span> czf mysqld_<span style="color: #007800;">$toDay</span>.tgz mysqld-log<span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># compress apache log files. uncomment if your server runs apache service. </span>
<span style="color: #666666; font-style: italic;">#`cp httpd/access_log ./access-log`</span>
<span style="color: #666666; font-style: italic;">#`cp httpd/error_log ./error-log`</span>
<span style="color: #666666; font-style: italic;">#`/bin/tar czf httpd_$toDay.tgz access-log error-log`</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">#copy all compressed files to backup server, you must set secure authentication for password less scp, else you have to enter password</span>
<span style="color: #000000; font-weight: bold;">`/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">scp</span> <span style="color: #000000; font-weight: bold;">*</span>_<span style="color: #007800;">$toDay</span>.tgz <span style="color: #007800;">$bakHostDaily</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#remove all temp files</span>
<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">*</span>-log<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">*</span>_<span style="color: #007800;">$toDay</span>.tgz<span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Apart from daily, Take a weekly backup on Monday for files which get rotated on weekly basis. </span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$toDay</span> == <span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> 
&nbsp;
		<span style="color: #666666; font-style: italic;"># take backup of messages log file</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> messages.1 <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #000000; font-weight: bold;">`/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">tar</span>  czf message_<span style="color: #007800;">$yesterDate</span>.tgz messages.1<span style="color: #000000; font-weight: bold;">`</span>
                <span style="color: #000000; font-weight: bold;">`/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">scp</span> message_<span style="color: #007800;">$yesterDate</span>.tgz <span style="color: #007800;">$bakHost</span><span style="color: #000000; font-weight: bold;">`</span>
                <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> message_<span style="color: #007800;">$yesterDate</span>.tgz<span style="color: #000000; font-weight: bold;">`</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
		<span style="color: #666666; font-style: italic;"># take backup of secure log file</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> secure.1 <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #000000; font-weight: bold;">`/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">tar</span>  czf secure_<span style="color: #007800;">$yesterDate</span>.tgz secure.1<span style="color: #000000; font-weight: bold;">`</span>
                <span style="color: #000000; font-weight: bold;">`/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">scp</span> secure_<span style="color: #007800;">$yesterDate</span>.tgz <span style="color: #007800;">$bakHost</span><span style="color: #000000; font-weight: bold;">`</span>
                <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> secure_<span style="color: #007800;">$yesterDate</span>.tgz<span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<p>Again I&#8217;m stressing on point that this is a very basic script and doesn&#8217;t handle any unforeseen situations like file doesn&#8217;t exist or what happens if compression or copying to other server fails etc. You have to do it yourself. </p>
<p>The point of taking backup on weekly basis is that the file combines week log in a single file which is easy to retain. Daily backup files here get overwritten but I want to retain weekly files for longer duration. </p>
<p>Now you should run this script on daily basis through cron at around 4:30am. why 4:30? because the syslog service normally runs at 4:03am daily to rotate log files and you should copy the rotated file if needed.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$crontab</span> <span style="color: #660033;">-l</span> 
<span style="color: #666666; font-style: italic;"># backup logs to backup server daily</span>
<span style="color: #000000;">30</span> <span style="color: #000000;">4</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">bash</span> <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>logBackup<span style="color: #000000; font-weight: bold;">/</span>hostlogBackup.sh</pre></div></div>

<p>That&#8217;s all we need to do. Let me know your views about it.</p>
]]></content:encoded>
			<wfw:commentRss>http://linuxadminzone.com/bash-script-to-backup-essential-log-files-of-linux-server/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

