Copying files to multiple comp's on a lan

Emile van Sebille emile at fenx.com
Sat Jun 2 20:20:48 EDT 2001


Attached below is a script I put together that runs on a win2k box and backs
up the windows shares of other systems in the local network.  Perhaps
there's some ideas you can use.

HTH,

--

Emile van Sebille
emile at fenx.com

#!python
## netBackup.py

import os
import string

from win32pipe import popen3

from time import time, localtime

def targetSystems():
    "return list of active systems on the network"
    retval = []
    systemsList = popen3("net view")[1].readlines()
    for line in systemsList:
        i = string.split(line)
        if i:
            i = i[0]
            isServer = string.find(i,'\\\\') + 1
            if isServer:
                retval.append(i)
    return retval

def backupFolderList(targetSystem):
    " return list of shares on target system that should be backed up"
    retval = []
    cmd = "net view %s" % targetSystem
    shares = popen3(cmd)[1].readlines()
    for share in shares:
        shareInfo = string.split(share)
        try:
            shareName = string.join(shareInfo[0:shareInfo.index('Disk')],'
')
            if len(shareName) != 1:
                retval.append(shareName)
        except:
            pass
    return retval

def setBackupDestination():
    today = localtime(time())
    thisMonth = today[1]
    dayOfMonth = today[2]
    dayOfWeek = today[6]    # 0(Monday) - 6(Sunday)
    dayOfYear = today[7]
    if dayOfMonth == 5:
        backUpDrive = 'HI'[thisMonth % 2]
        backupDestinationFolder = 'Monthly'
    elif dayOfWeek == 6:    #It's Sunday
        backupDrive = 'HI'[dayOfYear % 2]
        backupDestinationFolder = 'Sunday'
    else:
        backupDrive = 'HI'[dayOfWeek % 2]
        backupDestinationFolder = ['MWF','TTS'][dayOfWeek % 2]
    return r'%s:\Backups\%s' % (backupDrive, backupDestinationFolder)

def backupFolder(target, folder, backupDestination, trace=1):
    "backup contents of folder on target"

    cmd = r'xcopy "%s\%s\*.*" "%s\%s\%s" /D /E /C /I /K /X /Y' % (target,
folder, backupDestination, string.split(target,"\\")[-1],folder)

    rslt = popen3(cmd)[1].readlines()
    if trace: print rslt

def excludeSystems():
    return [r'\\spam', r'\\parrot']

def backup():
    backupDestination = setBackupDestination()
    for target in targetSystems():
        if target not in excludeSystems():
            sysName = string.split(target,r"\\")[-1]
            print "\n\nBacking up %s " % target
            folders = backupFolderList(target)
            if folders:
                if not os.path.isdir(r'%s\%s' % (backupDestination,
sysName)):
                    os.mkdir(r'%s\%s' % (backupDestination, sysName))
                open(r'%s\%s\%s' % (backupDestination, sysName,
'BackupStart'),'w').write('Backup Start Time')
                for folder in folders:
                    print "   ", folder
                    backupFolder(target, folder, backupDestination)
                open(r'%s\%s\%s' % (backupDestination, sysName,
'BackupCompleted'),'w').write(string.join(folders, '\r\n'))

if __name__ == '__main__':
    backup()






More information about the Python-list mailing list