Help with Docker Copy Command Issue When Using Variables

0
1
Asked By TechSavvy123 On

I'm trying to automate the backup process for my Pi-hole instance, but I'm running into some odd behavior with the Docker copy command. I execute `sudo docker exec pihole pihole-FTL --teleporter` to create a backup and then try to set a variable with the filename using `export BACKUP=$(sudo docker exec -t pihole find / -maxdepth 1 -name '*teleporter*.zip')`. After that, I use the variable to copy the backup file: `sudo docker cp pihole:"$BACKUP" /mnt/synology/apps/pihole`.

The script deletes the zip file from inside the container after copying to avoid confusion with future backups. The variable holds the value like `/pi-hole_57f2c340b9f0_teleporter_2025-08-11_11-12-14_EDT.zip` when I echo it, but I get an error when running the cp command: `Error response from daemon: Could not find the file /pi-hole_57f2c340b9f0_teleporter_2025-08-11_11-12-14_EDT.zip in container pihole`. However, if I run the same command without using a variable, it works fine. The variable seems to include an unwanted leading `/`, which might be causing the issue. I'm stumped as to what could be going wrong here!

3 Answers

Answered By DockerGuru42 On

Just a thought — you might need to wrap the variable in quotes within the source argument like this: `sudo docker cp "pihole:$BACKUP" /mnt/synology/apps/pihole`. Additionally, adding `set -x` at the start of your script could help show the commands that are actually being executed. It might also be worth looking into using a bind mount for backups instead. You could store them directly in a directory you can access more easily without needing the docker cp command.

Answered By BackupWhiz On

Try running your script with a bash option to debug, like this: `bash -vx /path/to/your/script | cat -A`. This could reveal hidden characters or formatting issues that might be causing the problem.

Answered By CodeNinja88 On

Have you thought about checking out the environment variable settings within your bash script? You might need the `-E` or `--preserve-env` option with sudo to keep your existing environment variables intact. Just a thought!

UserCurious -

That might be worth checking out. I'm just unsure how that would affect the variable expansion before the command runs.

SudoSleuth -

Good point! But if the variable is being set in the shell, wouldn't it automatically resolve before sudo runs?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.