Find the answer to your Linux question:
Results 1 to 3 of 3
Hey, so I'm running a simple backup and log script that is cronjobed to run twice a day. So currently, when new data is added to the log, its added ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2011
    Posts
    1

    Exclamation Changing Log script to write to Top of File


    Hey, so I'm running a simple backup and log script that is cronjobed to run twice a day. So currently, when new data is added to the log, its added to the direct bottom of the log file. However, I would like to have it printed to the very top of the log. The code is attached, I can't quote it in here because I am a new user and the system thinks I have url's in it, when they are just paths. Any suggestions would be useful, cheers and this is my first post!



    ** Note the breaks are in there intentionally, I have the log being printed into a php file which is displayed on my website
    Attached Files Attached Files

  2. #2
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    Here is how I would do it:
    Code:
    #!/bin/bash
    
    # Create a filename that will point to a temporary file:
    TEMP=/tmp/home-backuplog.$(date +'%Y-%m-%d')
    
    # Save the current backup log to the temporary file
    cp /home/backuplog $TEMP
    
    # Do everything, then cat the temporary file
    ( echo '<br />Backup Started<br />'
      mkdir /home/chris/Archive/`date +%R@%d-%m-%Y`
      tar -czf /home/chris/Archive/`date +%R@%d-%m-%Y`/mcs1.tar.gz /home/chris/mcserver1/
      echo 'Backup Completed<br />'
      date
      cat $TEMP
    ) >/home/backuplog #overwrite the old backup log
    
    # delete the temporary file
    rm $TEMP
    Last edited by ramin.honary; 04-06-2011 at 06:00 AM.

  3. #3
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    Here is a slightly more confusing way to do it, but does not require the use of a temporary file. You can use the "ed" text editor to edit files in memory.
    Code:
    #!/bin/bash
    
    # Setup the text you want to prepend to the file in
    # a format that can be used by the "ed" text editor.
    ( echo 1 # this is the "ed" command to edit the beginning of a file
      echo i # this is the "ed" command to begin inserting text
      # Now you are inserting text. Inserting will continue
      # until it sees a line that contains only a "."
      echo '<br />Backup Started<br />'
      mkdir /home/chris/Archive/`date +%R@%d-%m-%Y`
      tar -czf /home/chris/Archive/`date +%R@%d-%m-%Y`/mcs1.tar.gz /home/chris/mcserver1/
      echo 'Backup Completed<br />'
      date
      echo . # this is the "ed" command to stop inserting text
      echo w # this is the "ed" command to save the file
      echo q # this is the "ed" command to quit editing and exit
    ) | ed /home/backuplog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •