Skip to content

Instantly share code, notes, and snippets.

@Daniel-Worrall
Last active July 30, 2019 21:01
Show Gist options
  • Save Daniel-Worrall/422a8ad2cf2ec966d0b43fd1dd2427f2 to your computer and use it in GitHub Desktop.
Save Daniel-Worrall/422a8ad2cf2ec966d0b43fd1dd2427f2 to your computer and use it in GitHub Desktop.
Backup all named local volumes on docker
folder_name = "" # Dir.pwd
volumes = %x`docker volume ls`
arr = []
volumes.scan(/local\s*(\w*)/) { |x| arr << x[0] }
arr.reject!{ |x| x =~ /^[0123456789abcdef]+$/ }
arr.each do |volume_name|
p %x`docker run --rm -v #{volume_name}:/volume_data -v #{folder_name}:/backup ubuntu tar cvf /backup/#{volume_name}.tar /volume_data`
end
folder_name = "" # Dir.pwd
Dir.chdir(folder_name)
Dir.glob("*.tar").each do |filename|
volume_name = filename[0..-5]
%x`docker run --rm -v #{volume_name}:/volume_data -v #{folder_name}:/backup ubuntu bash -c "cd /volume_data && tar xvf /backup/#{filename} --strip 1"`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment