Advertisement
DelphyM

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

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