• Linux
  • March 2019
    M T W T F S S
    « Feb   Apr »
     123
    45678910
    11121314151617
    18192021222324
    25262728293031
  • Meta

  • Follow me on Twitter

  • Advertisements

Archive-Bank Update

As I mentioned in Another cleanup script, I talked about writing a script to archive the backup files created by HomeBank. I’ve been working on it and improving it regularly over the past month.

Basically, the script sets up a temporary reference file with a timestamp set to midnight on the first day of the month and moves the backup files older than that to a backup folder, then places them into a dated zipped archive. (See the comments on the post for more detail).

Today, I added code to check to if the date is the last day of the month, which is the day I usually want to run it to archive the previous month’s backups. It it’s not the last day of the month, it prints a warning and asks me if I still want to run it. This feature will come in handy if I know I won’t be able to run it on the last day of the month for some reason.

Running the script on the first of the month will archive the previous month along with the month before that. The idea behind the script is to run it on the last day of the month to archive files from the previous month. For instance, I ran it on February 28 to archive my January backup files. Had I run it on March 1, I would have archived both January and February because the reference file would have been dated 1903010001.

Maybe I can set up the tests to look at a range of dates when it would be safe to run, maybe a five day window. However, if I were to accidentally archive the wrong month, I could easily extract the files back into the directory.

By the way, I came up with the idea and worked out most of the code whilst listening to my granddaughter’s high school band concert. Maybe it was the music that put me in the right state of mind and started the creative juices flowing.

Advertisements

5 Responses

  1. I changed my mind about hard coding a range of acceptable dates to archive the files since I’ve already provide a mechanism to force running the script on an earlier date. However, I could insert a check that would cancel out that mechanism if the date were before a certain day of the month. It’s something to consider.

  2. I inserted an if statement to override the choice to run the script before the last day of the month if the dates is earlier than the 21st. I picked that date because I think any time before the third week of the month is too early to archive the backup files.

    Currently, half of the script involves checking the date and determining if the main portion of the script can be run. Am I making it to complicated?

  3. Yesterday, I came across an article on using the trap Bash builtin command to clean up files, restarting services, or opening/closing ports when a script exits. The archive-bank script immediately came to mind since it creates a temporary file which is deleted once it’s been used.

    This morning I took a look at the script and decided that using trap really wasn’t necessary or even all the useful. The temporary file is only used as a timestamp reference, no data is written to it so if the script did exit unexpectedly, no harm would be done.

    Even though I don’t currently have any scripts that can make good use the trap command, I’ll keep my notes in my toolkit.

  4. I made several more changes to the Archive-Bank script today which I hope will improve the script.

    First, I streamlined the process to determine whether or not the script will actually archive the files if it’s run before the last day of the month. I reduced the number of if statements to an if/elif. The script now checks to see if the runit variable has not been changed and if the current date is earlier than the 21st. If both conditions are true, the script displays a message stating that it’s too early to archive the files and exits. If the current date is the 21st or later, it will ask if you want to run the script and either run the rest of the script or exit depending on the user’s response.

    In the case statement where I determine if it’s the last day of the month, I made months with 31 days the default condition since there are seven such months. That’s seven strings that don’t have to be checked. It turns out that I had previously forgotten in include May.

    Finally, after the script had moved the previous months *.bak files to the backup folder, I put in a check to see if any files had actually been moved there before creating the zipped archive. This covers cases where the script may have been successfully run earlier.

    I also moved some of the variable assignments so they were more logically placed. I didn’t see much point in creating a variable to name the archive file if the file wasn’t going to be created. It also made sense to create variables just before they were to be used.

    To paraphrase the French poet, Paul Valery, “A script is never finished, only abandoned.” That applies to programs in general, as well as poetry.

  5. […] working on my script to creat monthly archives my HomeBank backup files throughout the month. (See Archive-Bank Update). The script has grown quite a bit from its original incarnation and become a bit more […]

Comments are closed.

%d bloggers like this: