How To Backup Docker Container Volumes Using Powershell Core
Backup each directory into a Tar file
- Login to the docker vm and elevate to root
-
Launch Powershell Core
pwsh
If you don’t have powershell core installed, see Installing Powershell Core on Linux
-
Open docker volumes directory
cd /var/lib/docker/volumes/
-
Compress each directory into a file with a corresponding name
# store list of directories in a variable $directories = Get-ChildItem -Directory # list commands to be run $directories | ForEach-Object { echo "tar -cvzf $($_.name).tgz $($_.name)" } # run commands listed above $directories | ForEach-Object { tar -cvzf "$($_.name).tgz" "$($_.name)" }
- Now you are free to download or move the tar files to a backup location for safe keeping.
Restore each Tar file back to their original directory
- Login to the docker vm and elevate to root
-
Launch Powershell Core
pwsh
-
Open docker volumes directory
cd /var/lib/docker/volumes/
-
Copy/Move your backups (.tgz) files into the directory you want to restore them to.
cp \temp\*.tgz /var/lib/docker/volumes/ #note: replace \temp with the directory your backups are located in
-
Uncompress each file into their original directory
# store list of .tgz files in a variable $files = Get-ChildItem -File -Filter *.tgz # list commands to be run $files | ForEach-Object { echo "tar -zxvf $($_.name)" } # run commands listed above $files | ForEach-Object { tar -zxvf "$($_.name)" }
-
Remove backups (IF NO LONGER NEEDED)
rm *.tgz