{"id":502790,"date":"2022-10-22T03:48:52","date_gmt":"2022-10-22T00:48:52","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-back-up-your-docker-volumes\/"},"modified":"2022-10-22T03:48:52","modified_gmt":"2022-10-22T00:48:52","slug":"how-to-back-up-your-docker-volumes","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-back-up-your-docker-volumes\/","title":{"rendered":"#How to Back Up Your Docker Volumes"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a33b95906e58\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a33b95906e58\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-back-up-your-docker-volumes\/#%E2%80%9CHow_to_Back_Up_Your_Docker_Volumes%E2%80%9D\" >&#8220;How to Back Up Your Docker Volumes&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-back-up-your-docker-volumes\/#Managing_Volume_Backups\" >Managing Volume Backups<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-back-up-your-docker-volumes\/#Restoring_Your_Backup\" >Restoring Your Backup<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-back-up-your-docker-volumes\/#Backing_Up_Volumes_Directly\" >Backing Up Volumes Directly<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-back-up-your-docker-volumes\/#Summary\" >Summary<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CHow_to_Back_Up_Your_Docker_Volumes%E2%80%9D\"><\/span>&#8220;How to Back Up Your Docker Volumes&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-805981\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/Docker-New.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Graphic showing the Docker logo\" width=\"1600\" height=\"900\"\/><\/p>\n<p>Docker volumes are used to store persistent data separately from your containers. Data that\u2019s kept in a volume remains accessible after your containers stop, allowing you to containerize stateful workloads.<\/p>\n<p>Although volumes outlive containers, this isn\u2019t enough protection for production <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lications. You should back up your volumes so you can recover them after a disaster. Creating regular volume backups ensures you\u2019re able to restore your environment if your Docker host is compromised or data is accidentally deleted.<\/p>\n<h2 id=\"managing-volume-backups\"><span class=\"ez-toc-section\" id=\"Managing_Volume_Backups\"><\/span>Managing Volume Backups<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker doesn\u2019t have a built-in mechanism for backing up volumes or exporting their contents. You need to set up your own solution to access the volume and copy its data to your backup destination.<\/p>\n<p>Creating a temporary container that mounts the volume you need to back up is usually the easiest way to proceed. Add the <code>--volumes-from<\/code> flag to a <code>docker run<\/code> command to automatically mount an existing container\u2019s volumes into your backup container. You can then use tools such as <code>tar<\/code> and <code>gzip<\/code> to deposit an archive of the volume\u2019s content into your working directory.<\/p>\n<p>Here\u2019s a complete example of this technique:<\/p>\n<pre escaped=\"true\" lang=\"$1\"># Create a container that stores data in the \"mysql_data\" volume&#13;\ndocker run -d \\&#13;\n    --name mysql \\&#13;\n    -v mysql_data:\/var\/lib\/mysql \\&#13;\n    -e MYSQL_ROOT_PASSWORD=mysql \\&#13;\n    mysql:8&#13;\n&#13;\n# Start a temporary container to back up the \"mysql_data\" volume&#13;\ndocker run --rm \\&#13;\n    --volumes-from mysql&#13;\n    -v $PWD:\/backup-dir \\&#13;\n    ubuntu tar cvf \/backup-dir\/mysql-backup.tar \/var\/lib\/mysql<\/pre>\n<p>The <code>--volumes-from<\/code> flag means the temporary backup container receives access to the <code>mysql<\/code> container\u2019s volumes. The <code>\/var\/lib\/mysql<\/code> directory inside the backup container exposes the volume\u2019s content because this is the path used by the <code>mysql<\/code> container. Tarring the path will produce an archive of your volume that you can use as a backup. It gets deposited into your working directory because of the bind mount that\u2019s set up by the <code>-v<\/code> flag.<\/p>\n<p>The <code>--rm<\/code> flag will remove the backup container as soon as the command completes. This leaves the archive in your working directory, ready to be moved to long-term storage. You can automate backup creation by adding the <code>docker run<\/code> command as a cron task.<\/p>\n<h2 id=\"restoring-your-backup\"><span class=\"ez-toc-section\" id=\"Restoring_Your_Backup\"><\/span>Restoring Your Backup<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can use a similar technique to restore your backup. When you\u2019re replacing the contents of an existing volume, create another temporary container with the volume and a bind mount to your backup archive. Extract the contents of the archive into the volume\u2019s mount path.<\/p>\n<pre escaped=\"true\" lang=\"$1\">$ docker run --rm \\&#13;\n    --volumes-from mysql&#13;\n    -v $PWD:\/backup-dir&#13;\n    bash -c \"cd \/var\/lib\/mysql &amp;&amp; tar xvf \/backup-dir\/mysql-backup.tar\"<\/pre>\n<p>This can be risky if containers are actively using the volume. Overwriting files that are in use could cause errors and unexpected behavior. You can use the <code>docker stop<\/code> command to temporarily halt your containers before bringing them back up with <code>docker start<\/code>.<\/p>\n<pre escaped=\"true\" lang=\"$1\">$ docker stop mysql&#13;\n&#13;\n# Restore the backup&#13;\n# ...&#13;\n&#13;\n$ docker start mysql<\/pre>\n<p>Create the volume before you start your container if you\u2019re restoring a backup to a new host:<\/p>\n<pre escaped=\"true\" lang=\"$1\">$ docker volume create new_volume<\/pre>\n<p>Then mount this volume to your temporary container:<\/p>\n<pre escaped=\"true\" lang=\"$1\">docker run --rm \\&#13;\n    -v new_volume:\/var\/lib\/mysql&#13;\n    -v $PWD:\/backup-dir \\&#13;\n    ubuntu tar cvf \/backup-dir\/mysql-backup.tar \/var\/lib\/mysql<\/pre>\n<p>Starting your application container with the same volume will provide access to the files you\u2019ve restored:<\/p>\n<pre escaped=\"true\" lang=\"$1\">docker run -d \\&#13;\n    --name mysql \\&#13;\n    -v new_volume:\/var\/lib\/mysql \\&#13;\n    -e MYSQL_ROOT_PASSWORD=mysql \\&#13;\n    mysql:8<\/pre>\n<p>Testing these procedures lets you check your backups will be usable if you ever face a disaster.<\/p>\n<h2 id=\"backing-up-volumes-directly\"><span class=\"ez-toc-section\" id=\"Backing_Up_Volumes_Directly\"><\/span>Backing Up Volumes Directly<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The procedure outlined above is <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/storage\/volumes\/#backup-restore-or-migrate-data-volumes\">the recommended way<\/a> to back up Docker volumes. However some situations could be better served by directly copying content from where volumes are stored on your host\u2019s filesystem.<\/p>\n<p>You\u2019ll usually find the content of your volumes in <code>\/var\/lib\/docker\/volumes<\/code>. Each volume gets its own subdirectory, such as <code>\/var\/lib\/docker\/volumes\/mysql<\/code>. Within this top-level path you\u2019ll find a <code>_data<\/code> folder which contains all the files stored inside the volume.<\/p>\n<p>Archiving the <code>\/var\/lib\/docker\/volumes<\/code> directory can be a convenient way to quickly backup everything on your host. You\u2019ll need to use <code>sudo<\/code> though because everything under this path is owned by <code>root<\/code>.<\/p>\n<p>Backing up volumes in this way isn\u2019t recommended for regular use because it\u2019s not portable across installations. Docker\u2019s <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/storage\/volumes\/#use-a-volume-driver\">volume driver system<\/a> means volume data won\u2019t necessarily be stored on your host\u2019s filesystem \u2013 it could be on a network share or another remote location. This technique should only be attempted when you want a quick backup before you run maintenance on a specific machine.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker volumes need to be treated with care because they contain your application\u2019s persistent data. Creating regular back ups will protect you from data loss in case your host is compromised or an errant container process deletes files by mistake.<\/p>\n<p>While you can create backups by archiving Docker\u2019s installation directory, this should be avoided wherever possible. Temporary backup containers may seem cumbersome but they can be easily scripted and provide predictable results across volume drivers.<\/p>\n<p>Once you\u2019ve created a volume backup archive, remember to upload it to remote storage as soon as possible. A backup stored on the machine it originates from will be no help if you lose access or a hardware failure occurs.<\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/devops\/how-to-back-up-your-docker-volumes\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How to Back Up Your Docker Volumes&#8221; Docker volumes are used to store persistent data separately from your containers. Data that\u2019s kept in a volume remains accessible after your containers stop, allowing you to containerize stateful workloads. Although volumes outlive containers, this isn\u2019t enough protection for production applications. You should back up your volumes so&#8230;<\/p>\n","protected":false},"author":1,"featured_media":502791,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/Docker-New.jpeg?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-502790","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/502790","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=502790"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/502790\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/502791"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=502790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=502790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=502790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}