skip to main | skip to sidebar

Python Programs and Examples

Pages

  • Home
 
  • RSS
  • Facebook
  • Twitter
Tuesday, October 23, 2012

Fast and efficient Backup script that works on Windows and Linux

Posted by Raju Gupta at 4:55 AM – 7 comments
 
This python script backup your directory structure that works on Linux and Windows both. It directly uses the shell to do this task and shell commands are considered very fast compared to file operations does otherwise.

import os, os.path
import subprocess
import time
def backUpDir(path):
    """
    Creates backup of the passed old dir and creates a new dir. The backed up dir gets the
    date and time as the name of the new backed up file.
    On success, returns a list consising of two values:
        0: to signify the success
        None: means no error occurred.

    On error, return a list consisting of two values:
        -1 : to signify the failure
        error string: the exact error string
    """

    if os.path.exists(path) == True:
        #dir exists then backup old dir and create new
        backupDir = path + time.strftime('-%Y-%m-%d-%Hh%Mm%Ss')
        
        if os.name == "nt":
            #NT Sysyem  - used the DOS Command 'move' to rename the folder
            cmd = subprocess.Popen(["mov", path, backupDir], \
                                    shell = True, \
                                    stdout = subprocess.PIPE, \
                                    stdin = subprocess.PIPE, \
                                    stderr = subprocess.PIPE)
        elif os.name == "posix":
            #POSIX System - use the appropriate POSIX command to rename the folder.
            cmd=subprocess.Popen(["mv", path, backupDir], \
                                    shell = True, \
                                    stdout = subprocess.PIPE, \
                                    stdin = subprocess.PIPE, \
                                    stderr = subprocess.PIPE)
            pass
        else:
            # Not supported on other platforms
            return [-1, "Not supported on %s platform" %(os.name)]
        (out, err) = cmd.communicate()
        if len(err) != 0:
            return [-1, err]
        else:
            os.mkdir(path)
            return [0, None]
    else:
        #create new dir
        os.mkdir(path)
        return [0, None]

Email This BlogThis! Share to X Share to Facebook

7 Responses so far.

  1. Dipanwita says:
    August 12, 2018 at 11:30 PM

    Great job on the blog. Hope to see more such amazing posts. python training in Chennai

  2. pavithra dass says:
    August 23, 2019 at 9:05 AM

    Great post!
    Thanks for sharing this list!
    It helps me a lot finding a relevant blog in my niche!
    Java Training in Chennai
    Java Training in Coimbatore
    Java Training in Bangalore

  3. Anbarasan14 says:
    August 27, 2019 at 5:11 AM

    A big thanks for this sharing this awesome post with us. Read it recently and felt very happy on reading every bit of the blog.
    Spoken English Classes in Chennai
    Best Spoken English Classes in Chennai
    IELTS Coaching in Chennai
    IELTS Coaching Centre in Chennai
    English Speaking Classes in Mumbai
    English Speaking Course in Mumbai
    IELTS Classes in Mumbai
    IELTS Coaching in Mumbai
    IELTS Coaching in Anna Nagar
    Spoken English Class in Anna Nagar

  4. harish kalyan says:
    September 23, 2019 at 11:25 PM

    Excellent and very cool idea and great content of different kinds of the valuable information's.
    Data Analytics Courses in Chennai
    Data Analytics Courses
    TOEFL Training in Chennai
    Spoken English in Chennai
    Blockchain Training in Chennai
    spanish institute in chennai
    content writing training in chennai
    Data Analytics Courses in Tambaram
    Data Analytics Courses in Adyar

  5. rocky says:
    July 17, 2020 at 9:10 PM

    I have read some useful information in your blog.
    Python Training in Chennai

    Python Training in Training

    Python Training in Bangalore
    Python Hyderabad

    Python Training in Coimbatore

  6. Rupesh Kumar says:
    September 29, 2023 at 2:24 AM

    This is a good post and informative and very.thanks for sharing these information with all of us. Discover the ultimate solution for mastering English with Ziyyara Edutech's online home tuition for English. Our expert tutors offer personalized English tuition classes that cater to your unique learning needs.
    For more info visit English tuition classes

  7. Anonymous says:
    February 9, 2024 at 8:53 PM

    Thanks for the post. It was worth reading blog.
    Linux Course in Pune

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments (Atom)
  • Popular
  • Recent
  • Archives

Popular Posts

  • To Send the entire contents of directory as an email Message.
    Here is a Python Program to send the entire contents of a directory as an email message #!/usr/bin/env python """Send the...
  • Control Structures in Python
    This Program explains different control structures in python If ==== # basic "If" if boolean: x = 1 else: x = 0 # No...
  • Python Code for creating Screen saver
    This is the Python code for creating a screensaver of a moving ball. The ball will bee moving along the screen and when it hits any of the ...
  • Python script for walking the directory tree structure but excluding some directories and files
    The script walks the directory structure recursively. Some of the directories and files can be excluded from the walk by providing them in ...
  • XML to CSV Converter
    Python Program to extract each execution data (few children in xml) and create a csv file. def xml2csv(inputFile,outputFile,elemName,n...
  • HTML Email using Python
    We created a module called sendmail which email html content. Each of application dumps data from the datastructure into html format and th...
  • Overview of Python Programming Language
    Python is an easy to learn, powerful programming language. It has efficient high level data structures and a simple but effective approac...
  • Fast and efficient Backup script that works on Windows and Linux
    This python script backup your directory structure that works on Linux and Windows both. It directly uses the shell to do this task and she...
  • Run Application from host
    import os os.execl( "c:/Windows/Notepad.exe", "c:/userlog.txt") print "Running notepad" #or import subpro...
  • Orphan Checker
    The script does not use any outside libraries, thus keeping its execution simple. we will start by initializing the arrays that we'll...
Powered by Blogger.

Archives

  • ▼  2012 (66)
    • ▼  October (28)
      • Find text/pattern under parent folder from matched...
      • XML to CSV Converter
      • XSL to CSV converter
      • Spell Corrector in Python
      • Format a plain ASCII file using Python script
      • Control Structures in Python
      • Python script to zip and unzip files
      • Fast and efficient Backup script that works on Win...
      • File Splitter Script
      • Python script for walking the directory tree struc...
      • Python XML Parser
      • IP Address Generation
      • Orphan Checker
      • HTML Email using Python
      • Python interface to load commands.
      • Python script to Validate Tabular format Data and ...
      • Python script to Load Configuration file content i...
      • Python Script to search file for specific content
      • Network Wise File Concatenation
      • Python Script for Adding or Subtracting the Dates
      • Program to create make file in VMS/UNIX
      • Python Code for creating Screen saver
      • Run Application from host
      • Multithreading in Python
      • This utility determines the size of a folder and i...
      • Using UNIX Environment Variables From Python
      • CVS Macro for Commited Files
      • Generating unique id's
    • ►  September (36)
    • ►  March (1)
    • ►  February (1)
 

Labels

  • Command Execution Using Python (2)
  • Control Structure (1)
  • CSV Examples (4)
  • Database (3)
  • Features of Python Programming Language (1)
  • File Example (2)
  • IF Statement (1)
  • List (2)
  • Mail (5)
  • Multithreading (1)
  • MySQL (3)
  • Python Date Example (2)
  • Python Example (2)
  • Python File Example (7)
  • Python Introduction (1)
  • Python Network Example (1)
  • Python XML Parser (2)
  • Searching Example (2)
  • Simple Calculation Program (1)
  • Strings (4)
  • Zip File Example (1)

Followers

 
 
© 2011 Python Programs and Examples | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger