⬆️ ⬇️

Simple homemade data backup 2 (Python + DropBox)

New version of the script for backup ( original ). Or another version ...



The logic of working with file lists has not changed. Only the script parameter has changed - now this is the name of the archive without an extension, the script itself will add .tar.gz .



New:



In addition to the pleasant new, an unpleasant minus poked in - the script began to eat a lot of memory.

')

Make a compressed archive using the tarfile module failed. The archive is created and compressed, but for some reason the paths of the archive are moving down.



Those. you need to create an archive d:\dropbox\my dropbox\backup\backup.tar.gz , it appears there. But if you open this gz archive, the tar archive will be deep inside the dropbox\my dropbox\backup\backup.tar . Those. the full path to the data is: d:\dropbox\my dropbox\backup\backup.tar.gz\dropbox\my dropbox\backup\backup.tar\... In principle, you can live with this, but ugly ...



Therefore, I did it differently - first a tar archive is created without compression with all the files, then it is compressed. It is in this place of the function that compresses the archive, it, the archive, is transferred entirely, i.e. the entire file weighing several hundred MB is sucked into the memory.



It would be necessary to dig in this direction and find the best solution ...



Copy Source | Copy HTML
  1. import sys
  2. import os
  3. import os .path
  4. import tarfile
  5. import time
  6. import gzip
  7. if __name__ == "__main__" :
  8. if ( len ( sys .argv) < 2 ):
  9. print ( "Usage: backup.py <output archive filename>" )
  10. print ( "Eg: \" backup.py d: \\ dropbox \\ my dropbox \\ backup \ "" )
  11. exit ( 1 )
  12. RootFoldersFile = "tobackup.lst"
  13. IgnoreFoldersFile = "ignore.lst"
  14. ExtraFile = "extra.lst"
  15. OutputArchive = sys .argv [ 1 ]
  16. OutputArchiveTar = OutputArchive + ".tar"
  17. OutputArchiveTarGz = OutputArchive + ".tar.gz"
  18. # Only mandatory file is RootFoldersFile, check that it exists
  19. if ( not os .path.isfile (RootFoldersFile)):
  20. print ( "% s doesn't exist" % RootFoldersFile)
  21. exit ( 1 )
  22. # Read root folders from file
  23. rootFolders = [i.strip () for i in open (RootFoldersFile, "r" ). readlines ()]
  24. # Read list of folders that need to be igonred (if specified)
  25. ignoreList = []
  26. if ( os .path.isfile (IgnoreFoldersFile)):
  27. ignoreList = [i.strip (). lower () for i in open (IgnoreFoldersFile, "r" ). readlines ()]
  28. # Delete old archive if exists
  29. if ( os .path.isfile (OutputArchiveTar)):
  30. os .unlink (OutputArchiveTar)
  31. if ( os .path.isfile (OutputArchiveTarGz)):
  32. os .unlink (OutputArchiveTarGz)
  33. filesCount = 0
  34. # Open output archive for writing (gzip it later)
  35. try :
  36. tar = tarfile . open (OutputArchiveTar, "w" )
  37. except IOError as err:
  38. print (err)
  39. exit ( 1 )
  40. lastTime = time . time ()
  41. for rootFolder in rootFolders:
  42. for root, dirs, files in os .walk (rootFolder):
  43. for file in files:
  44. tar.add ( os .path.join (root, file))
  45. filesCount + = 1
  46. # Show number of processed files each second
  47. if ( time . time () - lastTime> = 1 ):
  48. print ( "% d files ..." % (filesCount))
  49. lastTime = time . time ()
  50. # Skip ignored folders
  51. for dir in dirs:
  52. if ( dir .lower () in ignoreList or ( os .path.join (root, dir) .lower () in ignoreList)):
  53. dirs.remove (dir)
  54. # Add some files from extra files list
  55. if ( os .path.isfile (ExtraFile)):
  56. for file in [i.strip () for i in open (ExtraFile, "r" ) .readlines ()]:
  57. tar.add (file)
  58. tar.close ()
  59. print ( "Compressing ..." )
  60. # Gzip output archive
  61. try :
  62. gz = gzip open (OutputArchiveTarGz, "wb" )
  63. except IOError as err:
  64. print (err)
  65. exit ( 1 )
  66. gz.write ( open (OutputArchiveTar, "rb" ) .read ())
  67. gz.close ()
  68. os .unlink (OutputArchiveTar)
  69. print ( "Output:% s,% d file (s),% d bytes" % (OutputArchiveTarGz, filesCount, os .path.getsize (OutputArchiveTarGz)))
  70. print ( "Done" )

Source: https://habr.com/ru/post/69856/



All Articles