Backup file code help?

I am trying to complete a Python script which backs up files, from one server to another, stopping the services while it runs. The code (as it stands) is below, with the requirements. I think I've got the logic down, but I'm still have major probs with syntax and errors. I'm trying to use the logging module, which I've never done before... Any help?


import arcpy # Import arcpy module
from arcpy import env # Import env module from arcpy
import os # Import os module
import logging # Import logging module
import shutil # Import shutil module

inFile = "G:data" # Input specified
outFile = "D:data" # Output specified
CONST backup = "D:ackup" # Intermediate backup file
CONST logdirectory = "S:ackup_directorylog"
log = logging.getLogger("G:data") # Creates logger instance
hdlr = logging.FileHandler("G:data.log") # Creates FileHandler
formatter = logging.Formatter("%(asctime)s %(levelName)s %(message)s") # Creates Formatter
hdlr.setFormatter(formatter) # Attaches Formatter to

FileHandler
logger.addHandler(hdlr) # Attaches FileHandler to Logger
logger.setLevel(logging.NOTE) # Sets logger note

inFile = open("G:data", "r") # Opens the data file to be read,

and assigns it to the variable, inFile
outFile = open("D:data", "w") # Opens a data output file to be

written to, and assigns it to outFile

shutil.copyfile(logger, D:datacopy_[fileDate].log) # Copy logfile to archive as

"copy_[filedate].log"

for .log in logdirectory # For loop to check current date,

and returns files 7 days old or less
return(currDate - fileDate) <= 7
logger.note("Log files updated")

for inLine in inFile: # same as file.readline() - reads

one line at a time into a string variable, loop to read, stop service, write, start service.
service, srcFile, destFile = inLine.split(",") # Split each line, while removing

the comma, and store each field

if os.path.exists("srcFile"): # If source exists, for each

service in the array, stop the service
for service in inFile[]:
os.system("AGSSOM.EXE -x")

if os.path.exists("destFile"): # If destination variable exists,

copy destination to backup, and remove destination
#arcpy.env.overwriteOutput = True (needed with shutil?)
shutil.copyfile("G:data*", "D:ackup*")
os.remove("destFile")
else
logger.note("Filename did not exist prior to copy")
end if

shutil.copyfile("srcFile", "destFile") # Copy source to destination

if not os.path.exists("destFile"): # If destination does not exist,

copy backup to destination, and add logger note
shutil.copyfile("D:ackup*", "D:data*")
logger.note("[filename] copied from backup")
end if

for service in inFile[]: # For each service in the array,

start the service
os.system("AGSSOM.EXE -s")

else # If the source is not found, add

logger note
logger.note("Source not found")
end if

inFile.close() # Close input file
outFile.close() # Close output file
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories