Showing posts with label wmi. Show all posts
Showing posts with label wmi. Show all posts

Monday, 18 June 2012

Python: Backup your data on the fly

I have been bitten before, and so to prevent any unforeseen loss of data, I set out to create something of the sort.

I did it.  And I am quite proud of it.  Even though its not perfect... the concept is pretty rad.

Things you will need.

  1. Python [Modules] - wmi, shutil, os, sys, winpaths(optional....)
  2. TaskScheduler
  3. USB or SD card, need to change its name to eBackup, instead of "Removable Disk"
The basic idea is to:
  • have a scheduled task run when the system is on idle...
  • only runs when a drive is present that is labeled eBackup
  • copy directories, specified in a cfg file.
Here is the code for this project:

'''
Created on Apr 30, 2012
@author: rcummins
'''
import wmi
import shutil
import os
import winpaths
import sys
def getDrive(computer=None):
    c = wmi.WMI(computer)
    host_info = c.Win32_LogicalDisk(VolumeName='EBACKUP')
    return host_info[0].Name
def fileChange(source):
    drive = getDrive()
    for dirs in os.listdir(source):
        path = source+'\\'+dirs
        dest = path.replace("C:\\", drive[0]+":\\")
        if os.path.getmtime(dest) - os.path.getmtime(path) < 0:
            shutil.copytree(path, dest)
if __name__ == '__main__':
    computer = ''
    logfile = ''
    x = getDrive(computer)
    try:
        logfile = str(sys.argv[1]).strip()
    except:
        logfile = 'eBrake.cfg'
    if os.path.exists(logfile):
        f = open(logfile,'r')
        for q in f.readlines():
            source = q.strip()
            for(path, dirs, files) in os.walk(source):
                for filez in files:
                    src = os.path.join(path, filez)
                    dst = str(os.path.join(path, filez)).replace("C:\\",x[0]+":\\")
                    if str(src).find(".metadata") < 0:
                        try:
                            if os.path.getmtime(dst) - os.path.getmtime(src) < 0:
                                print "Overwriting >> " + dst
                                shutil.copy(src, dst)
                        except:
                            print "Created: " + dst
                            if not os.path.exists(dst): os.makedirs(dst)
                            try:
                                shutil.copytree(src, dst)
                            except: shutil.copy(src, dst)
    else:              
        print "Create file: %s \n\n Enter paths to backup on SD card\n\ncard needs to be named 'EBACKUP'"%(logfile)
        pass                   
I hope this has either inspired you or saved your data.  Enjoy!

Tuesday, 12 June 2012

Python: Preventing an application from exiting.

I overheard somebody complaining that a program needs to be "Always open" and that the "Stinkin' Users" always close it causing issues down the line...

This can be really easy to fix... create a watcher with the WMI module for python and when the event is triggered, open the program again... then call the 'def' again recursively so the watcher program stays open all the time...  I don't think that's very clear to them, so I just said I could make something for them.

Heres what it looks like:

'''
Created on Feb 13, 2012
@author: rcummins
'''
import wmi
import subprocess
def ohnoyoudidnt():
    c = wmi.WMI()
    watcher = c.watch_for (
                               notification_type="Deletion",
                               wmi_class="Win32_Process",
                               delay_secs=1,
                               Name="application.exe"
                               )
    watcher ()
    exe_path = r'C:\Program Files\application.exe'
    subprocess.Popen(exe_path)
    ohnoyoudidnt()
if __name__ == '__main__':
    ohnoyoudidnt()
Now you need to get yourself the WMI Module here: http://timgolden.me.uk/python/wmi/index.html
It's a really awesome module and I have seen Tim Golden's posts around the web; very helpful.

the Subprocess module is standard so theres nothing to download there...

Hope it helps you out!