LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 10-16-2011, 11:49 AM   #1
PsYc0TIC
LQ Newbie
 
Registered: Oct 2011
Posts: 9

Rep: Reputation: Disabled
Question Backup then truncate log file


Okay... I have been searching and searching to no avail on what I am specifically looking for but here goes

I have a log file (several log files in several locations) that I was to backup and then truncate because they get big fast and once a week I want to just click a bash script to do the above task

Say the log is in /home/widget/log/x.log
and I want to back it up to /home/logs/<log name with current date on it>.log
and then truncate the original x.log leaving the filename intact

I tried this for the truncate:
Code:
#!/bin/bash
cat /dev/null >|  /home/widget/log/x.log
and that works just fine

I tried this for a backup of file:
Code:
#!/bin/bash
SRCD="/home/widget/log/x.log"
TGTD=" /home/logs/<log name with current date on it>.log
OF=.-$(date +%Y%m%d).tgz
tar -cZf $TGTD$OF $SRCD
and it creates a file in the desired dir but it is empty and I do not want a tar file I was the same file type it started as (but as I said this only creates an empty file and doesn't backup the original log file)

I am pretty new to linux and any help would be appreciated

A script that contained the back feature and the tuncate feature following would help me a lot. I have several different ones I need to just change the original log location and destination paths for and I will later set up my cron job to perform this automatically.

Thank you in advance for your help

P.S. I use CentOS 5.7
 
Old 10-16-2011, 12:03 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Is there a reason why you can't use logrotate?
 
Old 10-16-2011, 12:25 PM   #3
PsYc0TIC
LQ Newbie
 
Registered: Oct 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
logrotate will not allow me to set the original log file location to different places. I have 5 logs in different places plus logrotate will not truncate after backup.

Last edited by PsYc0TIC; 10-16-2011 at 12:28 PM.
 
Old 10-16-2011, 04:06 PM   #4
PsYc0TIC
LQ Newbie
 
Registered: Oct 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
okay I got the backup code working now and I guess it is okay that it is an archived file for space reasons but will this work for backup then truncate:

Code:
#!/bin/bash
SRCD="/home/widget/log/x.log"
TGTD=" /home/logs/"
OF=backup-$(date +%Y%m%d).tgz
tar czf $TGTD$OF $SRCD
cat /dev/null >| /home/widget/log/x.log
or is there something I need to put in between"

Code:
tar czf $TGTD$OF $SRCD
and
Code:
cat /dev/null >| /home/widget/log/x.log
for some kind of short pause... I don't know and I don't want to run this until I get an opinion on whether it is correct or not

thanks again in advance for the help
 
Old 10-16-2011, 04:54 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
In BASH just
Code:
$>/home/widget/log/x.log
but note that the application using the log file should be stopped to allow it to close its open file descriptors or the log file will remain open and growing.
 
Old 10-16-2011, 05:13 PM   #6
PsYc0TIC
LQ Newbie
 
Registered: Oct 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by unSpawn View Post
In BASH just
Code:
$>/home/widget/log/x.log
but note that the application using the log file should be stopped to allow it to close its open file descriptors or the log file will remain open and growing.
I don't understand

my truncate script truncates just fine... my backup script works just fine (all without stopping the program generating the log) and the log needs to remain open since it is a log after all

will my script above work or is there something that need to go between the last two lines to give a slight pause?

Last edited by PsYc0TIC; 10-16-2011 at 05:15 PM.
 
Old 10-16-2011, 06:27 PM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by PsYc0TIC View Post
will my script above work (..)?
If it works for you, fine, if it doesn't just revisit the thread.
 
Old 10-16-2011, 07:15 PM   #8
PsYc0TIC
LQ Newbie
 
Registered: Oct 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
it didn't truncate but it did backup so this is incorrect.

how about this.... I have the two scripts that work independently.. how about what would need to be in a seperate script that calls the backup script, waits 3 seconds, then calls the truncate script?

it is okay to lose 3 seconds of log data
 
Old 10-16-2011, 09:52 PM   #9
PsYc0TIC
LQ Newbie
 
Registered: Oct 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
never mind I figured it out
 
Old 10-17-2011, 01:30 AM   #10
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by PsYc0TIC View Post
never mind I figured it out
So what was it then?
 
Old 10-17-2011, 04:22 AM   #11
PsYc0TIC
LQ Newbie
 
Registered: Oct 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
well not that everyone was so eager to help me but it was a simple answer to my question.... remember when I asked if anything needed to go between the last two lines? The answer would have been a simple "yes and you need to combine the last two separated by &&"

like this:
Code:
#!/bin/bash
SRCD="/home/urt/.q3a/q3ut4/games.log"
TGTD="/root/Desktop/CTF_Logs/"
OF=ctflog-$(date +%Y%m%d).tgz
tar czf $TGTD$OF $SRCD && cat /dev/null >| /home/urt/.q3a/q3ut4/games.log
 
Old 10-17-2011, 04:25 AM   #12
PsYc0TIC
LQ Newbie
 
Registered: Oct 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
my original question still remains.... how can I make it backup the file as a .log rather than turning it into an archive?

What would this part need to be changed to to leave the file extension intact? (I am not concerned about space as I have loads of it)
Code:
OF=ctflog-$(date +%Y%m%d).tgz
tar czf $TGTD$OF $SRCD
 
Old 10-17-2011, 11:06 AM   #13
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by PsYc0TIC View Post
how can I make it backup the file as a .log rather than turning it into an archive?
By not using tar. Copy following as "rotatelog":
Code:
#!/bin/bash
progn=${0//*\//}
__help() { echo "${progn}: [ -f file -d dest_dir ]"; exit 1; }
[ $# -ne 4 ] && __help
while getopts f:d:h OPT; do case "${OPT}" in
 f) [ -f "${OPTARG}" ] || { echo "No file \"${OPTARG}\", exiting." 1>&2; exit 1; }
    SOURCE="${OPTARG}";;
 d) [ -d "${OPTARG}" ] || { echo "No directory \"${OPTARG}\", exiting." 1>&2; exit 1; }
    DSTDIR="${OPTARG}";;
 h|*) __help;; esac; done
DSTDATE="$(/bin/date +'-%Y%m%d')"; DSTNAME="${SOURCE//*\//}"
[ "${DSTNAME}" != "${DSTNAME//.*/}" ] \
&& { EXT="${DSTNAME//*./}"; DSTNAME="${DSTNAME//.*/}${DSTDATE}.${EXT}"; } \
|| { DSTNAME="${DTSNAME}${DSTDATE}"; }
/bin/cp -p "${SOURCE}" "${DSTDIR}/${DSTNAME}" \
|| echo "Copying of ${SOURCE} to ${DSTDIR}/${DSTNAME} failed." 1>&2
exit 0
...and try 'rotatelog -f /home/urt/.q3a/q3ut4/games.log -d /var/tmp' (never save logs in / or /root).
 
Old 10-17-2011, 07:54 PM   #14
PsYc0TIC
LQ Newbie
 
Registered: Oct 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
wow that was a lot of code for a simple task

never mind... I figured that one out too

one line is all it took

Code:
cp  $TGTD$OF
instead of:
Code:
tar czf $TGTD$OF $SRCD
fixed the file extension thing so I don't have to have it archived
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
scp truncate text file busy - copying file is not a running binary jetberrocal Linux - Server 3 06-24-2010 03:56 PM
Truncate from beginning of the file lvprabhu Programming 12 05-25-2010 09:43 PM
Truncate the Lines from the File saurabhchokshi Programming 7 06-16-2009 09:12 AM
how to backup & truncate the log file while the process is running mvenkat_in Programming 13 10-30-2007 07:03 AM
Truncate A File Dovetails Linux - Newbie 2 10-22-2005 06:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 07:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration