How to Backup and Restore Docker Volumes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to W code in this video we're going to learn how to backup and restore Docker volumes and as an example we're going to back up a postrace database using Docker volumes so to start what we're going to do is create a postc container and volume and fill it with some default data and we're going to do that using an initialization script which is this script right here so initialization scripts are essentially just SQL or script files that are placed inside a folder called Docker entrypoint init db. D in the docker image so after we run this essentially any SQL or script file placed inside this directory will be ran by the post CR stalker image so right here is the docker file that we're going to use to create our post gra image for the volume that we will then back up so we have our environment variables to get the postrace image working and then we have our initialization script but anyway let's build this image now so Docker build I'm going to tag it as just p-i and we need to provide the docker file and the next thing I'm going to do is create a Docker container from this image so we of course do that with Docker run so it's going to be Docker run see if I can zoom in one more we have Docker run we give it a name of p-c just do some port mappings and then we have our volume postra data M to dasar lib post SQL data and we create it from our image p-i so this postc volume right here postc data will contain all our postra data and this is because by default this location right here is the directory where the database files for post Grace are stored but so let's just create this container so now we can see our database is up and ready to accept connections so we have our volume and we have it filled with data which is this data right here so now what I want to do is create a tarball of this volume data so we can start working with our backup and the way we can do this so I'm going to open another terminal I'm going to put it up here and we can do this using the tar command and so tar is basically a software utility for for collecting many files into an archive file and they're usually used for distribution and backup purposes and we can run this command using Docker actually so I'm going to paste in another command up here and go over what we're doing so here we're creating a tarball of the data referenced by the postrace data volume so specifically we're going to spin up a postrace container and then tear it down after we performed the backup of the postrace volume so- RM right here removes the container after it's stopped this volume from flag right here allows us to mount the volumes of another container so p-c is the name of our container in this terminal right here so essentially this will copy over our postra data volume then we're creating another volume which is going to be our current working directory and it's going to be mapped to a folder called Dash backup in the image and the docker image we're going to be using is busy box which is essentially a small image containing many common Unix utilities and one of them is tar which which we going to run and we're going to do CF which will essentially create a new archive file called postrace backup. tar inside this backup directory in the image and it's going to create it from this postra data and so this postrace data will come in from this volume right here and then because we have this volume mapped to our current working directory and then we're creating this tar inside it that'll essentially place this tar on our host computer so to demonstrate right now we can see we don't have it right there but if we run this command now we have our postrace backup tar file right here so now what we want to do is create a volume from this backup so we have our backup tar so let's use it and to do this we're going to run another busy box container and then use the tar command to extract the backup tar ball into a volume so let me paste the command to do this so here we're essentially creating a new volume from our postas backup. tar file and after this volume is created we can apply it to another container so specifically we have Docker run to create a container we have- RM to remove the container after it's stopped then we map a volume which is postc data 2 which is currently empty but we're going to fill it with the data in our tar file and we do that by mapping it to this location in the image or in our busy box container then we have another volume where we're going to bring in our postas backup. tar into the container and then we're going to run the tar Command right here which will extract this tar file into the container and essentially when we do that it'll fill this location with data and because our postc of data 2 volume is mapped to it it'll fill up this volume so if we run this we can see it all running down here so now essentially if we list out our Docker volumes we have one called postc Data 2 which is filled with the data from this tar file which was the data in here so we essentially backed up this volume into another one and now what we need to do is just verify that the backup worked and to do that we just need to create another container from our postc image and apply that volume so now we're going to name this one p- C2 we're going to map a different port and for our volume we're going to apply postra data 2 and map it to this location so let's run this and we can see actually in the logs postc database appears to contain a database which comes from our volume and now we can see that we have um we're ready to accept connections so let's just verify now that this worked and we can do that by using psql so this one is running on Port 5433 but me open up another terminal up here and so the first thing we're going to do is we're going to run a bash cell against this terminal with Docker exec so now we're inside this container right here and what we want to do is connect to our database which if we look at our Docker file we called it my DB so let's run psql now we're inside there now what we need to do is Select something so let's try and get this data so if we run select all from subscriber we can see all our data listed here so we can see how the backup worked successfully as the data that was in here we created a tar ball of it and then we essentially extracted all that into a different volume and then applied it to another container but this is my video on how to backup a Docker volume you can see how easy and also useful this is for so I'd recommend having this maybe set up in a as a crown job I'll probably make a video on that soon so if you're interested uh like And subscribe besides that if you want to support me further please consider downloading my Chrome extension called Witter Link in the description but besides that have a good one see you in the next one
Info
Channel: WittCode
Views: 62
Rating: undefined out of 5
Keywords: WittCode, wittcode, backup docker volumes, back up docker volumes, how to backup docker volumes, how to back up docker volumes, docker backups, docker backup, backup docker, how to backup postgres database docker
Id: 3JUqkyZCK88
Channel Id: undefined
Length: 6min 33sec (393 seconds)
Published: Thu Jun 06 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.