This PowerShell script automates the process of compressing and archiving log files based on their last modified time, as well as managing monthly backups. The script operates in two phases:
- Daily File Compression and Cleanup:
- It reads a list of file paths from the “FilePaths” input file and compresses log files that haven’t been modified in the last 24 hours into ZIP files.
- Once the files are compressed, the original non-ZIP files are deleted from the source directory.
- The destination ZIP file is named according to the folder’s name and the current date.
- Monthly Backup Routine:
- On the 1st of each month, the script archives ZIP files that are older than one month.
- The monthly ZIP files are moved to a backup folder created on the drive, under the directory
BackUp
. - After compression, the old ZIP files are removed from the source directory, ensuring a clean file system.
Tech Used
- PowerShell Scripting: The primary technology used to automate file compression, deletion, and directory creation.
- File and Directory Management: Managing file system objects, including recursively fetching files, checking file paths, and creating backup directories.
- Date Manipulation: Handling dates and times for file selection and naming using PowerShell’s
Get-Date
method. - Compression: Utilizing PowerShell’s
Compress-Archive
cmdlet for file compression into ZIP format. - Error Handling: Implementing error handling mechanisms like
Test-Path
andNew-Item
for safely creating backup directories.