Advertisement
DelphyM

Updated version of: VMware ESXi fastcopy.py to quickly copy

May 6th, 2015
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | None | 0 0
  1. #!/bin/python
  2. # fastcopy.py
  3. # Copy VMware virtual machine directories using cp and vmkfstools.
  4. # (c) alain.spineux@gmail.com
  5. # released under GPL
  6. # usage: fastcopy.py vm_directories_path target_dir
  7. # The original script can be retrieved from the author's blog:
  8. # http://magiksys.blogspot.co.nz/2012/09/vmware-esxi-fastcopypy-to-copy-vms.html
  9.  
  10. #
  11. # vm_directories_path - the directory where VMs reside.
  12. # target_dir - the directory to copy all VMs to.
  13. #
  14. # If a corresponding target directory, e.g. target_dir/vm1, exists it will be moved to
  15. # target_dir/vm1.old and a new copy will be created in its place. After the VM is copied
  16. # the vm1.old will be removed.
  17. #
  18. # ATTN: don't copy recursively
  19.  
  20. import sys, os, subprocess, re, shutil
  21.  
  22. def copy(srcdir, dstdir):
  23.     skipped=0
  24.     temp_backup=dstdir+'.old'
  25.  
  26.     # If temporary backup already exists (meaning the last backup failed) leave it alone
  27.     # and try to remove the failed one.
  28.     # Else rename target directory for temporary backup.
  29.     if os.path.exists(temp_backup):
  30.         if os.path.exists(dstdir):
  31.             shutil.rmtree(dstdir)
  32.     else:
  33.         if os.path.exists(dstdir):
  34.             os.rename(dstdir, temp_backup)
  35.  
  36.     os.mkdir(dstdir)
  37.  
  38.     for filename in os.listdir(srcdir):
  39.         fullfilename=os.path.join(srcdir, filename)
  40.         if os.path.isdir(fullfilename):
  41.             print fullfilename, 'SKIPPED'
  42.             skipped+=1
  43.             continue
  44.         print filename
  45.         if filename.endswith('-flat.vmdk'):
  46.             # this is the data file, it will be copied by the .vmdk
  47.             continue
  48.         if re.match('.*-s[0-9]{3}.vmdk$', filename):
  49.             # this is part of a sparse file, it will be copied by the .vmdk
  50.             continue
  51.  
  52.         # dont use vmkfstools for snapshot files
  53.         if filename.endswith('.vmdk') and \
  54.             not re.match('.*-[0-9]{6}-delta.vmdk$', filename) and \
  55.             not re.match('.*-[0-9]{6}.vmdk$', filename):
  56.             args=['vmkfstools', '-i', fullfilename, '-d', 'thin', os.path.join(dstdir, filename) ]
  57.         else:
  58.             args=['cp', fullfilename, os.path.join(dstdir, filename) ]
  59.         # print args
  60.         subprocess.call(args)
  61.  
  62.     # Remove the temporary backup.
  63.     if os.path.exists(temp_backup):
  64.         shutil.rmtree(temp_backup)
  65.     return skipped
  66.  
  67. if len(sys.argv)<3:
  68.     print 'Usage: fastcopy.py vm_directories_path target_dir'
  69.     sys.exit(1)
  70.  
  71. dstdir=sys.argv[2].rstrip('/')
  72.  
  73. if not os.path.isdir(dstdir):
  74.     print 'target_dir must be a directory'
  75.     sys.exit(1)
  76.  
  77. if not os.path.isdir(sys.argv[1]):
  78.     print 'vm_directories_path must be a directory'
  79.     sys.exit(1)
  80.  
  81. # Compile a list of source directories
  82. srcdirs = []
  83. for srcdir in os.listdir(sys.argv[1]):
  84.     if os.path.isdir(srcdir):
  85.         srcdirs.append(srcdir.rstrip('/'))
  86.  
  87. skipped=0
  88. for srcdir in srcdirs:
  89.     targetdir=os.path.join(dstdir, os.path.basename(srcdir))
  90.     skipped+=copy(srcdir, targetdir)
  91.  
  92. if skipped>0:
  93.     print "SKIPPED:", skipped
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement