Results 1 to 3 of 3

Thread: Creating a filename automatically"

Hybrid View

  1. #1
    Join Date
    Jun 2008
    Location
    /earth/europe/uk/england/west-yorkshire/leeds/mystreet/myhouse
    Posts
    248

    Default Creating a filename automatically"

    I've set a script up which is called using cron every hour or so. The script it runs is a simple affair - just rsyncs some files from A to B.

    It works and currently writes the output to a log file.
    Problem is, this file gets rather large and I want to "rotate" it.

    Currently, to create the log file, it has these lines in the script:-
    Code:
    #!/bin/sh
    LOG_FILE=/home/badger_fruit/my-backup.log
    echo >> $LOG_FILE
    echo "------------------------------" >> $LOG_FILE
    date >> $LOG_FILE
    echo "------------------------------" >> $LOG_FILE
    How can I get it to create a filename based on the date?

    For example, on Monday 01 January 2009, the file should be called 2009-01-01-backup.log; he following day to be 2009-01-02-backup.log and so on.

    I look forward to any / all suggestions!
    Thanks

    Rich

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Creating a filename automatically"

    LOG_FILE=/home/badger_fruit/$(date '+%Y-%m-%d')-backup.log

    Also change the first line to #!/bin/bash since $() is a bash construct, not sh, but you will get away with it on Linux because bash is the standard sh.

    Be careful with the % if you put this line in a crontab. See man crontab for why % is special.

  3. #3
    Join Date
    Jun 2008
    Location
    /earth/europe/uk/england/west-yorkshire/leeds/mystreet/myhouse
    Posts
    248

    Default Re: Creating a filename automatically"

    Wow, quick reply Ken, thanks!
    I'll give it a g now ...

    (by the way, this is not going in the crontab file, the crontab calls this file)

    badger_fruit@bgrsvr-y:~/bin> crontab -l
    # DO NOT EDIT THIS FILE - edit the master and reinstall.
    # (/tmp/crontab.XXXXEk688r installed on Sun Dec 21 12:12:33 2008)
    # (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
    30 * * * * /home/badger_fruit/bin/backup-script



    >>> EDIT >>>>
    Just made that change and it's worked 100% - nice one, thank you!!!!!

Posting Permissions

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