Search code examples
pythoncentos7glob

Read creation file date from other users home folders with Python2.7 centOS


I'm working on a script that will check when the latest backup have been taken by looking at the files creation date.

The files are located on a centOS7 machine in different users home folders.

I'm pretty sure that this has to do with the privileges but everything that I've tried have failed so far.

This is my code

#!/usr/bin/env python2.7

import os
import time
import glob
from datetime import date, datetime

date_format = "%m/%d/%Y"

def checkBackup (location, fileName, interval):
    newest = max(glob.iglob(fileName), key=os.path.getctime)
    created = time.strftime('%D', time.localtime(int(os.path.getctime(location+newest))))
    cDate = datetime.strptime(created[:6] + '20' + created[6:], date_format)
    localTime = time.strftime('%D')
    cTime = datetime.strptime(localTime[:6] + '20' + localTime[6:], date_format)
    delta = cTime-cDate

    if delta.days > interval:
            file.write("Missing backup for " + newest + "    ERROR!\n")
    else:
            file.write(newest + " SUCCESSFUL BACKUP\n")
            return;

file = open("backupStatus.txt","w")
checkBackup("/home/user1/","backupFile.gz",30);
checkBackup("/home/user2/","backupFile.gz",30);
file.close()

When running this script as user1 I get a new entry in the backupStatus.txt file, but when trying to check the status of the file in the user2's home folder I'm getting and error:

Traceback (most recent call last):
File "checkBackup.py", line 26, in <module>
checkBackup("/home/user2/","backupFile.gz",30);
File "checkBackup.py", line 11, in checkBackup
newest = max(glob.iglob(fileName), key=os.path.getctime)
ValueError: max() arg is an empty sequence

I've added the user1 to the user2 group and thought that this would solve the problem but I'm still getting this error, even when running this as root I'm getting this.

This is my first post in stackoverflow so I hope that I've written this question in the correct way. It's also been a few years since I've done anything with python so feel free to give me tips on how to improve the code!

So, Does anyone have any idea on how I can solve this problem?

Regards

Anton


Solution

  • The following code works. The reason that it didn't work before was because i only had the glob checking the fileName and not the correct path to it, so it only worked for the files in the same directory.

    With help from @cdarke I added the files path to the glob and could successfully get the information from other users /home folders, but after that the next line had an incorrect path (added the path twice to the string), by removing that everything worked as intended.

    #!/usr/bin/env python2.7
    
    import os
    import time
    import glob
    from datetime import date, datetime
    
    date_format = "%m/%d/%Y"
    
    def checkBackup (location, fileName, interval):
        newest = max(glob.iglob(location + fileName), key=os.path.getctime)
        created = time.strftime('%D', time.localtime(int(os.path.getctime(newest))))
        cDate = datetime.strptime(created[:6] + '20' + created[6:], date_format)
        localTime = time.strftime('%D')
        cTime = datetime.strptime(localTime[:6] + '20' + localTime[6:], date_format)
        delta = cTime-cDate
    
        if delta.days > interval:
                file.write("Missing backup for " + newest + "    ERROR!\n")
        else:
                return;
    
    file = open("backupStatus.txt","w")
    checkBackup("/home/user1/","backupFile.gz",30);
    checkBackup("/home/user2/","backupFile.gz",30);
    file.close()