Skip to content

Instantly share code, notes, and snippets.

@intellent
Created March 17, 2023 14:59
Show Gist options
  • Save intellent/bee4c26e4f16410fa275ff4c6904f782 to your computer and use it in GitHub Desktop.
Save intellent/bee4c26e4f16410fa275ff4c6904f782 to your computer and use it in GitHub Desktop.
The Docker Volume Manager (dvm) helps with backing up and restoring Docker volumes.
#!/usr/bin/env python3
import os, sys
def main(operation, volumeName):
match operation:
case 'backup':
backup(volumeName)
case 'restore':
restore(volumeName)
case 'inspect':
inspect(volumeName)
case _:
print('Invalid action. Use either backup, restore or inspect.')
exit(1)
def backup(volumeName):
archiveName = getArchiveName(volumeName)
print('Backing up', volumeName, 'to', archiveName)
os.system('docker run --rm -v %s:/volume -v $(pwd):/backup alpine tar czf /backup/%s -C /volume .' % (volumeName, archiveName))
print('Done. ', archiveName, 'created.')
def restore(archiveName):
volumeName = getVolumeName(archiveName)
print('Restoring', volumeName, 'from', archiveName)
os.system('docker run --rm -v %s:/volume -v $(pwd):/backup alpine tar xzf /backup/%s -C /volume' % (volumeName, archiveName))
print('Done.', volumeName, 'created.')
def inspect(volumeName):
print('Inspecting', volumeName)
os.system('docker run -it --rm -w /volume -v %s:/volume alpine /bin/sh' % volumeName)
def getArchiveName(volumeName):
return '%s.tar.gz' % volumeName
def getVolumeName(archiveName):
return archiveName.removesuffix('.tar.gz')
if (len(sys.argv) != 3):
print('Usage:')
print('./dvm.py backup <volume name>')
print('./dvm.py restore <archive name>')
print('./dvm.py inspect <volume name>')
exit(1)
if (__name__ == '__main__'):
main(sys.argv[1], sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment