skip to Main Content

I can copy all files and folders from /var/www in docker container with name container_name to host folder /var/mybackup with command
docker cp container_name:/var/www /var/mybackup

But problem i need to create exclusion of few folder which i don’t want to copy as example too "heavy" folders or sensitive information

2

Answers


  1. I think it would be best to prepare your data first, and then copy it into the container.

    • Prepare data, which needs to be copied, in a separate folder
    • Then copy this separate folder into your container with a single docker cp command.

    In general:

    • If you want to serve a static webpage, I’d better build your own Docker image (with Dockerfile). This way you can automate the process and store images in a container registry. Upgrading your webpage to a newer version would then only mean to exchange the image with a newer version.
    • If you prefer to have your webpage contents not inside the image, you could make /var/www a shared host directory, so that you don’t need to copy data into the container, but just update it on the host machine.
    Login or Signup to reply.
  2. I would use bash to write a script to do that for me. In the script I would then specify the files and folders to exclude. Like in the example below, I have excluded files that exceed a certain size…

    #!/bin/bash
    
    # Define variables
    CONTAINER_NAME="your_container_name"
    SOURCE_DIR="/var/www"
    DEST_DIR="/var/backup"
    TEMP_DIR="/tmp/container_copy"
    
    # Create temporary directory on host
    mkdir -p "$TEMP_DIR"
    
    # Copy all files to a temporary directory on the host
    docker cp "$CONTAINER_NAME:$SOURCE_DIR" "$TEMP_DIR"
    
    # Use find to exclude files larger than 20 MB and copy to the backup folder
    find "$TEMP_DIR/www" -type f -size -20M -exec rsync -R {} "$DEST_DIR" ;
    
    # Clean up temporary directory
    rm -rf "$TEMP_DIR"
    
    echo "Copy completed, excluding files larger than 20 MB."
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search