Backup and restore strategies in Docker - Time & Space Complexity
When backing up or restoring data in Docker, it's important to understand how the time taken grows as the data size increases.
We want to know how the process scales when handling more files or larger volumes.
Analyze the time complexity of the following Docker backup command.
docker run --rm \
-v my_volume:/data \
-v $(pwd):/backup \
alpine \
tar czf /backup/backup.tar.gz -C /data .
This command creates a compressed archive of all files in a Docker volume for backup.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Tar command reads and compresses each file in the volume.
- How many times: Once per file and directory inside the volume.
The time to create the backup grows roughly with the number of files and their total size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 files | 10 file reads and compressions |
| 100 files | 100 file reads and compressions |
| 1000 files | 1000 file reads and compressions |
Pattern observation: The time grows linearly as the number of files increases.
Time Complexity: O(n)
This means the backup time increases directly in proportion to the number of files and data size.
[X] Wrong: "Backing up a volume always takes the same time regardless of data size."
[OK] Correct: The backup process reads and compresses each file, so more data means more time.
Understanding how backup time scales helps you design efficient data management and recovery plans in real projects.
What if we changed the backup to only include files modified in the last day? How would the time complexity change?
Practice
Solution
Step 1: Understand backup purpose
Backing up means saving data safely so it is not lost.Step 2: Relate to Docker volumes
Docker volumes store important data outside containers, so backing them up protects that data.Final Answer:
To save important data so it can be restored later -> Option DQuick Check:
Backup = Save data safely [OK]
- Confusing backup with speeding up containers
- Thinking backup reduces image size
- Assuming backup updates Docker itself
mydata using tar?Solution
Step 1: Identify backup command syntax
Backing up a volume usestar cvfto create an archive of the volume data.Step 2: Check each option
docker run --rm -v mydata:/data -v /backup:/backup busybox tar cvf /backup/mydata.tar /dataruns a container mounting the volume and backup directory and usestar cvfto save data.docker run --rm -v mydata:/data busybox tar xvf /backup/mydata.tar /dataextracts instead of creating.docker volume backup mydata /backup/mydata.taris invalid Docker syntax.docker save mydata > /backup/mydata.tarsaves an image, not a volume.Final Answer:
docker run --rm -v mydata:/data -v /backup:/backup busybox tar cvf /backup/mydata.tar /data -> Option BQuick Check:
Backup = tar cvf inside container [OK]
- Using tar xvf which extracts instead of backs up
- Trying non-existent docker volume backup command
- Using docker save which backs up images, not volumes
docker run --rm -v mydata:/data -v /backup:/backup busybox tar xvf /backup/mydata.tar -C /data
Solution
Step 1: Understand the tar command
Thetar xvfcommand extracts files from an archive.Step 2: Analyze the Docker command
The command mounts the volumemydataat/dataand the backup directory at/backupand extracts the archive/backup/mydata.tarinto it using-C /data.Final Answer:
It restores the volume data from the backup archive -> Option AQuick Check:
tar xvf = extract = restore [OK]
- Confusing xvf with cvf (backup vs restore)
- Thinking it lists archive contents
- Assuming it deletes data
docker run --rm -v mydata:/data busybox tar cvf /backup/mydata.tar /data
But the backup file is empty. What is the likely mistake?
Solution
Step 1: Identify the command purpose
tar cvfcreates an archive (backup), not restore.Step 2: Understand the mistake
The command tries to create a backup but is used when intending to restore, so the backup file is empty or incorrect.Final Answer:
Creating a backup instead of restoring -> Option CQuick Check:
tar cvf = backup, not restore [OK]
- Confusing backup and restore tar options
- Mounting volume incorrectly
- Forgetting to use -C option
data1 and data2 into separate tar files and then restore them later. Which sequence of commands correctly achieves this?Solution
Step 1: Backup each volume separately
Usetar cvfinside a container mounting each volume and the backup directory to create separate tar files.Step 2: Restore each volume separately
Usetar xvfwith-Coption inside a container mounting each volume and the backup directory to extract data back.Step 3: Verify other options
Other sequences use invalid Docker commands likedocker volume backup, commands meant for images likedocker save/load, or for containers likedocker export/import.Final Answer:
docker run --rm -v data1:/data -v /backup:/backup busybox tar cvf /backup/data1.tar /data<br>docker run --rm -v data2:/data -v /backup:/backup busybox tar cvf /backup/data2.tar /data<br>docker run --rm -v data1:/data -v /backup:/backup busybox tar xvf /backup/data1.tar -C /data<br>docker run --rm -v data2:/data -v /backup:/backup busybox tar xvf /backup/data2.tar -C /data -> Option AQuick Check:
Backup and restore volumes with tar inside containers [OK]
- Using docker save/load which works for images, not volumes
- Using non-existent docker volume backup commands
- Confusing export/import with volume backup
Ready to prove it?
Pass the test and this pattern moves unknown
