A lightweight Docker container for automated file moving and logging, using cron
to handle both recurring and one-time file-moving tasks between specified directories.
- Customizable schedule for recurring file-moving tasks.
- One-time or immediate file-moving task execution using
cron
. - Logging of each file transfer, with error messages logged separately.
- Lightweight Alpine-based Docker image with support for setting time zones.
- Docker installed on your system.
To build the Docker image locally:
docker build -t file-mover .
Start the container to run with default behavior. By default, it will check for files in /app/source
and move them to /app/target
, logging each operation to /app/log/file-mover.log
.
docker run -d --name file-mover \
-v /path/to/source:/app/source \
-v /path/to/target:/app/target \
-v /path/to/log:/app/log \
file-mover
The container accepts the following command-line options:
Option | Description |
---|---|
--schedule [TIME] |
Schedule the container to run the file move operation repeatedly at a specified time (e.g., * * * * * cron format). |
--run-once [TIME] |
Run the container to execute the file move operation once at a specified time in cron format (e.g., 00 21 1 12 * to run at 9:00 PM on December 1st). If no time is provided, the operation runs immediately. |
--help |
Display help message with available options. |
--version |
Display the script version. |
This command schedules a one-time execution of the file-moving operation at a specified time using cron
format (e.g., 00 21 1 12 *
to run at 9:00 PM on the 1st of December):
docker run --rm -d --name file-mover \
-v /path/to/source:/app/source \
-v /path/to/target:/app/target \
-v /path/to/log:/app/log \
-e TZ=Asia/Colombo \
file-mover --run-once "00 21 1 12 *"
If the --run-once
option is used with no time argument, the operation will execute immediately.
To schedule a recurring file-moving operation every day at 9:00 PM, while ensuring the container restarts if stopped:
docker run -d --name file-mover \
-v /path/to/source:/app/source \
-v /path/to/target:/app/target \
-v /path/to/log:/app/log \
--restart always \
-e TZ=Asia/Colombo \
file-mover --schedule "00 21 * * *"
Use the TZ
environment variable to set the container's time zone. The example above uses Asia/Colombo
.
The container defines the following directories for file operations:
- /app/source: Mount this directory as the source location for files to be moved.
- /app/target: Mount this directory as the destination for files being moved.
- /app/log: Mount this directory for storing logs of file operations.
- Dockerfile: Sets up the Alpine environment, installs
tzdata
, and configures the working directories and entrypoint script. - entrypoint.sh: Main entry script that handles command-line options and schedules tasks with
cron
. - app.sh: The core script that checks for new files in the source directory, moves them to the target directory, and logs each operation.
All file-moving operations are logged to /app/log/file-mover.log
with time-stamped informational messages. Errors are also logged with details.
time=2024-09-15 15:20:35 level=info msg=checking for new files.
time=2024-09-15 15:20:36 level=info msg=2 file(s) found.
time=2024-09-15 15:20:36 level=info msg=example.txt has been moved.
Hereโs an example docker-compose.yml
file to run the container with Docker Compose:
services:
file-mover:
image: file-mover
container_name: file-mover
volumes:
- /path/to/source:/app/source
- /path/to/target:/app/target
- /path/to/log:/app/log
environment:
- TZ=Asia/Colombo
command: ["--schedule", "00 21 * * *"]
restart: always
To start the container with Docker Compose:
docker-compose up -d
You can display the help message and version information by running the container with the respective options:
# Display help message
docker run --rm file-mover --help
# Display version information
docker run --rm file-mover --version
This project is licensed under the MIT License.