| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# (c) 2022, John McCall (@lowlydba) |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r''' |
| 8 |
--- |
| 9 |
module: restore |
| 10 |
short_description: Performs a restore operation |
| 11 |
description: |
| 12 |
- Performs a database restore operation. |
| 13 |
version_added: 0.9.0 |
| 14 |
options: |
| 15 |
database: |
| 16 |
description: |
| 17 |
- The database to process. |
| 18 |
type: str |
| 19 |
required: false |
| 20 |
path: |
| 21 |
description: |
| 22 |
- Path to SQL Server backup files. |
| 23 |
- Paths passed in as strings will be scanned using the desired method, default is a recursive folder scan. |
| 24 |
- Accepts multiple paths separated by C(,). |
| 25 |
type: str |
| 26 |
required: true |
| 27 |
destination_data_directory: |
| 28 |
description: |
| 29 |
- Path to restore the SQL Server backups to on the target instance. |
| 30 |
- If only this option is specified, then all database files (data and log) will be restored to this location |
| 31 |
type: str |
| 32 |
required: false |
| 33 |
destination_log_directory: |
| 34 |
description: |
| 35 |
- Path to restore the database log files to. |
| 36 |
- This option can only be specified alongside I(destination_data_directory). |
| 37 |
type: str |
| 38 |
required: false |
| 39 |
destination_filestream_directory: |
| 40 |
description: |
| 41 |
- Path to restore FileStream data to. |
| 42 |
- This option can only be specified alongside I(destination_data_directory). |
| 43 |
type: str |
| 44 |
required: false |
| 45 |
restore_time: |
| 46 |
description: |
| 47 |
- Specify a datetime string C(HH:MM:SS MM/DD/YYYY) to which you want the database restored to. |
| 48 |
- Default is to the latest point available in the specified backups. |
| 49 |
type: str |
| 50 |
required: false |
| 51 |
with_replace: |
| 52 |
description: |
| 53 |
- Indicates if the restore is allowed to replace an existing database. |
| 54 |
type: bool |
| 55 |
required: false |
| 56 |
default: false |
| 57 |
keep_replication: |
| 58 |
description: |
| 59 |
- Indicates whether replication configuration should be restored as part of the database restore operation. |
| 60 |
type: bool |
| 61 |
required: false |
| 62 |
default: false |
| 63 |
xp_dirtree: |
| 64 |
description: |
| 65 |
- Switch that indicated file scanning should be performed by the SQL Server instance using C(xp_dirtree). |
| 66 |
- This will scan recursively from the passed in path. |
| 67 |
- You must have sysadmin role membership on the instance for this to work. |
| 68 |
type: bool |
| 69 |
required: false |
| 70 |
no_xp_dir_recurse: |
| 71 |
description: |
| 72 |
- If specified, prevents the C(XpDirTree) process from recursing (its default behaviour). |
| 73 |
type: bool |
| 74 |
required: false |
| 75 |
default: false |
| 76 |
verify_only: |
| 77 |
description: |
| 78 |
- Indicates the restore should be verified only. |
| 79 |
type: bool |
| 80 |
required: false |
| 81 |
default: false |
| 82 |
maintenance_solution_backup: |
| 83 |
description: |
| 84 |
- Switch to indicate the backup files are in a folder structure as created by Ola Hallengreen's maintenance scripts. |
| 85 |
- This allows for faster file parsing. |
| 86 |
type: bool |
| 87 |
required: false |
| 88 |
default: false |
| 89 |
ignore_log_backup: |
| 90 |
description: |
| 91 |
- Indicates to skip restoring any log backups. |
| 92 |
type: bool |
| 93 |
required: false |
| 94 |
default: false |
| 95 |
ignore_diff_backup: |
| 96 |
description: |
| 97 |
- Indicates to skip restoring any differential backups. |
| 98 |
type: bool |
| 99 |
required: false |
| 100 |
default: false |
| 101 |
use_destination_default_directories: |
| 102 |
description: |
| 103 |
- Switch that tells the restore to use the default Data and Log locations on the target server. |
| 104 |
- If they don't exist, the function will try to create them. |
| 105 |
type: bool |
| 106 |
required: false |
| 107 |
reuse_source_folder_structure: |
| 108 |
description: |
| 109 |
- By default, databases will be migrated to the destination Sql Server's default data and log directories. |
| 110 |
- You can override this by using C(reuse_source_folder_structure). |
| 111 |
type: bool |
| 112 |
required: false |
| 113 |
destination_file_prefix: |
| 114 |
description: |
| 115 |
- This value will be prefixed to B(all) restored files (log and data). |
| 116 |
type: str |
| 117 |
required: false |
| 118 |
restored_database_name_prefix: |
| 119 |
description: |
| 120 |
- A string which will be prefixed to the start of the restore Database's name. |
| 121 |
type: str |
| 122 |
required: false |
| 123 |
directory_recurse: |
| 124 |
description: |
| 125 |
- If specified the specified directory will be recursed into (overriding the default behaviour). |
| 126 |
type: bool |
| 127 |
required: false |
| 128 |
default: false |
| 129 |
standby_directory: |
| 130 |
description: |
| 131 |
- If a directory is specified the database(s) will be restored into a standby state, |
| 132 |
with the standby file placed into this directory (which must exist, and be writable by the target Sql Server instance). |
| 133 |
type: str |
| 134 |
required: false |
| 135 |
replace_db_name_in_file: |
| 136 |
description: |
| 137 |
- If switch set any occurrence of the original database's name in a data or log file |
| 138 |
will be replace with the name specified in the I(database_name) option. |
| 139 |
type: bool |
| 140 |
required: false |
| 141 |
destination_file_suffix: |
| 142 |
description: |
| 143 |
- This value will be suffixed to B(all) restored files (log and data). |
| 144 |
type: str |
| 145 |
required: false |
| 146 |
keep_cdc: |
| 147 |
description: |
| 148 |
- Indicates whether CDC information should be restored as part of the database. |
| 149 |
type: bool |
| 150 |
required: false |
| 151 |
stop_before: |
| 152 |
description: |
| 153 |
- Switch to indicate the restore should stop before I(stop_mark) occurs, default is to stop when mark is created. |
| 154 |
type: bool |
| 155 |
required: false |
| 156 |
default: false |
| 157 |
stop_mark: |
| 158 |
description: |
| 159 |
- Marked point in the transaction log to stop the restore at. |
| 160 |
type: str |
| 161 |
required: false |
| 162 |
stop_after_date: |
| 163 |
description: |
| 164 |
- By default the restore will stop at the first occurence of I(stop_mark) found in the chain, |
| 165 |
passing a datetime string C(HH:MM:SS MM/DD/YYYY) will cause it to stop the first I(stop_mark) after that datetime. |
| 166 |
type: str |
| 167 |
required: false |
| 168 |
no_recovery: |
| 169 |
description: |
| 170 |
- Indicates if the databases should be recovered after last restore. |
| 171 |
type: bool |
| 172 |
required: false |
| 173 |
default: false |
| 174 |
max_transfer_size: |
| 175 |
description: |
| 176 |
- Sets the size of the unit of transfer. Values must be a multiple of 64kb. |
| 177 |
type: int |
| 178 |
required: false |
| 179 |
default: 0 |
| 180 |
block_size: |
| 181 |
description: |
| 182 |
- Specifies block size to use. |
| 183 |
type: str |
| 184 |
required: false |
| 185 |
choices: ['0.5kb','1kb','2kb','4kb','8kb','16kb','32kb','64kb'] |
| 186 |
buffer_count: |
| 187 |
description: |
| 188 |
- Number of I/O buffers to use. |
| 189 |
type: int |
| 190 |
required: false |
| 191 |
default: 0 |
| 192 |
azure_credential: |
| 193 |
description: |
| 194 |
- The name of the SQL Server credential to be used if restoring from an Azure hosted backup using Storage Access Keys. |
| 195 |
type: str |
| 196 |
required: false |
| 197 |
author: "John McCall (@lowlydba)" |
| 198 |
requirements: |
| 199 |
- L(dbatools,https://www.powershellgallery.com/packages/dbatools/) PowerShell module |
| 200 |
extends_documentation_fragment: |
| 201 |
- lowlydba.sqlserver.sql_credentials |
| 202 |
- lowlydba.sqlserver.attributes.check_mode |
| 203 |
- lowlydba.sqlserver.attributes.platform_all |
| 204 |
''' |
| 205 |
|
| 206 |
EXAMPLES = r''' |
| 207 |
- name: Restore a Database |
| 208 |
lowlydba.sqlserver.restore: |
| 209 |
sql_instance: sql-01.myco.io |
| 210 |
database: LowlyDB |
| 211 |
|
| 212 |
- name: Restore a Database and allow future T-Log restores |
| 213 |
lowlydba.sqlserver.restore: |
| 214 |
sql_instance: sql-01.myco.io |
| 215 |
database: LowlyDB1 |
| 216 |
no_recovery: true |
| 217 |
|
| 218 |
- name: Verify backup files, no restore |
| 219 |
lowlydba.sqlserver.restore: |
| 220 |
sql_instance: sql-01.myco.io |
| 221 |
database: LowlyDB2 |
| 222 |
verify_only: true |
| 223 |
''' |
| 224 |
|
| 225 |
RETURN = r''' |
| 226 |
data: |
| 227 |
description: Modified output from the C(Restore-DbaDatabase) function. |
| 228 |
returned: success, but not in check_mode. |
| 229 |
type: dict |
| 230 |
''' |