DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 4th February 2010
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 3,154
Thanked 182 Times in 149 Posts
Default Archiving a log file to a directory with the date incorporated in the new name

Being tired of renaming my serial.log files, I hacked the following script.
I left in the comments and the debugging stuff, so you can see my step by step approach
Code:
#!/bin/sh

FILE=serial.log
BUPDIR=Serconlogs

if [ ! -f ${FILE} ] ; then
   echo "$0: Sorry no file ${FILE} found!"
   exit 1
fi

mkdir -p ${BUPDIR}

# -rw-r--r--  1 j65nko  j65nko  146366 Feb  3 05:03:54 2010 serial.log
#    1        2   3       4       5     6   7    8      9     10

listing=$(ls -lT ${FILE})

# echo ${listing} | awk '{ print $6, $7, $8, $9 }'
# FILEnew=$(echo ${listing} | awk '{ printf("%s_%s_%s_%s_%s" , $9,$6,$7,$8,$10) }')
# echo $FILEnew
# 2010_Feb_3_05:03:54_serial.log
# we need to align the day in a field of 2 

FILEnew=$(echo ${listing} | awk '{ printf("%s_%s_%02u_%s_%s" , $9,$6,$7,$8,$10) }')
# echo $FILEnew
# 2010_Feb_03_05:03:54_serial.log

#echo mv ${FILE} ${BUPDIR}/${FILEnew}
mv ${FILE} ${BUPDIR}/${FILEnew}

ls -l ${BUPDIR}
In the following I first create a fake new file to be archived then run the script
Code:
$ echo aap >serial.log
$ ./arch-serial.log                                                      

total 3364
-rw-r--r--  1 j65nko  j65nko   146366 Feb  3 05:03 2010_Feb_03_05:03:54_serial.log
-rw-r--r--  1 j65nko  j65nko        4 Feb  4 04:47 2010_Feb_04_04:47:17_serial.log
-rw-r--r--  1 j65nko  wheel   1093167 Jun 12  2009 serial.log.old.txt
-rw-r--r--  1 j65nko  j65nko   433313 Jan 26 17:45 serial.log.old
For other examples of using awk and printf to manipulate directory listing information see http://www.daemonforums.org/showthread.php?t=2611
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #2   (View Single Post)  
Old 6th February 2010
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 3,154
Thanked 182 Times in 149 Posts
Default Similar but now archive a directory and it's contents

The OpenBSD snapshots that I download by default end up in a directory Snapshots. To archive them before fetching a new snapshot, I use this script.

Code:
#!/bin/sh
# archive OpenBSD snapshots
# use the bsd.rd date in name of backup

DIR=Snapshots
BUPDIR=Snap-archive

if [ ! -d ${DIR} ] ; then
   echo "$0: Sorry no directory ${DIR} found!"
   exit 1
fi

mkdir -p ${BUPDIR}

# ls -lT Snapshots/bsd.rd
# -rw-r--r--  1 j65nko  j65nko  2162 Jan 28 18:10:51 2010 Snapshots/bsd.rd
#    1        2   3       4      5    6   7    8      9     10
listing=$(ls -lT ${DIR}/bsd.rd)

DIRnew=$(echo ${listing} | awk '{ printf("%s_%s_%02u_%s") , $9,$6,$7,$8 }')
#echo $DIRnew
#exit


# rename $DIR 
mv ${DIR} ${DIRnew}
# move renamed directory into archive directory
mv ${DIRnew} ${BUPDIR}/${DIRnew}

ls -l ${BUPDIR} ${BUPDIR}/${DIRnew}

# -- end of file
The similarities with the script in the previous script are obvious.
The output when run with the -vx shell options:

Code:
sh -vx arch-snap                                                     
#!/bin/sh
# archive OpenBSD snapshots
# use the bsd.rd date in name of backup

DIR=Snapshots
+ DIR=Snapshots
BUPDIR=Snap-archive
+ BUPDIR=Snap-archive

if [ ! -d ${DIR} ] ; then
   echo "$0: Sorry no directory ${DIR} found!"
   exit 1
fi
+ [ ! -d Snapshots ]

mkdir -p ${BUPDIR}
+ mkdir -p Snap-archive

# ls -lT Snapshots/bsd.rd
# -rw-r--r--  1 j65nko  j65nko  2162 Jan 28 18:10:51 2010 Snapshots/bsd.rd
#    1        2   3       4      5    6   7    8      9     10
listing=$(ls -lT ${DIR}/bsd.rd)
+ ls -lT Snapshots/bsd.rd
+ listing=-rw-r--r--  1 j65nko  j65nko  6244738 Feb  3 15:49:19 2010 Snapshots/bsd.rd

DIRnew=$(echo ${listing} | awk '{ printf("%s_%s_%02u_%s") , $9,$6,$7,$8 }')
+ echo -rw-r--r-- 1 j65nko j65nko 6244738 Feb 3 15:49:19 2010 Snapshots/bsd.rd
+ awk { printf("%s_%s_%02u_%s") , $9,$6,$7,$8 }
+ DIRnew=2010_Feb_03_15:49:19
#echo $DIRnew
#exit


# rename $DIR 
mv ${DIR} ${DIRnew}
+ mv Snapshots 2010_Feb_03_15:49:19
# move renamed directory into archive directory
mv ${DIRnew} ${BUPDIR}/${DIRnew}
+ mv 2010_Feb_03_15:49:19 Snap-archive/2010_Feb_03_15:49:19

ls -l ${BUPDIR} ${BUPDIR}/${DIRnew}
+ ls -l Snap-archive Snap-archive/2010_Feb_03_15:49:19
Snap-archive:
total 12
drwxr-xr-x  2 j65nko  j65nko  512 Feb  6 03:51 2010_Feb_03_15:49:19
drwxr-xr-x  2 j65nko  j65nko  512 Feb  5 01:22 2010_Feb_05_01:22:00
drwxr-xr-x  2 j65nko  j65nko  512 Feb  5 01:13 2010_Jan_28_18:10:51

Snap-archive/2010_Feb_03_15:49:19:
total 503352
-rw-r--r--  1 j65nko  j65nko     97601 Feb  3 15:48 INSTALL.i386
-rw-r--r--  1 j65nko  j65nko      7553 Feb  5 01:40 Logfile
-rw-r--r--  1 j65nko  j65nko      2162 Feb  3 15:49 SHA256
-rw-r--r--  1 j65nko  j65nko  50381006 Feb  3 15:49 base47.tgz
-rw-r--r--  1 j65nko  j65nko   7510284 Feb  3 15:49 bsd
-rw-r--r--  1 j65nko  j65nko   7529736 Feb  3 15:49 bsd.mp
-rw-r--r--  1 j65nko  j65nko   6244738 Feb  3 15:49 bsd.rd
-rw-r--r--  1 j65nko  j65nko  92614312 Feb  3 15:49 comp47.tgz
-rw-r--r--  1 j65nko  j65nko    522115 Feb  3 15:49 etc47.tgz
-rw-r--r--  1 j65nko  j65nko   1474560 Feb  3 15:49 floppy47.fs
-rw-r--r--  1 j65nko  j65nko   1474560 Feb  3 15:49 floppyB47.fs
-rw-r--r--  1 j65nko  j65nko   1474560 Feb  3 15:49 floppyC47.fs
-rw-r--r--  1 j65nko  j65nko   9446489 Feb  3 15:48 man47.tgz
-rw-r--r--  1 j65nko  j65nko    364768 Feb  3 15:48 misc47.tgz
-rw-r--r--  1 j65nko  j65nko     53532 Feb  3 15:49 pxeboot
-rw-r--r--  1 j65nko  j65nko      4058 Feb  5 06:07 site47.tgz
-rw-r--r--  1 j65nko  j65nko  15547766 Feb  3 15:49 xbase47.tgz
-rw-r--r--  1 j65nko  j65nko     70003 Feb  3 15:49 xetc47.tgz
-rw-r--r--  1 j65nko  j65nko  39684911 Feb  3 15:49 xfont47.tgz
-rw-r--r--  1 j65nko  j65nko  19880621 Feb  3 15:49 xserv47.tgz
-rw-r--r--  1 j65nko  j65nko   2946035 Feb  3 15:49 xshare47.tgz

# -- end of file
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[fman] tags out of date for ports BSDKaffee Feedback and Suggestions 1 12th June 2009 07:36 AM
strange "~" directory in home directory gosha OpenBSD General 5 23rd February 2009 06:12 PM
Using Date variable? cwhitmore FreeBSD General 9 13th August 2008 07:16 AM
Jails - mount: /usr/home: No such file or directory chris FreeBSD General 6 6th August 2008 10:47 PM
HOWTO: Always install an up-to-date port chris Guides 8 28th May 2008 11:53 AM


All times are GMT. The time now is 08:00 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick