쉘스크립트 문의 드려요. find로 찾은 설정 파일 백업하기

익명 사용자의 이미지

안녕하세요.

아래와 같이 찾고 진행 했는데.. 더 이상 진행이 안되서 문의 드려요..
고수님들의 가이드 부탁 드려요..

가. 하려는 것
설정 파일들을 찾아서 해당 파일을 복사 하려고 합니다.

나. 문제점
파일 위치는 틀리지만 파일명이 같을 경우 덮어 씌우거나 복사하지 못합니다.
파일을 이름을 바꿀 경우 어느 위치에 있는 설정 값인지 알 수 없다는 문제가 발생.

다. 해결 방법
파일의 경로까지 가져 와서 복사 하면 될 것 같다는 생각을 함.

라. 실행 명령어

A. 아래 파일을 복사 하려고 합니다
[root@ufw ~]# find /opt -name "*.conf" | grep local
/opt/splunkforwarder/etc/apps/learned/local/props.conf
/opt/splunkforwarder/etc/apps/Splunk_TA_stream/local/streamfwdlog.conf
/opt/splunkforwarder/etc/apps/Splunk_TA_stream/local/inputs.conf
/opt/splunkforwarder/etc/system/local/inputs.conf
/opt/splunkforwarder/etc/system/local/server.conf
/opt/splunkforwarder/etc/system/local/outputs.conf

B. 위 파일을 아래 경로로 복사 하려고 합니다.
/opt/test/splunkforwarder/etc/apps/learned/local/props.conf
/opt/test/splunkforwarder/etc/apps/Splunk_TA_stream/local/streamfwdlog.conf
/opt/test/splunkforwarder/etc/apps/Splunk_TA_stream/local/inputs.conf
/opt/test/splunkforwarder/etc/system/local/inputs.conf
/opt/test/splunkforwarder/etc/system/local/server.conf
/opt/test/splunkforwarder/etc/system/local/outputs.conf

C. 내가 찾은 방법

dirname을 이용해서 경로만을 가져 올 수 있음
sed 's/opt/opt\/test/g' 를 이용해 경로를 바꿀 수 있음
cp -rp 옵션을 사용 할 경우 /opt/test 디렉토리만 있으면 아래 디렉토리는 알아서 생성됨

D. 아래 쉘스크립트를 통해 각각 list는 가져 올 수 있으나..
dirname, sed, cp -rp 명령어를 적용 할 수가 없어요.
실력이 부족 합니다.

#!/bin/bash
count=$(find /opt -name "*.conf" | grep local | wc -l )
file_name=$(find /opt -name "*.conf" | grep local )

for ((i=1; i<=$count; i++ ))
do
echo $file_name | awk -F " " '{print $'$i'}'
done

.의 이미지

<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><i><font color="#9A1900">#!/bin/bash</font></i>
<font color="#009900">files</font><font color="#990000">=</font><font color="#009900">$(</font>find /opt -name <font color="#FF0000">"*.conf"</font> <font color="#990000">|</font> grep <b><font color="#0000FF">local</font></b><font color="#990000">)</font>
<font color="#009900">from</font><font color="#990000">=</font><font color="#FF0000">'splunkforwarder'</font>
<font color="#009900">to</font><font color="#990000">=</font><font color="#FF0000">'test'</font>
 
<b><font color="#0000FF">while</font></b> <font color="#009900">IFS</font><font color="#990000">=</font><font color="#FF0000">''</font> <b><font color="#0000FF">read</font></b> infile<font color="#990000">;</font> <b><font color="#0000FF">do</font></b>
        <font color="#009900">outfile</font><font color="#990000">=</font><font color="#009900">$(</font>echo <font color="#FF0000">"$infile"</font> <font color="#990000">|</font> sed <font color="#FF0000">"s@$from@$to@"</font><font color="#990000">)</font>
        <font color="#009900">dir</font><font color="#990000">=</font><font color="#009900">$(</font>dirname <font color="#FF0000">"$outfile"</font><font color="#990000">)</font>
        <b><font color="#0000FF">test</font></b> -d <font color="#FF0000">"$dir"</font> <font color="#990000">||</font>
                {
                        mkdir -p <font color="#FF0000">"$dir"</font>
                        echo <font color="#FF0000">"Make "</font><font color="#009900">$dir</font><font color="#FF0000">""</font>
                }
        cp -f <font color="#FF0000">"$infile"</font> <font color="#FF0000">"$outfile"</font>
        echo <font color="#FF0000">"$infile --&gt; $outfile"</font>
<b><font color="#0000FF">done</font></b> <font color="#990000">&lt;&lt;&lt;</font> <font color="#009900">$files</font>
</tt></pre>
.의 이미지

#!/bin/bash
files=$(find /opt -name "*.conf" | grep local)
from='splunkforwarder'
to='test'
 
while IFS='' read infile; do
        outfile=$(echo "$infile" | sed "s@$from@$to@")
        dir=$(dirname "$outfile")
        test -d "$dir" ||
                {
                        mkdir -p "$dir"
                        echo "Make "$dir""
                }
        cp -f "$infile" "$outfile"
        echo "$infile --> $outfile"
done <<< $files
.의 이미지

from='splunkforwarder'
to='test'

이 부분을 알아서 수정하기 바랍니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.