Welcome, guest | Sign In | My Account | Store | Cart

Copies files and directories periodically to a time-stamaped directory. Used to create a running record of digital files to document change over time.

Python, 51 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#WI Rapids DNR Fire Situations Unit backup script
#To be executed by double-clicking the file in the incident folder on Buffy
#(Buffy is a TeraStation portable file server)
#Will endlessly repeat, kill it by closing the window
#Tyler Grosshuesch - tyler.grosshuesch@co.adams.wi.us
#2011-04-13

import time, os, fnmatch, shutil

#Endless loop
while 1:
    #set up timestamp
    t = time.localtime()
    timestamp = time.strftime('%b-%d-%Y_%H%M', t)

    #buffy backup directory with timestamp
    bu_dir = os.path.join(r'.\Folder_Backup', timestamp)

    #set up local backup directory
    proj_dir = os.getcwd()
    local_dir = os.path.join(r'C:\Share', os.path.basename(proj_dir))

    #set up mkdir Folder_Backup\timestamp command
    mkdir_cmd = r'mkdir ' + bu_dir
    #execute mkdir Folder_Backup\timestamp
    os.system(mkdir_cmd)

    #Copy files to Folder_Backup\timestamp directory on Buffy
    for d in os.listdir(proj_dir):
        if not fnmatch.fnmatch(d, '*Folder_Backup*'): #don't backup Folder_Backup
            #backup directories and subdirectories
            if os.path.isdir(d):
                print 'backing up ' + d
                shutil.copytree(d, os.path.join(bu_dir, d))
            #backup loose files in the project directory
            else:
                print 'backing up ' + d
                shutil.copy(d, os.path.join(bu_dir, d))

    #copy shell command - entire project folder to ranger mapper's local
    copy_c_cmd = r'xcopy . ' + local_dir + ' /D /E /Y /C'
    
    #execute shell commands
    mkdir_c_cmd = 'mkdir ' + local_dir
    if not os.path.exists(local_dir):
        os.system(mkdir_c_cmd)
    os.system(copy_c_cmd)

    #timer - repeats every 10 min
    print 'waiting to copy backup files...'    
    time.sleep(600) #units = seconds (10min = 600s, 30min = 1800s)

This was developed to document changes over time, primarily for maps and other documents made during a disaster response by an incident management team. In our case, there is an incident directory with standard subdirectories and files on a portable file server. Directory and file structure follows the Fire Incident Management Tools structure pretty closely. I'm not an experienced programmer, so any advice on how to make the script more resilient or on better ways to do things would be appreciated. This was developed during the response to a tornado in Adams County, WI earlier this week (April 10-13, 2011) in cooperation with WI DNR.