1

I wrote the script below which checks the specified path PARENT_DIR and finds the recently modified directories (+subdirectories) then makes a back up of them into MOVE_DIR

The problem I'm struggling with is that, it seems this script, somehow, doesn't check the mtime of the directories and copies the whole content within the parent directory to the destination address. what am I doing wrong here?

import os
import os.path
import datetime
from shutil import copytree
from shutil import move
from time import time
os.chdir("/home/sina/Desktop/incoming")

def mins_since_mod(fname):
    """Return time from last modification in minutes"""
    return (time() - os.path.getmtime(fname)) / 60
PARENT_DIR = '/home/sina/Desktop/incoming'
MOVE_DIR = '/home/sina/Desktop/incoming_New'

# Loop over files in PARENT_DIR
for fname in os.listdir(PARENT_DIR):
    # If the file is a directory and was modified in last 10 days
        if ((os.path.isdir(fname)) and (mins_since_mod(fname) < 14400)):
           copytree(fname, MOVE_DIR) # move it to a new location

if I want to make the question more clear, you can consider the path below with folowing subdirectoris:

 /home/sina/Desktop/incoming/A/A1.1
 /home/sina/Desktop/incoming/A/A2.2
 ...

the subdirectories within ../A such as A2.2 or A1.1 are being modified by another app (this app may add even new subdirectories) and I'd like to have a back up of them every 10 days at this path /home/sina/Desktop/incoming_New/A/{subdirectories}

Any kind of help is appreciated.

Sina Sh
  • 1,177
  • 3
  • 15
  • 24
  • Using `crontab` to run backup command every 10 days? – Eric Jan 28 '16 at 10:05
  • What are the return values of time() and getmtime(fname)? And are you sure the recorded modification time of your directories, as obtained by e.g. the ls command, is what you expect? Something in your testing procedures might be modifying the directories inadvertently – Lorenzo Gatti Jan 28 '16 at 10:06
  • Well, I'd prefer it to be done with a python script, because this step is only a part of a bigger project which will populate the backed up directories to feed another python apps. – Sina Sh Jan 28 '16 at 10:09
  • @LorenzoGatti , time() is the current machine time and getmtime(fname) will get the last modified time of fname (in this case is a directory), you can check that time with ls -la (or if you run that line on python shell will see the result for a specific file or folder) – Sina Sh Jan 28 '16 at 10:11
  • in Linux when you change file then directory mtime is chaged too, Because directory is like a file with information about mtime of files in this directory. – furas Jan 28 '16 at 10:17
  • I think @LorenzoGatti was just saying that it seems like you could easily check whether `mins_since_mod()` is computing reasonable values for a given set of input directories. I'd like to add it would also be easy to determine whether the `mtime` of directories is being modified the way you are assuming. – martineau Jan 28 '16 at 11:51

0 Answers0