#!/bin/bash
source_dir=$1
target_dir=$2
log_file="backup_log.txt"
if [ -z "$source_dir" ] || [ -z "$target_dir" ]; then
echo "Usage: $0 <source_directory> <target_directory>"
exit 1
fi
if [ ! -d "$source_dir" ]; then
echo "Source directory $source_dir does not exist."
exit 1
fi
if [ ! -d "$target_dir" ]; then
mkdir -p "$target_dir"
fi
for file in "$source_dir"/*; do
if [ -f "$file" ]; then
file_name=$(basename "$file")
target_file="$target_dir/$file_name"
cp "$file" "$target_file"
backup_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "$backup_time $file $target_file" >> "$log_file"
fi
done