Perrypackettracer / Powershell-scripts-to-use-in-an-active-directory

0 stars 0 forks source link

PowerShell script to create backups on a Windows Server. #25

Open Perrypackettracer opened 10 months ago

Perrypackettracer commented 10 months ago

Below is a simple PowerShell script to create backups on a Windows Server. This script uses the built-in wbadmin command, which is available on Windows Server editions. Adjust the script based on your backup requirements.

# Backup Configuration
$BackupDrive = "E:"  # Specify the drive where the backup will be stored
$BackupFolder = "Backups"  # Specify the folder name for storing backups
$BackupTarget = "$BackupDrive\$BackupFolder"  # Full path to the backup folder

# Create backup folder if it doesn't exist
if (-not (Test-Path -Path $BackupTarget -PathType Container)) {
    New-Item -Path $BackupTarget -ItemType Directory
}

# Backup Configuration
$BackupName = "ServerBackup"  # Specify a name for the backup
$BackupPolicy = "Incremental"  # Specify the backup policy (Full, Incremental, etc.)

# Perform backup using wbadmin
try {
    Write-Host "Initiating backup..."
    wbadmin start backup -backupTarget:$BackupTarget -include:C:\ -allCritical -vssFull -quiet -backuptarget:$BackupTarget -allCritical -systemState -policy:$BackupPolicy -quiet
    Write-Host "Backup completed successfully."
} catch {
    Write-Host "Backup failed. Error: $_"
}

Explanation:

  1. Set the $BackupDrive to the drive where you want to store backups.

  2. Specify the $BackupFolder as the folder name within the specified drive.

  3. The script checks if the backup folder exists. If not, it creates the folder.

  4. Set the $BackupName to a name for the backup, and $BackupPolicy to the desired backup policy (e.g., "Full" or "Incremental").

  5. The wbadmin command is used to start the backup process with the specified configuration.

  6. The script catches any errors that may occur during the backup process.

  7. Customize the script based on your backup requirements, such as specifying additional drives or folders to include in the backup.

  8. Save the script with a .ps1 extension and run it using PowerShell.

  9. You can schedule this script to run at specified intervals using Task Scheduler for automated backups.

Ensure that the account running the script has the necessary permissions to perform backups and write to the specified backup location. Adjust paths and configurations based on your server setup.

Perrypackettracer commented 10 months ago

Below is a simple PowerShell script that you can use to create a server backup every 7 days. This script assumes that you want to create a backup of specific directories. Make sure to customize the script according to your server configuration and backup requirements.

# Set the source and destination directories for the backup
$sourceDirectory = "C:\Path\To\Your\Source"
$destinationDirectory = "D:\Path\To\Your\Backup"

# Set the backup frequency in days
$backupFrequencyInDays = 7

# Get the current date
$currentDate = Get-Date

# Calculate the date for the last backup
$lastBackupDate = $currentDate.AddDays(-$backupFrequencyInDays)

# Check if it's time to create a new backup
if ((Test-Path $destinationDirectory) -and ((Get-Item $destinationDirectory).LastWriteTime -lt $lastBackupDate)) {
    # Create a backup folder with the current date as the name
    $backupFolderName = "Backup_" + $currentDate.ToString("yyyyMMdd_HHmmss")
    $backupFolderPath = Join-Path -Path $destinationDirectory -ChildPath $backupFolderName
    New-Item -ItemType Directory -Path $backupFolderPath

    # Copy files and directories from source to destination
    Copy-Item -Path $sourceDirectory\* -Destination $backupFolderPath -Recurse -Force

    Write-Host "Backup created successfully at $backupFolderPath"
} else {
    Write-Host "No backup needed at this time."
}

Instructions for customization:

  1. Set the $sourceDirectory variable to the path of the directory you want to back up.
  2. Set the $destinationDirectory variable to the path where you want to store your backups.
  3. Adjust the $backupFrequencyInDays variable to set the desired frequency for backups.
  4. Save the script with a .ps1 extension, for example, BackupScript.ps1.
  5. Schedule the script to run automatically using Task Scheduler or any other scheduling mechanism on your server.

Remember to test the script in a safe environment before deploying it to production to ensure it meets your requirements.