<?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; backup</title>
	<atom:link href="http://linuxadminzone.com/tag/backup/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>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>

