Fake Files

需求

为了迎(yìng)接(fù)上级部门检查,需要提供系统日志文件的备份记录。要求是每天一个文件夹,其下对应有若干个文件,具体如下:
file

实现

Linux

直接用 bash shell 来实现:

#!/bin/bash

# 设置起始和结束日期
start_date="20240101"
end_date="20240331"

# 将日期转换为时间戳
start_ts=$(date -d "$start_date" +%s)
end_ts=$(date -d "$end_date" +%s)

# 开始循环,来生成目录和文件
current_ts=$start_ts
while [ $current_ts -le $end_ts ]; do
    # 获取当前日期
    current_date=$(date -d "@$current_ts" +%Y%m%d)

    # 创建目录
    mkdir -p "$current_date"

    # 设置文件名和时间戳,对应在每天的23:20:08
    timestamp="${current_date}232008"
    file1="${timestamp}-operation-log-dateThreshold_info.xml"
    file2="${timestamp}-operation-log-dateThreshold-1.zip"
    file3="${timestamp}-system-log-dateThreshold_info.xml"
    file4="${timestamp}-system-log-dateThreshold-1.zip"

    # 创建文件并填充随机内容,文件大小也用随机数生成范围
    head -c $((RANDOM % 101 + 700)) /dev/urandom > "$current_date/$file1"
    head -c $((RANDOM % 10001 + 40000)) /dev/urandom > "$current_date/$file2"
    head -c $((RANDOM % 101 + 700)) /dev/urandom > "$current_date/$file3"
    head -c $((RANDOM % 1001 + 3000)) /dev/urandom > "$current_date/$file4"

    # 将文件生成时间戳修改为指定时间
    touch -d "${current_date} 23:20:08" "$current_date/$file1"
    touch -d "${current_date} 23:20:08" "$current_date/$file2"
    touch -d "${current_date} 23:20:08" "$current_date/$file3"
    touch -d "${current_date} 23:20:08" "$current_date/$file4"

    # 目录的时间戳也不要忘记设置
    touch -d "${current_date} 23:20:08" "$current_date"

    # 循环移动到下一个日期继续
    current_ts=$(($current_ts + 86400))
done

Windows

用 power shell 实现,文件扩展名为 .ps1 :

# 设置起始和结束日期
$start_date = "20240101"
$end_date = "20240331"

# 将日期转换为时间戳
$start_ts = [datetime]::ParseExact($start_date, "yyyyMMdd", $null).ToFileTimeUtc()
$end_ts = [datetime]::ParseExact($end_date, "yyyyMMdd", $null).ToFileTimeUtc()

# 开始循环,来生成目录和文件
$current_ts = $start_ts
while ($current_ts -le $end_ts) {
    # 获取当前日期
    $current_date = [datetime]::FromFileTimeUtc($current_ts).ToString("yyyyMMdd")

    # 创建目录
    New-Item -ItemType Directory -Path $current_date -Force

    # 设置文件名和时间戳,对应在每天的23:20:08
    $timestamp = "${current_date}232008"
    $file1 = "${timestamp}-operation-log-dateThreshold_info.xml"
    $file2 = "${timestamp}-operation-log-dateThreshold-1.zip"
    $file3 = "${timestamp}-system-log-dateThreshold_info.xml"
    $file4 = "${timestamp}-system-log-dateThreshold-1.zip"

    # 创建文件并填充随机内容,文件大小也用随机数生成范围
    $random = New-Object System.Random
    $file1_size = $random.Next(700, 800)
    $file2_size = $random.Next(40000, 50000)
    $file3_size = $random.Next(700, 800)
    $file4_size = $random.Next(3000, 4000)

    $file1_content = [byte[]]::new($file1_size)
    $file2_content = [byte[]]::new($file2_size)
    $file3_content = [byte[]]::new($file3_size)
    $file4_content = [byte[]]::new($file4_size)

    $random.NextBytes($file1_content)
    $random.NextBytes($file2_content)
    $random.NextBytes($file3_content)
    $random.NextBytes($file4_content)

    [System.IO.File]::WriteAllBytes("$current_date\$file1", $file1_content)
    [System.IO.File]::WriteAllBytes("$current_date\$file2", $file2_content)
    [System.IO.File]::WriteAllBytes("$current_date\$file3", $file3_content)
    [System.IO.File]::WriteAllBytes("$current_date\$file4", $file4_content)

    # 将文件生成时间戳修改为指定时间
    $timestamp_date = [datetime]::ParseExact("${current_date} 23:20:08", "yyyyMMdd HH:mm:ss", $null)
    (Get-Item "$current_date\$file1").LastWriteTime = $timestamp_date
    (Get-Item "$current_date\$file2").LastWriteTime = $timestamp_date
    (Get-Item "$current_date\$file3").LastWriteTime = $timestamp_date
    (Get-Item "$current_date\$file4").LastWriteTime = $timestamp_date

    # 目录的时间戳也不要忘记设置
    (Get-Item $current_date).LastWriteTime = $timestamp_date

    # 循环移动到下一个日期继续
    $current_ts += 864000000000 # 增加一天的时间戳(单位为100纳秒)
}

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注