Be aware this is going to be ugly because I have never written anything in python and am attempting to quickly pick up just enough to solve my problem. I need a way to automate doing about 2 weeks of daily archives of a backup file. I think I have the logic down and have hacked together some code from the help files and some web searches, but I am sure it is not quite right yet. I am still hacking away at it but I figured it was worth asking here for some help too. Here is what I have so far:

#!/usr/bin/env python
import commands,shutil

#get the day of the month as an int in the range of 1-31 using a bash command to use in filenames
day=int(commands.getoutput('date "+%d"'))

#drop the latter half of the month by 15 since I only need 2 weeks of backups
if [ "$day" > "15" ]; then
day -= 15

#create my source and destination filenames using the day number to cycle the files
sfn = r"/volumes/Storage/MediaNet/*saveset.bck"
dfn = r"/volumes/Storage/MediaNet/.bck/" + str(day) + r"backup.bck"

#copy the file into the backup directory
shutil.copy(sfn, dfn)

I am sure my syntax is off and if anyone knows a better way to do the same thing I would gratefully accept any input. Thanks for taking the time to help.

Your code will only run on linux. It looks more like a bash script then a python code.

I just don't get if they are multiple files to copy and if it is to copy every day
or id it only 15 of all the backups that there exist.

The file "saveset.bck" is a backup file created by another program which overwrites itself daily, making it pretty useless. My objective is to make copies of this file daily after each backup to a sub directory, "/.bck/" with about a week worth of daily copies; in my attempt above these would be named 1backup.bck - 16backup.bck, in theory at least. Once it gets to 15 or 16 (depending on the month) it should start back over at 1 and overwrite the oldest save file. I would have written a bash script to do this if I could have figured out a way to do so. It is likely there is and I just missed it.

If you have a suggestion on how I can do this better, please let me know. My knowledge is fairly limited and I can use all the help I can get. :)

For the day modulo 15 you can use:

## get 0..15 int from current day
from datetime import date
print( % 16)

So something like this (if you have pre-existing bck directory in place before hand):

from datetime import date
import os
import shutil

## get 0..15 int from current day
day = ( % 16)

path = r"/volumes/Storage/MediaNet"
sfn = "saveset.bck"
dfn = "%sbackup.bck" % day

print 'Copying',sfn,'to',dfn

#copy the file into the backup directory
print 'Saved backups are:',', '.join(os.listdir(os.path.join(path,'bck')))

A simple example. It's only a example, not a finnished code, but you can try to add to the concept of Tony and we'll see where you get. ;)

f_backup = path_to_file
basename, ext = os.splitext(f_backup)
copy_path = path_to_copy

counter =0
while running:

    if copied == None and os.path.isfile(f_backup) == 1:
        counter += 1
        os.copy(f_backup, copy_path + basename + counter + ext)
        copied = True

    if copied == True and os.path.isfile(f_backup) == 1:
        copied = None

A simple example. It's only a example, not a finnished code, but you can try to add to the concept of Tony and we'll see where you get. ;)

Sorry, not my concept, but concept of posters original " ' Python code ' "

Would probably make sense also to put

from time import sleep ## first lines
### end of script
sleep(24*60*60) # daily

And indent the code in endless while True: loop.

EDIT: The datetime code is the lines until line 10 in original post.

I was talking about the datetime code Tony.

When I started writing you didn't had edited your post yet.

Wow. Excellent. I can not thank you both enough for the incredibly helpful and quick replies. I think it is working!

I didn't add in the sleep code, I was planning on using crontab to schedule the daily executions. Is there any reason to use one over the other?

Just one question on how it is working though: How does the " % 16" work? It appears to give me the right number, but I don't understand how. I am looking it up right now on the python site as well to make sure I understand it.

Thank you both again.

EDIT: I found it and I get it now. That is slick and works well, much better than the cumbersome way I was trying to do it. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.