Latest update
This commit is contained in:
@@ -233,5 +233,219 @@ Default: `C:\Rubrik\backup-multi-{InstanceName}.log`
|
||||
|
||||
## License
|
||||
|
||||
This script is provided as-is for database backup automation. Ensure compliance with your organization's backup and retention policies.</content>
|
||||
This script is provided as-is for database backup automation. Ensure compliance with your organization's backup and retention policies.
|
||||
|
||||
---
|
||||
|
||||
# RestoreScript.ps1 - SQL Server Database Restore Script
|
||||
|
||||
A PowerShell script for restoring SQL Server databases from backup files, with support for full, differential, and log backups. It automatically catalogs backups from a specified directory and handles file relocation during restore operations.
|
||||
|
||||
## Features
|
||||
|
||||
- **Automatic Backup Cataloging**: Scans a directory for backup files and organizes them by database, type (FULL, DIFF, LOG), and timestamp
|
||||
- **Flexible Restore Options**: Supports restoring from full backups only, or full + diffs + logs in the correct sequence
|
||||
- **File Relocation**: Moves database and log files to specified paths during restore (e.g., for different drive layouts)
|
||||
- **Backup Verification**: Validates backup integrity and provides LSN range information
|
||||
- **LSN-Aware Sequencing**: Applies backups in the correct order to avoid LSN conflicts (full → all diffs → logs after latest diff)
|
||||
- **Error Handling**: Robust error detection with detailed SQL error messages
|
||||
|
||||
## Requirements
|
||||
|
||||
### System Requirements
|
||||
- **PowerShell**: Version 5.1 or higher
|
||||
- **Operating System**: Windows Server 2016 or later (or Windows 10/11 for development/testing)
|
||||
- **SQL Server**: SQL Server 2016 or later (Express, Standard, Enterprise, or Developer editions)
|
||||
- **Permissions**: SQL Server sysadmin privileges or appropriate database restore permissions
|
||||
|
||||
### Software Dependencies
|
||||
- **SQL Server PowerShell Module**: Either `SqlServer` or `SQLPS` module must be available
|
||||
- Install with: `Install-Module -Name SqlServer -AllowClobber`
|
||||
|
||||
### Network Requirements
|
||||
- **SQL Server Connectivity**: The script must be able to connect to the target SQL Server instance
|
||||
- **Backup Source Access**: Read access to the backup files directory
|
||||
- **Restore Destination Access**: Read/write access to the data and log file directories
|
||||
|
||||
## Installation
|
||||
|
||||
1. **Download the Script**:
|
||||
```powershell
|
||||
# Place RestoreScript.ps1 in your preferred scripts directory
|
||||
# Example: C:\Rubrik\Scripts\
|
||||
```
|
||||
|
||||
2. **Install SQL Server PowerShell Module** (if not already installed):
|
||||
```powershell
|
||||
Install-Module -Name SqlServer -AllowClobber -Force
|
||||
```
|
||||
|
||||
3. **Verify Permissions**:
|
||||
- Ensure the account running the script has SQL Server sysadmin privileges
|
||||
- Verify read access to backup directories and write access to restore destinations
|
||||
|
||||
## Usage
|
||||
|
||||
### Basic Syntax
|
||||
```powershell
|
||||
.\RestoreScript.ps1 -SqlInstance "ServerName\InstanceName" -LiveMountRoot "C:\BackupPath" -Action <action> [parameters]
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
| Parameter | Type | Required | Default | Description |
|
||||
|-----------|------|----------|---------|-------------|
|
||||
| `SqlInstance` | String | Yes | sqlfcsql\TESTINST | SQL Server instance name (e.g., "SERVER01\SQL2019") |
|
||||
| `LiveMountRoot` | String | Yes | - | Root directory containing backup files |
|
||||
| `Action` | String | Yes | - | Action to perform: `catalog`, `restore`, or `verify` |
|
||||
| `DatabaseName` | String | No | - | Specific database name (required for restore, optional for others) |
|
||||
| `DataPath` | String | No | - | Directory for data files (required for restore) |
|
||||
| `LogPath` | String | No | - | Directory for log files (required for restore) |
|
||||
|
||||
### Actions
|
||||
|
||||
#### Catalog Action
|
||||
Lists all backup files found in the LiveMountRoot directory, organized by database and backup type.
|
||||
|
||||
**Example**:
|
||||
```powershell
|
||||
.\RestoreScript.ps1 -SqlInstance "SQLFC1\SQLFC1P" -LiveMountRoot "C:\Rubrik\lm\" -Action catalog
|
||||
```
|
||||
|
||||
**Output**:
|
||||
```
|
||||
Database Backups Catalog:
|
||||
=========================
|
||||
Database: DataAW2
|
||||
Type: FULL
|
||||
Backup: 20251127093305
|
||||
C:\Rubrik\lm\DataAW2_FULL_20251127093305_0001.bak
|
||||
Type: DIFF
|
||||
Backup: 20251128005350
|
||||
C:\Rubrik\lm\DataAW2_DIFF_20251128005350_0001.bak
|
||||
...
|
||||
Type: LOG
|
||||
Backup: 20251130130213
|
||||
C:\Rubrik\lm\DataAW2_LOG_20251130130213_0001.trn
|
||||
...
|
||||
|
||||
Summary of Databases:
|
||||
- DataAW2
|
||||
```
|
||||
|
||||
#### Restore Action
|
||||
Restores a specific database using the latest full backup, all subsequent diffs, and logs taken after the latest diff. Files are relocated to the specified DataPath and LogPath.
|
||||
|
||||
**Example**:
|
||||
```powershell
|
||||
.\RestoreScript.ps1 -SqlInstance "SQLFC1\SQLFC1P" -LiveMountRoot "C:\Rubrik\lm\" -Action restore -DatabaseName "DataAW2" -DataPath "F:\Data" -LogPath "G:\Logs"
|
||||
```
|
||||
|
||||
**Output**:
|
||||
```
|
||||
Restoring FULL backup for DataAW2...
|
||||
Applying DIFF backup 20251128005350 for DataAW2...
|
||||
Applying DIFF backup 20251129010111 for DataAW2...
|
||||
Applying DIFF backup 20251130000053 for DataAW2...
|
||||
Applying DIFF backup 20251201113159 for DataAW2...
|
||||
Applying LOG backup 20251201124800 for DataAW2...
|
||||
Finalizing restore for DataAW2...
|
||||
Restore completed for DataAW2
|
||||
```
|
||||
|
||||
#### Verify Action
|
||||
Verifies backup file integrity and displays LSN ranges and backup details.
|
||||
|
||||
**Example**:
|
||||
```powershell
|
||||
.\RestoreScript.ps1 -SqlInstance "SQLFC1\SQLFC1P" -LiveMountRoot "C:\Rubrik\lm\" -Action verify -DatabaseName "DataAW2"
|
||||
```
|
||||
|
||||
**Output**:
|
||||
```
|
||||
Verifying backups for database: DataAW2
|
||||
Verifying FULL backup 20251127093305 for DataAW2...
|
||||
Verification successful for FULL 20251127093305
|
||||
Backup Details:
|
||||
Start Date: 11/27/2025 09:33:05
|
||||
Finish Date: 11/27/2025 09:33:07
|
||||
First LSN: 56000000289000001
|
||||
Last LSN: 56000000289300001
|
||||
Database Backup LSN: 56000000182200001
|
||||
Differential Base LSN:
|
||||
...
|
||||
Backup Summary for database: DataAW2
|
||||
===================================
|
||||
FULL Backups:
|
||||
Date: 11/27/2025 09:33:07 | LSN Range: 56000000289000001 - 56000000289300001
|
||||
DIFFERENTIAL Backups:
|
||||
Date: 11/25/2025 00:21:45 | Base LSN: 56000000182200001 | LSN Range: 56000000270100001 - 56000000270400001
|
||||
...
|
||||
LOG Backups:
|
||||
Point-in-Time Range: 11/30/2025 13:02:13 to 12/01/2025 12:48:00
|
||||
LSN Range: 56000000345300001 - 56000000353400001
|
||||
No gaps detected in LOG sequence
|
||||
```
|
||||
|
||||
## Restore Logic
|
||||
|
||||
The script follows a specific restore sequence to ensure data consistency:
|
||||
|
||||
1. **Full Backup**: Always starts with the latest full backup
|
||||
2. **Differential Backups**: Applies all differential backups taken after the full backup
|
||||
3. **Log Backups**: Applies only log backups taken after the latest differential backup
|
||||
|
||||
This approach avoids LSN conflicts that can occur when mixing diffs and logs from different time ranges.
|
||||
|
||||
## Error Handling
|
||||
|
||||
### Common Issues
|
||||
|
||||
#### "Directory lookup for the file failed"
|
||||
**Cause**: Original file paths don't exist on the restore server
|
||||
**Solution**: Use `-DataPath` and `-LogPath` to relocate files to valid directories
|
||||
|
||||
#### "The log in this backup set terminates at LSN X, which is too early to apply"
|
||||
**Cause**: Log backup is from before the applied differential backup
|
||||
**Solution**: The script automatically handles this by only applying logs after the latest diff
|
||||
|
||||
#### "Database not found in catalog"
|
||||
**Cause**: No backups found for the specified database
|
||||
**Solution**: Run catalog action first to verify available databases
|
||||
|
||||
#### "Access denied" to directories
|
||||
**Cause**: Insufficient permissions on data/log paths
|
||||
**Solution**: Ensure the SQL Server service account has write access to the specified directories
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Debug Steps
|
||||
1. Run the `catalog` action to verify backup files are detected correctly
|
||||
2. Run the `verify` action to check backup integrity and LSN ranges
|
||||
3. Ensure DataPath and LogPath directories exist and are writable
|
||||
4. Check SQL Server error logs for additional details
|
||||
|
||||
### Performance Considerations
|
||||
- Restore operations can be time-intensive for large databases
|
||||
- Ensure sufficient disk space on target directories
|
||||
- Monitor SQL Server performance during restore operations
|
||||
|
||||
## Security Considerations
|
||||
|
||||
- **SQL Permissions**: Requires sysadmin or database restore permissions
|
||||
- **File System Access**: Read access to backup sources, write access to restore destinations
|
||||
- **Data Protection**: Ensure backup files are from trusted sources
|
||||
|
||||
## Support and Maintenance
|
||||
|
||||
### Monitoring
|
||||
- Verify restore completion and database accessibility
|
||||
- Check SQL Server logs for any restore-related errors
|
||||
- Validate file locations after restore
|
||||
|
||||
### Best Practices
|
||||
- Test restores in development environments first
|
||||
- Keep backup files organized and accessible
|
||||
- Document custom DataPath/LogPath configurations
|
||||
- Regularly verify backup integrity with the verify action</content>
|
||||
<parameter name="filePath">README.md
|
||||
Reference in New Issue
Block a user