Simple homemade data backup (Python + DropBox)

    I did not do backups for a long time. It was laziness, and I have not lost the data for several years. But recently I thought about it and decided to do something that would reserve something that I don’t really want to lose. Just that would not be boring, and a little practice.

    First you need to decide where to store copies of the files. In my opinion DropBox is a very good choice - it is a service for synchronizing data on different computers. They give 2 GB for free, plus a program that displays the selected local folder on their online storage. Added a file to this special folder - it was added on all computers on which there is a special program configured for the same account. Plus access via the web interface. This is very convenient for backup - no need to worry about how to copy files to FTP, but simply copy them to a special folder. All the fill / sync work will be done by DropBox client.

    Next, you need to actually copy the necessary files. To do this, a tiny python script was written, backup.py. This script creates a text list of files to be copied, and passes it to WinRar, which creates an archive with the specified name.

    At first I wanted to completely instruct WinRar to create a backup by writing some code in batch files, but unfortunately he is not able to skip the specified folders (only files). Therefore, I had to write an intermediate script that creates a list of files. Well and good, it's even more fun.

    The script has a single parameter - the name of the output archive file with the copied files.

    The algorithm of the script:
    1. It reads the tobackup.lst file from the current folder - it contains a list of folders to be copied. Each line is a separate folder. For example:
      d: \ projects
      d: \ www
    2. Reads a list of folders to be excluded from backup. This is the optional igonre.lst file in the current folder. This is either the full path (d: \ projects \ old), or simply the name of the folder (.svn). For example:
      .svn
      d: \ projects \ old
    3. Creates a text list of files to copy list.lst (in the current folder). After that, it adds the optional extra.lst file (from the current folder) to the end of the list file . It contains a list of files, not folders, to include. For example, we want to save php settings, but from the whole d: \ programs \ php folder we need only one php.ini file. Therefore, instead of adding a folder with php, we add only one php.ini file to extra.lst.
    4. Calls the archiver, which creates an archive with files from the list. Archiver options - save the full path of files (along with the disk) and create an archive without compression.

    As a result, after the script runs, we will have an archive with the specified folders.

    It was not for nothing that I pointed out that the script takes files from the current folder. Thanks to this, it is very simple to make different “profiles” - just create a new folder, create the tobackup.lst file there, the optional ignore.lst and extra.lst, and the new profile is ready! For convenience, you can make a batch file that will call backup.py and pass it the name of the archive file that should turn out.

    Now, for example, I have two profile folders, projects (for backup of current projects) and other (for backup of program settings). The script itself lies in the core folder (at the same level as the profile folders), along with WinRar.

    Folder core:
    Rar.exe
    WinRAR.exe
    rarreg.key
    backup.py

    Folder projects:
    tobackup.lst:
    d: \ projects
    d: \ svn

    ignore.lst:
    .svn
    d: \ projects \ old

    backup.bat:
    .. \ core \ backup.py "d: \ dropbox \ my dropbox \ backup \ dev.rar »The

    batch file backup.bat calls the script, passes it the name of the resulting archive file, which will be created in the folder associated with DropBox. The script takes the list files from the current folder (projects in this case). One launch of the batch file - the new backup is ready, and DropBox uploads it to the server.

    It remains to tighten the launch on a schedule, but I’m happy with a manual launch several times a month.

    backup.py:
    Copy Source | Copy HTML
    1. import subprocess
    2. import sys
    3. import os.path
    4. import os
    5.  
    6. if __name__ == "__main__":
    7.     if (len(sys.argv) < 2):
    8.         print("Usage: backup.py ")
    9.         print("E.g.: \"backup.py d:\\dropbox\\my dropbox\\backup.rar\"")
    10.         exit(1)
    11.  
    12.     scriptFolder = os.path.dirname(sys.argv[ 0])
    13.     ArchiverFile = scriptFolder + "\\winrar.exe"
    14.     RootFoldersFile = "tobackup.lst"
    15.     IgnoreFoldersFile = "ignore.lst"
    16.     ExtraFile = "extra.lst"
    17.     FilelistFile = "list.lst"
    18.     OutputArchive = sys.argv[1]
    19.  
    20.     # Only mandatory file is RootFoldersFile, check that it exists
    21.     if (not os.path.isfile(RootFoldersFile)):
    22.         print("%s doesn't exist" % RootFoldersFile)
    23.         exit(1)
    24.  
    25.     # Archiver file also should exist
    26.     if (not os.path.isfile(ArchiverFile)):
    27.         print("%s doesn't exist" % ArchiverFile)
    28.         exit(1)
    29.  
    30.     # Read root folders from file
    31.     rootFolders = [i.strip() for i in open(RootFoldersFile, "r").readlines()]
    32.  
    33.     # Read list of folders that need to be igonred (if specified)
    34.     ignoreList = []
    35.     if (os.path.isfile(IgnoreFoldersFile)):
    36.         ignoreList = [i.strip().lower() for i in open(IgnoreFoldersFile, "r").readlines()]
    37.  
    38.     # Open filelist file for writing list of files
    39.     out = open(FilelistFile, "w")
    40.     filesCount =  0
    41.  
    42.     for rootFolder in rootFolders:
    43.         for root, dirs, files in os.walk(rootFolder):
    44.             for file in files:
    45.                 out.write(root + "\\" + file + "\n")
                      filesCount += 1

                  for dir in dirs:
                      # Skip ignored folders
                      if (dir.lower() in ignoreList or ("%s\%s" % (root, dir)).lower() in ignoreList):
                          dirs.remove(dir)

          # Append some files from extra files list
          if (os.path.isfile(ExtraFile)):
              out.writelines(open(ExtraFile, "r").readlines())
              
          out.close()

          print("Added %d file(s)" % (filesCount))

          # Delete old archive if exists
          if (os.path.isfile(OutputArchive)):
              os.unlink(OutputArchive)

          # Call archiver (winrar)
          subprocess.call(ArchiverFile + " a -m0 -ep3 \"" + OutputArchive + "\" @list.lst")
    46.  
    47.     os.unlink(FilelistFile)
    48.  
    49.     print("Done")


    UPD The new version is here .

    Also popular now: