├── .gitignore
├── Documentation
├── Change Log
├── LICENSE
└── To-Do List
├── README.md
├── description
└── script
/.gitignore:
--------------------------------------------------------------------------------
1 | # OS files
2 | .DS_Store
3 | Thumbs.db
4 | ehthumbs.db
5 | Desktop.ini
6 | .LSOverride
7 | ._*
8 |
9 | # user files
10 | .vscode
11 |
--------------------------------------------------------------------------------
/Documentation/Change Log:
--------------------------------------------------------------------------------
1 | ## Change Log ##
2 |
3 | # v1.3.1 - 2020/01/21
4 | # So Say We All
5 | #
6 | # - added option to create a vm specific log in each vm's sub-folder.
7 |
8 | # v1.3.0 - 2020/01/15
9 | # Better than standard
10 | #
11 | # - added option to use zstd inline compression.
12 |
13 | # v1.2.3 - 2019/12/24
14 | # Leftovers
15 | #
16 | # - fixed extension matching case sensitivity.
17 |
18 | # v1.2.2 - 2019/12/18
19 | # Sam Vimes
20 | #
21 | # - added logging to local vm config dump actions.
22 | # - added ability to use vms_to_backup as an exclusion list when backup_all_vms is enabled.
23 | # - fixed rm commands missing -fv options.
24 |
25 | # v1.2.1 - 2019/12/11
26 | # Moth-man is Real
27 | #
28 | # - fixed bug that could cause snapshots not to be removed in specific situations.
29 |
30 | # v1.2.0 - 2019/11/27
31 | # Starring Tom Hanks
32 | #
33 | # - major rewrites to make script smaller, slightly more efficient, and more modular.
34 | # - added option to choose different snapshot extension.
35 | # - added option to allow all vms to be backed up without having to explicitly list them.
36 | # - added option to revert back to standard backup if snapshot fails.
37 | # - added option to pause vms during standard backups instead of shutting down.
38 | # - added support for pulling nvram extension from the config file.
39 | # - fixed 'integer expression expected' error when performing regular expression integer comparisons.
40 | # - fixed dry run option not working.
41 | # - workaround for 'namespace warning' error when working with config file.
42 | # - improved readability of log file by removing unnecessary line breaks.
43 |
44 |
45 | # v1.1.5 - 2019/11/21
46 | # In Loving Memory of Net Neutrality
47 | #
48 | # - fixed some errors not generating an error log file.
49 | # - updated comments.
50 | # - updated documentation to include important information.
51 | # - reverse change log order.
52 | # - added support for more vdisk extensions by parsing them directly from the config.
53 | # - added user variable (case-insensitive) for blacklisting extensions. iso included by default.
54 | # - fixed issue with where files would not be removed after compressing when config files were not set to be backed up.
55 | # - updated comments to use more consistent terms.
56 | # - added option to allow specific vms to be backed up without shutting down.
57 | # - added option to allow vms to be backed up using snapshots. disabled by default.
58 | # - fixed script trying to remove leftover files that weren't part of the backup.
59 |
60 | # v1.1.4 - 2018/05/19
61 | # A Rose by Any Other Name
62 | #
63 | # - fixed vdisks not being skipped.
64 | # - fixed incorrect copying when using delta sync with multiple vdisks.
65 | # - fixed number of vdisks being kept when VM has multiple vdisks.
66 | # - added option to enable reconstruct write during backups.
67 |
68 | # v1.1.3 - 2018/04/16
69 | # From Here to Infirmary
70 | #
71 | # - included inline variable to prevent script from running if a parity check is in progress.
72 | # - fixed spelling errors in script.
73 | # - added advanced option to ignore iso files during vdisk check.
74 | # - fixed so tarballs so files are added without full paths.
75 | # - updated user selectable options so that they only generate a warning in the log, not a full error log file.
76 |
77 | # v1.1.2 - 2018/03/26
78 | # The Land Before Time 4: how many are there anyway?
79 | #
80 | # - updated readme.
81 | # - added option to only receive error notifications.
82 | # - fixed issue where not all failures were using notification system.
83 | # - fixed issue where not all warnings were using notification system.
84 | # - fixed send_notifications not being able to disable most notifications.
85 | # - added detailed notification option.
86 | # - detailed notifications: sends notifications when vm backups are started and stopped.
87 | # - detailed notifications: sends notifications when old backups are deleted.
88 | # - detailed notifications: sends notifications when old log files are deleted.
89 | # - made rm commands verbose.
90 | # - updated notification messages to be more precise.
91 | # - updated and fixed script syntax.
92 | # - fixed issues with tarball backups not removing original files.
93 | # - added option to backup nvram files. enabled by default.
94 | # - added detailed logging for tarballs.
95 | # - fixed issue where rm didn't always get logged to the log file.
96 |
97 | # v1.1.1 - 2018/03/18
98 | # For the Queen
99 | #
100 | # - added option to disable delta syncs.
101 | # - added option to only use rsync.
102 | # - dry-runs can now be used with the rsync_only option.
103 | # - added dates to logging.
104 | # - creates log files in backup folder.
105 | # - creates error log files in backup folder.
106 | # - log messages pushed through unRAID notification system by default.
107 |
108 | # v1.1.0 - 2018/03/14
109 | # Stephen Hawking special edition
110 | #
111 | # - moved variables closer to the top to make them easier to access.
112 | # - added ability to specify specific vdisks to be skipped.
113 | # - vm_original_state can be overridden.
114 | # - xml and/or vdisk backups can be skipped.
115 | # - created variable to set number of days to keep backups.
116 | # - support for img and qcow2 files.
117 | # - made xml backups more efficient.
118 | # - can now set the number of backups to be kept.
119 | # - changed from mtime to mmin for more accurate cleaning.
120 | # - timestamps are always used except when the number of backups being kept is equal to 1.
121 | # - added ability to compress backups.
122 | # - added option to compare backups and retry once on failure.
123 | # - updated disclaimer.
124 |
125 | # v1.0.0 - 2018/03/13
126 | # initial release
127 | #
128 | # includes features from original as well as:
129 | # - Uses cp for initial sync because it is quicker than rsync and has the added bonus (at least in my testing) of creating smaller sparse files than rsync does.
130 | # - Keeps one copy of each vdisk to offset risk of using --inplace with rsync.
131 | # - Uses rsync for syncing changes to backups.
132 | # - Corrected some logging errors.
133 | # - Temporarily disabled keeping backups for a set number of days.
134 | # - Changed default check from 60 seconds to 30 seconds and doubled number of allowed failures to 20.
135 | # - VMs are now started after backup if they were running before the backup was started.
136 |
--------------------------------------------------------------------------------
/Documentation/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 | {one line to give the program's name and a brief idea of what it does.}
635 | Copyright (C) {year} {name of author}
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | {project} Copyright (C) {year} {fullname}
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/Documentation/To-Do List:
--------------------------------------------------------------------------------
1 | ## To-Do List ##
2 | # - create script to update scripts. will need ability to skip update if it will break functionality of existing scripts. plus ability to keep existing user variables.
3 | # - possible to make into a plugin?
4 | # - option to backup vdisks using full paths. will resolve issues with unique names.
5 | # - test case where vdisk1 does not exist.
6 | # - plugin/script for enabling and disabling snapshots on specific vm for testing?
7 | # - ignore/change number of backups to keep and number of days to keep backups warnings
8 | # - check to see if VM is running off existing snapshot file (possibly by using backingStore in config).
9 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # unraid-vmbackup
2 |
3 | v1.3.1 - 2020/01/21
4 |
5 | [](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=NG5HGW4Q3CZU4&source=url "Donations are appreciated")
6 |
7 | *A beta version for an unRAID plugin is located [here](https://github.com/JTok/unraid.vmbackup "VM Backup plugin") and in Community Applications.*
8 |
9 | Script for use with unRAID's CA User Scripts plugin. See [here](https://lime-technology.com/forums/topic/48286-plugin-ca-user-scripts/ "CA User Scripts") for more information.
10 |
11 | Currently the script is in a relatively stable state, and most of the features have been implemented. I have tested them as well as I can, but I cannot guarantee they will work correctly for everyone, so be sure to test thoroughly on your system before depending on this script for backups. Please review the Change Log and To-Do List if you would like to know more.
12 |
13 | ## Important
14 |
15 | The virtual disks attached to a single VM must have unique names regardless of their location since during the backup they will be placed into the same folder.
16 | i.e. VM1 cannot have /mnt/diskX/vdisk1.img and /mnt/users/domains/VM1/vdisk1.img since the vdisks will overwrite the each other during the backup. However, VM1 and VM2 can both have a vdisk1.img since they will be backed up to different folders.
17 |
18 | ## Installation
19 |
20 | - Add the script and description files to the CA User Scripts plugin. No other files are necessary to make the script work.
21 |
22 | - Set the variables in the script file.
23 |
24 | - be sure to set enabled = "1" to ensure that the script will run.
25 |
26 | - to enable the script while parity check is running change line 4 from "noParity=true" to "noParity=false"
27 |
28 | - Choose a schedule in the CA User Scripts plugin.
29 |
30 | ### Script options
31 |
32 | - Choose a backup location.
33 |
34 | - Choose to backup all VMs, or list specific VMs to be backed up.
35 |
36 | - If backup all VMs is enabled, the list of VMs to backup is used as an exclusion list instead.
37 |
38 | - List specific vdisks to skip, if any.
39 |
40 | - List specific vdisk extensions to skip, if any (iso listed by default).
41 |
42 | - Option to use snapshots to backup VMs without shutting down.
43 |
44 | - be sure to install the qemu guest agent on VMs to enable quiescence, which will improve the integrity of backups.
45 |
46 | - the disk path in the VM config cannot be /mnt/user, but instead must be /mnt/cache or /mnt/diskX.
47 |
48 | - Option to kill a VM that won't shutdown cleanly.
49 |
50 | #### VM restart options
51 |
52 | - Option to have VMs start after backup based on their previous state.
53 |
54 | - Advanced: Option to have VMs start after successful backup regardless of previous state.
55 |
56 | - Advanced: Option to have VMs start after failed backup regardless of previous state.
57 |
58 | #### Backup retention options
59 |
60 | - Choose the number of days to keep backups.
61 |
62 | - Choose the number backups to keep.
63 |
64 | #### Logging and notification options
65 |
66 | - Option to log to file.
67 |
68 | - Choose the number of log files to keep.
69 |
70 | - Option to create a VM specific log in each VM's sub-folder.
71 |
72 | - Uses the same retention policy as the VM's backups.
73 |
74 | - Option to log messages through unRAID notification system.
75 |
76 | - Option to only receive error notifications
77 |
78 | - Option to receive detailed notifications.
79 |
80 | - sends notifications when VM backups are started and stopped.
81 |
82 | - sends notifications when old backups are deleted.
83 |
84 | #### Additional options
85 |
86 | - Option to compress backups.
87 |
88 | - legacy option for tar.gz files.
89 |
90 | - support for zstandard.
91 |
92 | - Option to set compression level.
93 |
94 | - Option to choose number of threads.
95 |
96 | - Option to timestamp backups.
97 |
98 | - Option to disable delta syncs.
99 |
100 | - Option to only use rsync.
101 |
102 | - Advanced: Choose the extension used for snapshots.
103 |
104 | - Advanced: Option to fallback to standard backups if snapshot creation fails.
105 |
106 | - Advanced: Option to pause VMs instead of shutting them down during standard backups. Could result in unusable backups.
107 |
108 | - Advanced: Option to keep specific VMs running during backup. Not recommended.
109 |
110 | - Advanced: Option to enable reconstruct write during backups.
111 |
112 | - Advanced: Option to compare files and retry backup in the event of failure.
113 |
114 | - Advanced: Option to skip backing up xml configuration.
115 |
116 | - Advanced: Option to skip backing up nvram.
117 |
118 | - Advanced: Option to skip backing up vdisks.
119 |
120 | - Advanced: Choose the number of times to check if a VM is shut down.
121 |
122 | - Advanced: Choose the number of seconds to wait between checks to see if a VM is shut down.
123 |
124 | ##### Disclaimer
125 |
126 | I do not make any guarantees as to the function of this script. It is provided as-is. Use at your own risk.
127 |
128 | ###### Originally from unraid-autovmbackup by Daniel Jackson (danioj) [here](https://lime-technology.com/forums/topic/46281-unraid-autovmbackup-automate-backup-of-virtual-machines-in-unraid-v04/ "unraid-autovmbackup")
129 |
130 | ###### Includes additions for removing old backups added by Deeks [here](https://lime-technology.com/forums/topic/46281-unraid-autovmbackup-automate-backup-of-virtual-machines-in-unraid-v04/?do=findComment&comment=589821 "unraid-autovmbackup Deeks' script")
131 |
132 | ###### Includes additions for creating snapshots added by Dikkekop (thies88) [here](https://github.com/thies88/unraid-vmbackup "unraid-vmbackup Dikkekop's script")
133 |
--------------------------------------------------------------------------------
/description:
--------------------------------------------------------------------------------
1 | # unraid-vmbackup v1.3.1 - 2020/01/21
2 | a script for backing up VMs in unraid including vdisks and configuration files.
3 |
--------------------------------------------------------------------------------
/script:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | #backgroundOnly=true
3 | #arrayStarted=true
4 | #noParity=true
5 |
6 | # v1.3.1 - 2020/01/21
7 |
8 | #### DISCLAIMER ####
9 | # Use at your own risk. This is a work-in-progress and provided as is.
10 | # I have tested this on my own server, as best as I am able, but YMMV.
11 | # -jtok
12 |
13 |
14 | # what is the scripts' official name.
15 | official_script_name="script"
16 |
17 | # set the name of the script to a variable so it can be used.
18 | me=$(basename "$0")
19 |
20 |
21 | # this script copies unRAID vm's vdisks and their configurations to a specified location.
22 |
23 |
24 | ################################################## script variables start ######################################################
25 |
26 | # default 0 but set the master switch to 1 if you want to enable the script otherwise it will not run.
27 | enabled="0"
28 |
29 | # backup location to put vdisks.
30 | backup_location="/mnt/user/share/backup_folder/"
31 |
32 | # default is 0. backup all vms or use vms_to_backup.
33 | # when set to 1, vms_to_backup will be used as an exclusion list.
34 | backup_all_vms="0"
35 |
36 | # list of vms that will be backed up separated by a new line.
37 | # if backup_all_vms is set to 1, this will be used as a list of vms to exclude instead.
38 | vms_to_backup="
39 | vm1
40 | vm2
41 | "
42 |
43 | # list of specific vdisks to be skipped separated by a new line. use the full path.
44 | # NOTE: must match path in vm config file. remember this if you change the virtual disk path to enable snapshots.
45 | vdisks_to_skip="
46 | /mnt/user/domains/vm1/vdisk2.img
47 | /mnt/user/domains/vm1/vdisk3.img
48 | "
49 |
50 | # list of specific vdisk extensions to be skipped separated by a new line. this replaces the old ignore_isos variable.
51 | vdisk_extensions_to_skip="
52 | iso
53 | "
54 |
55 | # default is 0. use snapshots to backup vms.
56 | # NOTE: vms that are backed up using snapshots will not be shutdown. if a vm is already shutdown the default backup method will be used.
57 | # NOTE: it is highly recommended that you install the qemu guest agent on your vms before using snapshots to ensure the integrity of your backups.
58 | # WARNING: this will fail if the config path for the virtual disk is /mnt/user/. you must use /mnt/cache/ or /mnt/diskX/ for snapshots to work.
59 | use_snapshots="0"
60 |
61 | # default is 0. set this to 1 if you would like to kill a vm if it cant be shutdown cleanly.
62 | kill_vm_if_cant_shutdown="0"
63 |
64 | # default is 1. set this to 0 if you do not want a vm to be started if it was running before the backup started. Paused VMs will be left stopped.
65 | set_vm_to_original_state="1"
66 |
67 | # default is 0. set this to the number of days backups should be kept. 0 means indefinitely.
68 | number_of_days_to_keep_backups="0"
69 |
70 | # default is 0. set this to the number of backups that should be kept. 0 means infinitely.
71 | # WARNING: If VM has multiple vdisks, then they must end in sequential numbers in order to be correctly backed up (i.e. vdisk1.img, vdisk2.img, etc.).
72 | number_of_backups_to_keep="0"
73 |
74 | # default is 0. set this to 1 if you would like to perform inline zstd compression. This overrides the "compress_backups" and "compare_files" options.
75 | inline_zstd_compress="0"
76 |
77 | # default is 3. higher values may produce smaller archives but are slower and use more CPU.
78 | zstd_level="3"
79 |
80 | # default is 2. set this to the desired number of compression worker threads, or 0 to auto detect (i.e. use all)
81 | zstd_threads="2"
82 |
83 | # default is 0. set this to 1 if you would like to compress backups. This can add a significant amount of time to the backup process. uses tar.gz for sparse file compatibility.
84 | # this is the legacy setting for compression.
85 | # WARNING: do not turn on if you already have uncompressed backups. You will need to move or delete uncompressed backups before using. this will compress all config, nvram, and vdisk images in the backup directory into ONE tarball.
86 | compress_backups="0"
87 |
88 | # default is 1. set this to 0 if you would like to have backups without a timestamp. Timestamps are dropped only when number_of_backups_to_keep is equal to 1.
89 | timestamp_files="1"
90 |
91 |
92 | #### logging and notifications ####
93 |
94 | # default is 1. set to 0 to have log file deleted after the backup has completed.
95 | # NOTE: error logs are separate. settings for error logs can be found in the advanced variables.
96 | keep_log_file="1"
97 |
98 | # default is 1. number of successful log files to keep. 0 means infinitely.
99 | number_of_log_files_to_keep="1"
100 |
101 | # default is "logs". set to "" to put in root of backups folder. set to "logs/" to keep logs separate if running multiple versions of this script.
102 | log_file_subfolder="logs"
103 |
104 | # default is 0. create a vm specific log in each vm's subfolder using the same retention policy as the vm's backups.
105 | enable_vm_log_file="0"
106 |
107 | # default is 1. set to 0 to prevent notification system from being used. Script failures that occur before logging can start, and before this variable is validated will still be sent.
108 | send_notifications="1"
109 |
110 | # default is 0. set to 1 to receive more detailed notifications. will not work with send_notifications disabled or only_send_error_notifications enabled.
111 | detailed_notifications="0"
112 |
113 |
114 | #### advanced variables ####
115 |
116 | # default is snap. extension used when creating snapshots.
117 | # WARNING: do not choose an extension that is the same as one of your vdisks or the script will error out. cannot be blank.
118 | snapshot_extension="snap"
119 |
120 | # default is 0. fallback to standard backup if snapshot creation fails.
121 | # NOTE: this will act as though use_snapshots was disabled for just the vm with the failed snapshot command.
122 | snapshot_fallback="0"
123 |
124 | # default is 0. pause vms instead of shutting them down during standard backups.
125 | # WARNING: this could result in unusable backups, but I have not thoroughly tested.
126 | pause_vms="0"
127 |
128 | # list of vms that will be backed up WITHOUT first shutting down separated by a new line. these must also be listed in vms_to_backup.
129 | # NOTE: vms backed up via snapshot will not be shutdown (see use_snapshots option).
130 | # WARNING: using this setting can result in an unusable backup. not recommended.
131 | vms_to_backup_running="
132 | "
133 |
134 | # default is 0. set to 1 to have reconstruct write (a.k.a. turbo write) enabled during the backup and then disabled after the backup completes.
135 | # NOTE: may break auto functionality when it is implemented. do not use if reconstruct write is already enabled. backups may run faster with this enabled.
136 | enable_reconstruct_write="0"
137 |
138 | # default is 0. set this to 1 to compare files after copy and run rsync in the event of failure. could add significant amount of time depending on the size of vms.
139 | compare_files="0"
140 |
141 | # default is 1. set to 0 if you would like to skip backing up xml configuration files.
142 | backup_xml="1"
143 |
144 | # default is 1. set to 0 if you would like to skip backing up nvram files.
145 | backup_nvram="1"
146 |
147 | # default is 1. set to 0 if you would like to skip backing up vdisks. setting this to 0 will automatically disable compression.
148 | backup_vdisks="1"
149 |
150 | # default is 0. set this to 1 if you would like to start a vm after it has successfully been backed up. will override set_vm_to_original_state when set to 1.
151 | start_vm_after_backup="0"
152 |
153 | # default is 0. set this to 1 if you would like to start a vm after it has failed to have been backed up. will override set_vm_to_original_state when set to 1.
154 | start_vm_after_failure="0"
155 |
156 | # default is 0. set this to 1 to disable rsync delta syncs.
157 | disable_delta_sync="0"
158 |
159 | # default is 0. set this to 1 to always use rsync instead of cp.
160 | # NOTE: rsync was significantly slower in my tests.
161 | rsync_only="0"
162 |
163 | # default is 1. set this to 0 if you would like to perform a dry-run backup.
164 | # NOTE: dry run will not work unless rsync_only is set to 1. if this is set to 1 rsync_only will be set to 1.
165 | actually_copy_files="1"
166 |
167 | # default is 20. set this to the number of times you would like to check if a clean shutdown of a vm has been successful.
168 | clean_shutdown_checks="20"
169 |
170 | # default is 30. set this to the number of seconds to wait in between checks to see if a clean shutdown has been successful.
171 | seconds_to_wait="30"
172 |
173 | # default is 1. set to 0 to have error log files deleted after the backup has completed.
174 | keep_error_log_file="1"
175 |
176 | # default is 10. number of error log files to keep. 0 means infinitely.
177 | number_of_error_log_files_to_keep="10"
178 |
179 | # default is 0. set to 1 to only send error notifications.
180 | only_send_error_notifications="0"
181 |
182 | ################################################## script variables end #########################################################
183 |
184 |
185 | ###################################################### script start #############################################################
186 |
187 |
188 | #### define functions start ####
189 |
190 | # copy files based on passed arguments.
191 | copy_file () {
192 | # assign arguments to local variables for readability.
193 | local source="$1"
194 | local destination="$2"
195 | local rsync_dry_run_option="$3"
196 | local sync_type="$4"
197 | local rsync_only="$5"
198 | if [[ ! "$rsync_only" =~ ^[0-9]+$ ]]; then
199 | local rsync_only="0"
200 | fi
201 |
202 | # determine the copy command that should be ran and capture the result.
203 | case "$sync_type" in
204 | "standard")
205 | # perform standard rsync.
206 | rsync -av"$rsync_dry_run_option" "$source" "$destination"
207 | local copy_result="$?"
208 | ;;
209 |
210 | "inline_zstd_compress")
211 | # perform inline zstd compression (with sparse file support).
212 | zstd -$zstd_level -T$zstd_threads --sparse "$source" -o "$destination"
213 | local copy_result="$?"
214 | ;;
215 |
216 | "sparse")
217 | # perform rsync or copy with support for sparse files.
218 | if [ "$rsync_only" -eq 1 ]; then
219 | rsync -av"$rsync_dry_run_option" --sparse "$source" "$destination"
220 | local copy_result="$?"
221 |
222 | else
223 |
224 | cp -av --sparse=always "$source" "$destination"
225 | local copy_result="$?"
226 | fi
227 | ;;
228 |
229 | "inplace")
230 | # perform inplace copy (i.e. delta sync).
231 | rsync -av"$rsync_dry_run_option" --inplace --no-whole-file "$source" "$destination"
232 | ;;
233 |
234 | *)
235 | # no valid copy choice was able to be ran.
236 | log_message "failure: no valid copy choice was able to run for copy of $source to $destination failed." "copy failed" "alert"
237 |
238 | # exit the function
239 | return 1
240 | ;;
241 | esac
242 |
243 | # get rsync result and send notification
244 | if [[ "$copy_result" -eq 1 ]]; then
245 | log_message "failure: copy of $source to $destination failed." "copy failed" "alert"
246 |
247 | else
248 |
249 | # set actually_copy_files based rsync_dry_run_option
250 | if [ "$rsync_dry_run_option" == "n" ]; then
251 | local actually_copy_files="0"
252 |
253 | else
254 |
255 | local actually_copy_files="1"
256 | fi
257 |
258 | # send a message to the user based on whether there was an actual copy or a dry-run.
259 | if [ "$actually_copy_files" -eq 0 ]; then
260 | log_message "information: dry-run copy of $source to $destination complete."
261 |
262 | else
263 |
264 | log_message "information: copy of $source to $destination complete." "completed copy" "normal"
265 |
266 | fi
267 | fi
268 | }
269 |
270 | # pass log messages to log files and system notifications.
271 | log_message () {
272 |
273 | # assign arguments to local variables for readability.
274 | local message="$1"
275 | local description="$2"
276 | local importance="$3"
277 |
278 | case "$importance" in
279 | "information")
280 | local is_error="0"
281 | ;;
282 | "alert")
283 | local is_error="1"
284 | ;;
285 | "warning")
286 | local is_error="1"
287 | ;;
288 | *)
289 | local is_error="0"
290 | ;;
291 | esac
292 |
293 | if [ "$description" ] && [ "$importance" ]; then
294 | local enable_detailed_notifications="1"
295 | else
296 | local enable_detailed_notifications="0"
297 | fi
298 | local force_notification="$4"
299 |
300 | # add the message to the main log file.
301 | echo "$(date '+%Y-%m-%d %H:%M:%S') $message" | tee -a "$backup_location/$log_file_subfolder$timestamp""unraid-vmbackup.log"
302 |
303 | # add the message to the vm specific log file if enabled.
304 | if [[ -n "$vm_log_file" ]] && [ "$enable_vm_log_file" -eq 1 ]; then
305 | echo "$(date '+%Y-%m-%d %H:%M:%S') $message" >> "$vm_log_file"
306 | fi
307 |
308 | # check to see if the message is an error message.
309 | if [ "$is_error" -eq 1 ]; then
310 |
311 | # mark that the script encountered an error.
312 | errors="1"
313 |
314 | # send a notification if they are enabled.
315 | if [ "$send_notifications" -eq 1 ] || [ "$force_notification" == "force_notification" ]; then
316 | /usr/local/emhttp/plugins/dynamix/scripts/notify -s "unRAID VM Backup script" -d "$description" -i "$importance" -m "$(date '+%Y-%m-%d %H:%M:%S') $message"
317 | fi
318 | fi
319 |
320 | # send a detailed notification if it is enabled and if they should be used.
321 | if [ "$enable_detailed_notifications" -eq 1 ] && [ "$detailed_notifications" -eq 1 ]; then
322 | if [ "$send_notifications" -eq 1 ] && [ "$only_send_error_notifications" -eq 0 ]; then
323 | /usr/local/emhttp/plugins/dynamix/scripts/notify -s "unRAID VM Backup script" -d "$description" -i "$importance" -m "$(date '+%Y-%m-%d %H:%M:%S') $message"
324 | fi
325 | fi
326 | }
327 |
328 | # pass notification messages to system notifications.
329 | notification_message () {
330 |
331 | # assign arguments to local variables for readability.
332 | local message="$1"
333 | local description="$2"
334 | local importance="$3"
335 |
336 | # show the message in the log.
337 | echo "$(date '+%Y-%m-%d %H:%M:%S') $message"
338 |
339 | # send message notification.
340 | if [[ -n "$description" ]] && [[ -n "$importance" ]]; then
341 | /usr/local/emhttp/plugins/dynamix/scripts/notify -s "unRAID VM Backup script" -d "$description" -i "$importance" -m "$(date '+%Y-%m-%d %H:%M:%S') $message"
342 | fi
343 | }
344 |
345 | # compare two files.
346 | run_compare () {
347 |
348 | # assign arguments to local variables for readability.
349 | local source="$1"
350 | local destination="$2"
351 | local file_type="$3"
352 |
353 | # check to see if compare_files is enabled. if yes, check for config differences.
354 | if [ "$compare_files" -eq 1 ]; then
355 | if ! cmp -s "$source" "$destination"; then
356 | log_message "warning: $file_type file for $vm is different than source file. retrying backup." "$(basename "$source") compare failed" "warning"
357 |
358 | case "$file_type" in
359 | "config")
360 | copy_file "$source" "$destination" "$rsync_dry_run_option" "standard" "$rsync_only"
361 | ;;
362 | "nvram")
363 | copy_file "$source" "$destination" "$rsync_dry_run_option" "standard" "$rsync_only"
364 | ;;
365 | "vdisk")
366 | copy_file "$source" "$destination" "$rsync_dry_run_option" "sparse" "$rsync_only"
367 | ;;
368 | *)
369 | log_message "warning: unable to re-run copy command for $source. file_type is $file_type." "$(basename "$source") compare copy failed" "warning"
370 | ;;
371 | esac
372 |
373 | # make sure copy has current date/time for modified attribute so that removing old backups by date will work.
374 | touch -d "now" "$destination"
375 |
376 | if ! cmp -s "$source" "$destination"; then
377 | log_message "failure: $file_type file for $vm failed second comparison." "$(basename "$source") second compare failed" "alert"
378 |
379 | else
380 |
381 | log_message "information: $file_type file for $vm passed second comparison. moving on."
382 | fi
383 |
384 | else
385 |
386 | log_message "information: $file_type file for $vm matches source file. moving on."
387 | fi
388 | fi
389 | }
390 |
391 | # copy vdisks.
392 | copy_vdisks () {
393 |
394 | # assign arguments to local variables for readability.
395 | local mode="$1"
396 |
397 | # get number of vdisks assoicated with the vm.
398 | vdisk_count=$(xmllint --xpath "count(/domain/devices/disk/source/@file)" "$vm.xml")
399 |
400 | # unset array for vdisks.
401 | unset vdisks
402 | # initialize vdisks as empty array
403 | vdisks=()
404 |
405 | # unset dictionary for vdisk_types.
406 | unset vdisk_types
407 | # initailize vdisk_types as dictionary.
408 | declare -A vdisk_types
409 |
410 | # unset dictionary for vdisk_types.
411 | unset vdisk_specs
412 | # initailize vdisk_types as dictionary.
413 | declare -A vdisk_specs
414 |
415 | # get vdisk paths from config file.
416 | for (( i=1; i<=vdisk_count; i++ ))
417 | do
418 | vdisk_path="$(xmllint --xpath "string(/domain/devices/disk[$i]/source/@file)" "$vm.xml")"
419 | vdisk_type="$(xmllint --xpath "string(/domain/devices/disk[$i]/driver/@type)" "$vm.xml")"
420 | vdisk_spec="$(xmllint --xpath "string(/domain/devices/disk[$i]/target/@dev)" "$vm.xml")"
421 |
422 | vdisks+=("$vdisk_path")
423 | vdisk_types["$vdisk_path"]="$vdisk_type"
424 | vdisk_specs["$vdisk_path"]="$vdisk_spec"
425 | done
426 |
427 | # check for the header in vdisks to see if there are any disks
428 | if [ ${#vdisks[@]} -eq 0 ]; then
429 | log_message "warning: there are no vdisk(s) associated with $vm to backup." "no vdisk(s) for $vm" "warning"
430 | fi
431 |
432 | # unset array for vdisk_extensions.
433 | unset vdisk_extensions
434 | # initialize vdisk_extensions as empty array.
435 | vdisk_extensions=()
436 |
437 | # get vdisk names to check on current backups
438 | for disk in "${vdisks[@]}"
439 | do
440 |
441 | if [[ -n "$disk" ]]; then
442 |
443 | # assume disk will not be skipped.
444 | skip_disk="0"
445 |
446 | # check to see if vdisk should be explicitly skipped.
447 | for skipvdisk_name in $vdisks_to_skip
448 | do
449 |
450 | if [ "$skipvdisk_name" == "$disk" ]; then
451 | skip_disk="1"
452 | log_message "information: $disk on $vm was found in vdisks_to_skip. skipping disk."
453 | fi
454 | done
455 |
456 | # get the extension of the disk.
457 | disk_extension="${disk##*.}"
458 |
459 | # disable case matching.
460 | shopt -s nocasematch
461 |
462 | # check to see if vdisk extension is the same as the snapshot extension. if it is, error and skip the vm.
463 | if [[ "$disk_extension" == "$snapshot_extension" ]]; then
464 | log_message "failure: extension for $disk on $vm is the same as the snapshot extension $snapshot_extension. disk will always be skipped. this usually means that the disk path in the config was not changed from /mnt/user. if disk path is correct, then try changing snapshot_extension or vdisk extension." "cannot backup vdisk on $vm" "alert"
465 | fi
466 |
467 | # check to see if vdisk should be skipped by extension.
468 | for skipvdisk_extension in $vdisk_extensions_to_skip
469 | do
470 |
471 | if [[ "$skipvdisk_extension" == "$disk_extension" ]]; then
472 | skip_disk="1"
473 | log_message "information: extension for $disk on $vm was found in vdisks_extensions_to_skip. skipping disk."
474 | fi
475 | done
476 |
477 | # re-enable case matching.
478 | shopt -u nocasematch
479 |
480 | # get the filename of the disk without the path.
481 | new_disk=$(basename "$disk")
482 |
483 | if [ "$mode" == "existing_backup" ]; then
484 |
485 | # unset vairiable for disk_number
486 | unset -v disk_number
487 |
488 | # get the disk number and extension
489 | vdisknameregex="[0-9]+\\.$disk_extension"
490 | if [[ $disk =~ $vdisknameregex ]]; then
491 | disk_number=${BASH_REMATCH[0]}
492 | fi
493 |
494 | # skip the vdisk if skip_disk is set to 1
495 | if [ "$skip_disk" -ne 1 ]; then
496 |
497 | # unset variable for the most recent vdisk file.
498 | unset -v newest_vdisk_file
499 |
500 | # see if disk_number is empty. if not, set to wildcard.
501 | if [[ -z "$disk_number" ]]; then
502 | disk_number='.@'$disk_extension''
503 | fi
504 |
505 | # enable extended globbing
506 | shopt -s extglob
507 |
508 | # get the most recent vdisk file.
509 | for diskimage in "$backup_location/$vm/"*"$disk_number"
510 | do
511 | [[ $diskimage -nt $newest_vdisk_file ]] && newest_vdisk_file=$diskimage
512 | done
513 |
514 | # disable extended globbing
515 | shopt -u extglob
516 |
517 | # check to see if a backup already exists for this vdisk and make a copy of it before shutting down the guest.
518 | if [[ -f "$newest_vdisk_file" ]]; then
519 | log_message "information: copy of backup of $newest_vdisk_file vdisk to $backup_location/$vm/$timestamp$new_disk starting." "script starting copy $vm backup" "normal"
520 |
521 | # call function copy_files to copy existing backup
522 | copy_file "$newest_vdisk_file" "$backup_location/$vm/$timestamp$new_disk" "$rsync_dry_run_option" "sparse" "$rsync_only"
523 |
524 | # make sure copy has current date/time for modified attribute so that removing old backups by date will work.
525 | touch -d "now" "$backup_location/$vm/$timestamp$new_disk"
526 | fi
527 | fi
528 |
529 | elif [ "$mode" == "source_image" ]; then
530 |
531 | # skip the vdisk if skip_disk is set to 1
532 | if [ "$skip_disk" -ne 1 ]; then
533 |
534 | # add the extension of the disk being backed up to an array of vdisk extensions if it doesn't already exist
535 | # set variable extension_exists to false.
536 | extension_exists=false
537 |
538 | # for each extension check to see if it is already in the array.
539 | for extension in "${vdisk_extensions[@]}"
540 | do
541 |
542 | # if the extension already exists in the array set extension_exists to true and break out of the current loop.
543 | if [ "$extension" == "$disk_extension" ]; then
544 | extension_exists=true
545 | break
546 | fi
547 | done
548 |
549 | # if the extension was not found in the array add it.
550 | if [ "$extension_exists" = false ]; then
551 | vdisk_extensions+=("$disk_extension")
552 | fi
553 |
554 | # check to see if snapshots should be used, and if the vm is running.
555 | if [ "$use_snapshots" -eq 1 ] && [ "$vm_state" == "running" ]; then
556 | snapshot_using_traditional_backup=false
557 | log_message "information: able to perform snapshot for disk $disk on $vm. use_snapshots is $use_snapshots. vm_state is $vm_state. vdisk_type is ${vdisk_types[$disk]}"
558 |
559 | # set variable for qemu agent is installed.
560 | qemu_agent_installed=$(virsh qemu-agent-command "$vm" '{"execute":"guest-info"}' | grep -c "version" | awk '{ print $0 }')
561 |
562 | # get directory of current disk.
563 | disk_directory=$(dirname "$disk")
564 |
565 | # remove trailing slash.
566 | disk_directory=${disk_directory%/}
567 |
568 | # get name of current disk without extension and add snapshot extension.
569 | snap_name="${new_disk%.*}.$snapshot_extension"
570 |
571 | # check to see if qemu agent is installed for snapshot creation command.
572 | if [[ "$qemu_agent_installed" -eq 1 ]]; then
573 |
574 | # set quiesce to enabled.
575 | quiesce="--quiesce"
576 | log_message "information: qemu agent found. enabling quiesce on snapshot."
577 |
578 | else
579 |
580 | # set quiesce to disabled.
581 | quiesce=""
582 | log_message "information: qemu agent not found. disabling quiesce on snapshot."
583 | fi
584 |
585 | # create snapshot command.
586 | # unset array for snapshot_cmd.
587 | unset snapshot_cmd
588 | # initialize snapshot_cmd as empty array.
589 | snapshot_cmd=()
590 |
591 | # find each vdisk_spec and use it to build a snapshot command.
592 | for vdisk_spec in "${vdisk_specs[@]}"
593 | do
594 |
595 | # check to see if snapshot command is empty.
596 | if [ ${#snapshot_cmd[@]} -eq 0 ]; then
597 |
598 | # build intial snapshot command.
599 | snapshot_cmd=(virsh)
600 | snapshot_cmd+=(snapshot-create-as)
601 | snapshot_cmd+=(--domain "$vm")
602 | snapshot_cmd+=(--name "$vm-$snap_name")
603 |
604 | # check to see if this is the vdisk we are currently working with.
605 | if [ "$vdisk_spec" == "${vdisk_specs[$disk]}" ]; then
606 |
607 | # if it is, set the command to make a snapshot.
608 | snapshot_cmd+=(--diskspec "$vdisk_spec,file=$disk_directory/$snap_name,snapshot=external")
609 |
610 | else
611 |
612 | # if it is not, set the command to not make a snapshot.
613 | snapshot_cmd+=(--diskspec "$vdisk_spec,snapshot=no")
614 | fi
615 |
616 | else
617 |
618 | # add additional extensions to snapshot command.
619 | # check to see if this is the vdisk we are currently working with.
620 | if [ "$vdisk_spec" == "${vdisk_specs[$disk]}" ]; then
621 |
622 | # if it is, set the command to make a snapshot.
623 | snapshot_cmd+=(--diskspec "$vdisk_spec,file=$disk_directory/$snap_name,snapshot=external")
624 |
625 | else
626 |
627 | # if it is not, set the command to not make a snapshot.
628 | snapshot_cmd+=(--diskspec "$vdisk_spec,snapshot=no")
629 | fi
630 | fi
631 | done
632 |
633 | # add additonal options to snapshot command.
634 | snapshot_cmd+=(--disk-only)
635 | snapshot_cmd+=(--no-metadata)
636 | snapshot_cmd+=(--atomic)
637 |
638 | # check to see if snapshot command should include --quiesce.
639 | if [[ -n "$quiesce" ]]; then
640 | snapshot_cmd+=("$quiesce")
641 | fi
642 |
643 | # create snapshot.
644 | if ! "${snapshot_cmd[@]}"; then
645 | snapshot_succeeded=false
646 | log_message "failure: snapshot command failed on $snap_name for $vm." "$vm snapshot failed" "alert"
647 |
648 | # attempt backup using fallback method.
649 | if [ "$snapshot_fallback" -eq 1 ]; then
650 | log_message "warning: snapshot_fallback is $snapshot_fallback. attempting backup for $vm using fallback method." "$vm fallback backup" "warning"
651 |
652 | # get the state of the vm for making sure it is off before backing up.
653 | vm_state=$(virsh domstate "$vm")
654 |
655 | # get the state of the vm for putting the VM in it's original state after backing up.
656 | vm_original_state=$vm_state
657 |
658 | # initialize skip_vm_shutdown variable as false.
659 | skip_vm_shutdown=false
660 |
661 | # determine if vm should be kept running.
662 | # first check to see if vm exists in vms_to_backup_running variable.
663 | for vm_to_keep_running in $vms_to_backup_running
664 | do
665 |
666 | if [[ "$vm_to_keep_running" == "$vm" ]]; then
667 | skip_vm_shutdown=true
668 | fi
669 | done
670 |
671 | # if vm is not found in vms_to_backup_running and use_snapshots is disabled, then skip shutdown proceedure.
672 | if [ "$skip_vm_shutdown" = false ]; then
673 | log_message "information: skip_vm_shutdown is false. beginning vm shutdown procedure."
674 |
675 | # prepare vm for backup.
676 | prepare_vm "$vm" "$vm_state"
677 |
678 | elif [ "$skip_vm_shutdown" = true ]; then
679 | can_backup_vm="y"
680 | log_message "information: skip_vm_shutdown is $skip_vm_shutdown and use_snapshots is $use_snapshots. skipping vm shutdown procedure. $vm is $vm_state. can_backup_vm set to $can_backup_vm."
681 |
682 | else
683 |
684 | log_message "failure: skip_vm_shutdown is $skip_vm_shutdown and use_snapshots is $use_snapshots. skipping vm shutdown procedure. $vm is $vm_state. can_backup_vm set to $can_backup_vm." "$vm backup failed" "alert"
685 | fi
686 |
687 | # break out of current vdisk loop to prevent potential data loss.
688 | else
689 |
690 | log_message "failure: snapshot_fallback is $snapshot_fallback. skipping backup for $vm to prevent data loss. no cleanup will be performed for this vm." "$vm snapshot failed" "alert"
691 | break
692 | fi
693 |
694 | else
695 |
696 | snapshot_succeeded=true
697 | log_message "information: snapshot command succeeded on $snap_name for $vm."
698 | fi
699 |
700 | elif [ "$use_snapshots" -eq 1 ] && [ ! "$vm_state" == "running" ]; then
701 | snapshot_using_traditional_backup=true
702 | log_message "information: unable to perform snapshot for disk $disk on $vm. falling back to traditional backup. use_snapshots is $use_snapshots. vm_state is $vm_state. vdisk_type is ${vdisk_types[$disk]}."
703 | fi
704 |
705 | local dest_disk="$backup_location/$vm/$new_disk"
706 |
707 | if [ "$timestamp_files" -eq 1 ] || [ "$number_of_backups_to_keep" -ne 1 ]; then
708 | # if timestamps are enabled or more than one backup is being kept, add a timestamp to the destination filename.
709 | dest_disk="$backup_location/$vm/$timestamp$new_disk"
710 | fi
711 |
712 | # copy or pretend to copy the vdisk to the backup location specified by the user.
713 | if [ "$inline_zstd_compress" -eq 1 ]; then
714 | # if inline_zstd_compress is enabled, add ".zst" to the destination filename, and don't run the comparison.
715 | dest_disk+=".zst"
716 | copy_file "$disk" "$dest_disk" "$rsync_dry_run_option" "inline_zstd_compress" "$rsync_only"
717 | else
718 |
719 | if [ -f "$dest_disk" ]; then
720 | # if there is an existing backup, use rsync to just copy the changes (i.e. delta_sync).
721 | copy_file "$disk" "$dest_disk" "$rsync_dry_run_option" "inplace" "$rsync_only"
722 | else
723 | # otherwise, copy the whole file (with sparse support)
724 | copy_file "$disk" "$dest_disk" "$rsync_dry_run_option" "sparse" "$rsync_only"
725 | fi
726 |
727 | # run compare function. compare will not run if compare_files is disabled.
728 | run_compare "$disk" "$dest_disk" "vdisk"
729 | fi
730 |
731 | # make sure the copy has the current date/time for the modified attribute so that removing old backups by date will work.
732 | touch -d "now" "$dest_disk"
733 |
734 | # send a message to the user based on whether there was an actual copy or a dry-run.
735 | if [ "$actually_copy_files" -eq 0 ]; then
736 | log_message "information: dry-run backup of $disk vdisk to $dest_disk complete."
737 | else
738 | log_message "information: backup of $disk vdisk to $dest_disk complete."
739 | fi
740 |
741 | # check to see if snapshot was created.
742 | if [[ "$snapshot_succeeded" = true ]] && [[ ! "$snapshot_using_traditional_backup" = true ]]; then
743 |
744 | # verify vm is still running before attempting to commit changes from snapshot.
745 | if [ "$vm_state" == "running" ]; then
746 |
747 | # commit changes from snapshot.
748 | virsh blockcommit "$vm" "${vdisk_specs[$disk]}" --active --wait --verbose --pivot
749 |
750 | # wait 5 seconds.
751 | sleep 5
752 | log_message "information: commited changes from snapshot for $disk on $vm."
753 |
754 | # see if snapshot still exists.
755 | if [[ -f "$disk_directory/$snap_name" ]]; then
756 |
757 | # if it does, forcibly remove it.
758 | rm -fv "$disk_directory/$snap_name"
759 | log_message "information: forcibly removed snapshot $disk_directory/$snap_name for $vm."
760 | fi
761 |
762 | else
763 |
764 | log_message "warning: snapshot performed for $vm, but vm state is $vm_state. cannot commit changes from snapshot." "script skipping $vm" "warning"
765 | fi
766 | fi
767 | fi
768 | fi
769 | fi
770 | done
771 | }
772 |
773 | # build vdisk_extensions_find_cmd.
774 | build_vdisk_extensions_find_cmd () {
775 |
776 | # assign arguments to local variables for readability.
777 | local search_directory="$1"
778 |
779 | # unset array vdisk_extensions_find_cmd.
780 | unset vdisk_extensions_find_cmd
781 | # initialize vdisk_extensions_find_cmd as empty array.
782 | vdisk_extensions_find_cmd=()
783 |
784 | # find each vdisk extension and use it to build a find command.
785 | for extension in "${vdisk_extensions[@]}"
786 | do
787 |
788 | local _ext="$extension"
789 | if [ "$inline_zstd_compress" -eq 1 ]; then
790 | _ext+=".zst"
791 | fi
792 |
793 | # check to see if find command is empty.
794 | if [ ${#vdisk_extensions_find_cmd[@]} -eq 0 ]; then
795 |
796 | # build initial find command.
797 | vdisk_extensions_find_cmd=(find)
798 | vdisk_extensions_find_cmd+=("$search_directory")
799 | vdisk_extensions_find_cmd+=(-type f)
800 | vdisk_extensions_find_cmd+=(\()
801 | vdisk_extensions_find_cmd+=(-name '*.'"$_ext")
802 |
803 | else
804 |
805 | # add additional extensions to find command.
806 | vdisk_extensions_find_cmd+=(-or -name '*.'"$_ext")
807 | fi
808 | done
809 |
810 | # put closing parenthesis on find command.
811 | vdisk_extensions_find_cmd+=(\))
812 | }
813 |
814 | # build remove_old_files_cmd.
815 | build_remove_old_files_cmd () {
816 |
817 | # assign arguments to local variables for readability.
818 | local search_directory="$1"
819 |
820 | # remove config, nvram, and image files that were compressed.
821 | # unset array remove_old_files_cmd.
822 | unset remove_old_files_cmd
823 | # initialize remove_old_files_cmd as empty array.
824 | remove_old_files_cmd=()
825 |
826 | # find each vdisk extension and use it to build a remove command.
827 | for extension in "${vdisk_extensions[@]}"
828 | do
829 |
830 | # check to see if remove command is empty.
831 | if [ ${#remove_old_files_cmd[@]} -eq 0 ]; then
832 |
833 | # build intial remove command.
834 | remove_old_files_cmd=(find)
835 | remove_old_files_cmd+=("$search_directory")
836 | remove_old_files_cmd+=(-type f)
837 | remove_old_files_cmd+=(\()
838 | remove_old_files_cmd+=(-name '*.'"$extension")
839 |
840 | else
841 |
842 | # add additional extensions to remove command.
843 | remove_old_files_cmd+=(-or -name '*.'"$extension")
844 | fi
845 | done
846 |
847 | # add config files to remove command.
848 | remove_old_files_cmd+=(-or -name '*.xml')
849 |
850 | # add nvram files to remove command.
851 | remove_old_files_cmd+=(-or -name '*.'"$nvram_extension")
852 |
853 | # put closing parenthesis on remove command.
854 | remove_old_files_cmd+=(\))
855 |
856 | # add delete command to remove command.
857 | remove_old_files_cmd+=(-delete)
858 | }
859 |
860 | # remove old files
861 | remove_old_files () {
862 |
863 | # assign arguments to local variables for readability.
864 | local -n _find_cmd=$1
865 | local _type="$2"
866 |
867 | # create variable equal to number_of_days_to_keep_backups plus one to make sure that there are files younger than the cutoff date.
868 | local _days_plus_one=$((number_of_days_to_keep_backups + 1))
869 | local _days_to_mins=$((24*60))
870 |
871 | local _deleted_files
872 | if [[ -n $("${_find_cmd[@]}" -mmin -$((_days_plus_one*_days_to_mins))) ]]; then
873 | _deleted_files=$("${_find_cmd[@]}" -mmin +$((number_of_days_to_keep_backups*_days_to_mins)) -delete -print)
874 | fi
875 | if [[ -n "$_deleted_files" ]]; then
876 | for _deleted_file in $_deleted_files
877 | do
878 | log_message "information: $_deleted_file $_type file." "script removing $_type file" "normal"
879 | done
880 | else
881 | log_message "information: did not find any $_type files to remove." "script removing $_type file" "normal"
882 | fi
883 | }
884 |
885 | # remove over limit files
886 | remove_over_limit_files () {
887 |
888 | # assign arguments to local variables for readability.
889 | local -n _find_cmd=$1
890 | local _number_of_files_to_keep="$2"
891 | local _type="$3"
892 |
893 | local _deleted_files
894 | _deleted_files=$("${_find_cmd[@]}" -printf '%T@\t%p\n' | sort -t $'\t' -gr | tail -n +$((_number_of_files_to_keep + 1)) | cut -d $'\t' -f 2- | xargs -d '\n' -r rm -fv --)
895 | if [[ -n "$_deleted_files" ]]; then
896 | for _deleted_file in $_deleted_files
897 | do
898 | log_message "information: $_deleted_file $_type file." "script removing $_type file" "normal"
899 | done
900 | else
901 | log_message "information: did not find any $_type files to remove." "script removing $_type file" "normal"
902 | fi
903 | }
904 |
905 | # prepare vm for backup.
906 | prepare_vm () {
907 |
908 | # assign arguments to local variables for readability.
909 | local vm="$1"
910 | local vm_state="$2"
911 |
912 | if [ "$pause_vms" -eq 1 ]; then
913 | vm_desired_state="paused"
914 | else
915 | vm_desired_state="shut off"
916 | fi
917 |
918 | # check to see if the vm is in the desired state.
919 | if [ "$vm_state" == "$vm_desired_state" ]; then
920 |
921 | # set a flag to check later to indicate whether to backup this vm or not.
922 | can_backup_vm="y"
923 | log_message "information: $vm is $vm_state. vm desired state is $vm_desired_state. can_backup_vm set to $can_backup_vm."
924 |
925 | # check to see if vm is shut off.
926 | elif [ "$vm_state" == "shut off" ] && [ ! "$vm_desired_state" == "shut off" ]; then
927 |
928 | # set a flag to check later to indicate whether to backup this vm or not.
929 | can_backup_vm="y"
930 | log_message "information: $vm is $vm_state. vm desired state is $vm_desired_state. can_backup_vm set to $can_backup_vm."
931 |
932 | # if the vm is running, try to get it to the desired state.
933 | elif [ "$vm_state" == "running" ] || { [ "$vm_state" == "paused" ] && [ ! "$vm_desired_state" == "paused" ]; }; then
934 | log_message "infomration: $vm is $vm_state. vm desired state is $vm_desired_state."
935 |
936 | if [ "$vm_desired_state" == "paused" ]; then
937 | # attempt to pause the vm.
938 | virsh suspend "$vm"
939 | log_message "information: $vm is $vm_state. vm desired state is $vm_desired_state. performing $clean_shutdown_checks $seconds_to_wait second cycles waiting for $vm to pause. "
940 |
941 | elif [ "$vm_desired_state" == "shut off" ]; then
942 |
943 | # resume the vm if it is suspended, based on testing this should be instant but will trap later if it has not resumed.
944 | if [ "$vm_state" == "paused" ]; then
945 | log_message "action: $vm is $vm_state. vm desired state is $vm_desired_state. resuming."
946 |
947 | # resume the vm.
948 | virsh resume "$vm"
949 | fi
950 |
951 | # attempt to cleanly shutdown the vm.
952 | virsh shutdown "$vm"
953 | log_message "information: performing $clean_shutdown_checks $seconds_to_wait second cycles waiting for $vm to shutdown cleanly."
954 | fi
955 |
956 | # the shutdown of the vm may last a while so we are going to check periodically based on global input variables.
957 | for (( i=1; i<=clean_shutdown_checks; i++ ))
958 | do
959 | log_message "information: cycle $i of $clean_shutdown_checks: waiting $seconds_to_wait seconds before checking if the vm has entered the desired state."
960 |
961 | # wait x seconds based on how many seconds the user wants to wait between checks for a clean shutdown.
962 | sleep $seconds_to_wait
963 |
964 | # get the state of the vm.
965 | vm_state=$(virsh domstate "$vm")
966 |
967 | # if the vm is running decide what to do.
968 | if [ ! "$vm_state" == "$vm_desired_state" ]; then
969 | log_message "information: $vm is $vm_state. vm desired state is $vm_desired_state."
970 |
971 | # if we have already exhausted our wait time set by the script variables then its time to do something else.
972 | if [ $i = "$clean_shutdown_checks" ] ; then
973 |
974 | # check if the user wants to kill the vm on failure of unclean shutdown.
975 | if [ "$kill_vm_if_cant_shutdown" -eq 1 ]; then
976 | log_message "information: kill_vm_if_cant_shutdown is $kill_vm_if_cant_shutdown. killing vm."
977 |
978 | # destroy vm, based on testing this should be instant and without failure.
979 | virsh destroy "$vm"
980 |
981 | # get the state of the vm.
982 | vm_state=$(virsh domstate "$vm")
983 |
984 | # if the vm is shut off then proceed or give up.
985 | if [ "$vm_state" == "shut off" ]; then
986 |
987 | # set a flag to check later to indicate whether to backup this vm or not.
988 | can_backup_vm="y"
989 | log_message "information: $vm is $vm_state. vm desired state is $vm_desired_state. can_backup_vm set to $can_backup_vm."
990 | break
991 |
992 | else
993 |
994 | # set a flag to check later to indicate whether to backup this vm or not.
995 | can_backup_vm="n"
996 | log_message "failure: $vm is $vm_state. vm desired state is $vm_desired_state. can_backup_vm set to $can_backup_vm." "$vm backup failed" "alert"
997 | fi
998 |
999 | # if the user doesn't want to force a shutdown then there is nothing more to do so i cannot backup the vm.
1000 | else
1001 |
1002 | # set a flag to check later to indicate whether to backup this vm or not.
1003 | can_backup_vm="n"
1004 | log_message "failure: $vm is $vm_state. vm desired state is $vm_desired_state. can_backup_vm set to $can_backup_vm." "$vm backup failed" "alert"
1005 | fi
1006 | fi
1007 |
1008 | # if the vm is shut off then go onto backing it up.
1009 | elif [ "$vm_state" == "$vm_desired_state" ]; then
1010 |
1011 | # set a flag to check later to indicate whether to backup this vm or not.
1012 | can_backup_vm="y"
1013 | log_message "information: $vm is $vm_state. vm desired state is $vm_desired_state. can_backup_vm set to $can_backup_vm."
1014 | break
1015 |
1016 | # if the vm is in a state that is not explicitly defined then do nothing as it is unknown how to handle it.
1017 | else
1018 |
1019 | # set a flag to check later to indicate whether to backup this vm or not.
1020 | can_backup_vm="n"
1021 | log_message "failure: $vm is $vm_state. vm desired state is $vm_desired_state. can_backup_vm set to $can_backup_vm." "$vm backup failed" "alert"
1022 | fi
1023 | done
1024 |
1025 | # if the vm is suspended then something went wrong with the attempt to recover it earlier so do not attempt to backup.
1026 | elif [ "$vm_state" == "suspended" ]; then
1027 |
1028 | # set a flag to check later to indicate whether to backup this vm or not.
1029 | can_backup_vm="n"
1030 | log_message "failure: $vm is $vm_state. vm desired state is $vm_desired_state. can_backup_vm set to $can_backup_vm." "$vm backup failed" "alert"
1031 |
1032 | # if the vm is in a state that has not been explicitly defined then do nothing as it is unknown how to handle it.
1033 | else
1034 |
1035 | # set a flag to check later to indicate whether to backup this vm or not.
1036 | can_backup_vm="n"
1037 | log_message "failure: $vm is $vm_state. vm desired state is $vm_desired_state. can_backup_vm set to $can_backup_vm." "$vm backup failed" "alert"
1038 | fi
1039 | }
1040 |
1041 | #### define functions end ####
1042 |
1043 |
1044 | #### validate user variables start ####
1045 |
1046 | # check the name of the script is as it should be. if yes, continue. if no, exit.
1047 | if [ "$me" == "$official_script_name" ]; then
1048 |
1049 | notification_message "information: official_script_name is $official_script_name. script file's name is $me. script name is valid. continuing."
1050 |
1051 | elif [ ! "$me" == "$official_script_name" ]; then
1052 |
1053 | notification_message "failure: official_script_name is $official_script_name. script file's name is $me. script name is invalid. exiting." "script failed" "alert"
1054 |
1055 | exit 1
1056 |
1057 | fi
1058 |
1059 |
1060 | # check to see if the script has been enabled or disabled by the user. if yes, continue if no, exit. if input invalid, exit.
1061 | if [[ "$enabled" =~ ^(0|1)$ ]]; then
1062 |
1063 | if [ "$enabled" -eq 1 ]; then
1064 |
1065 | notification_message "information: enabled is $enabled. script is enabled. continuing."
1066 |
1067 | elif [ ! "$enabled" -eq 1 ]; then
1068 |
1069 | notification_message "failure: enabled is $enabled. script is disabled. exiting." "script failed" "alert"
1070 |
1071 | exit 1
1072 |
1073 | fi
1074 | else
1075 |
1076 | notification_message "failure: enabled is $enabled. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1077 |
1078 | exit 1
1079 |
1080 | fi
1081 |
1082 | # remove the trailing slash from backup_location if it exists.
1083 | backup_location=${backup_location%/}
1084 |
1085 | # check to see if the backup_location specified by the user exists. if yes, continue if no, exit. if exists check if writable, if yes continue, if not exit. if input invalid, exit.
1086 | if [[ -d "$backup_location" ]]; then
1087 |
1088 | notification_message "information: backup_location is $backup_location. this location exists. continuing."
1089 |
1090 | # if backup_location does exist check to see if the backup_location is writable.
1091 | if [[ -w "$backup_location" ]]; then
1092 |
1093 | notification_message "information: backup_location is $backup_location. this location is writable. continuing."
1094 |
1095 | else
1096 |
1097 | notification_message "failure: backup_location is $backup_location. this location is not writable. exiting." "script failed" "alert"
1098 |
1099 | exit 1
1100 |
1101 | fi
1102 |
1103 | else
1104 |
1105 | notification_message "failure: backup_location is $backup_location. this location does not exist. exiting." "script failed" "alert"
1106 |
1107 | exit 1
1108 |
1109 | fi
1110 |
1111 |
1112 | # create timestamp variable for rolling backups.
1113 | timestamp="$(date '+%Y%m%d_%H%M')""_"
1114 |
1115 | # check to see if backups should have timestamp. if yes, continue. if no, continue. if input invalid, exit.
1116 | if [[ "$timestamp_files" =~ ^(0|1)$ ]]; then
1117 |
1118 | if [ "$timestamp_files" -eq 0 ]; then
1119 |
1120 | notification_message "information: timestamp_files is $timestamp_files. this variable is only used when number_of_backups_to_keep is set to 1. timestamp will not be added to backup files."
1121 |
1122 | elif [ "$timestamp_files" -eq 1 ]; then
1123 |
1124 | notification_message "information: timestamp_files is $timestamp_files. timestamp will be added to backup files."
1125 |
1126 | fi
1127 |
1128 | else
1129 |
1130 | notification_message "failure: timestamp_files is $timestamp_files. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1131 |
1132 | exit 1
1133 |
1134 | fi
1135 |
1136 |
1137 | # check log folder for trailing slash. add if missing.
1138 | length=${#log_file_subfolder}
1139 | last_char=${log_file_subfolder:$length-1:1}
1140 | [[ ! $last_char == "/" ]] && log_file_subfolder="$log_file_subfolder/"; :
1141 |
1142 |
1143 | # create the log file subfolder for storing log files.
1144 | if [ ! -d "$backup_location/$log_file_subfolder" ] ; then
1145 |
1146 | notification_message "information: $backup_location/$log_file_subfolder does not exist. creating it."
1147 |
1148 | # make the directory as it doesn't exist. added -v option to give a confirmation message to command line.
1149 | mkdir -vp "$backup_location/$log_file_subfolder"
1150 |
1151 | else
1152 |
1153 | notification_message "information: $backup_location/$log_file_subfolder exists. continuing."
1154 |
1155 | fi
1156 |
1157 |
1158 | # check to see if the log_file_subfolder specified by the user exists. if yes, continue if no, exit. if exists check if writable, if yes continue, if not exit. if input invalid, exit.
1159 | if [[ -d "$backup_location/$log_file_subfolder" ]]; then
1160 |
1161 | notification_message "information: log_file_subfolder is $backup_location/$log_file_subfolder. this location exists. continuing."
1162 |
1163 | # if log_file_subfolder does exist check to see if the log_file_subfolder is writable.
1164 | if [[ -w "$backup_location/$log_file_subfolder" ]]; then
1165 |
1166 | notification_message "information: log_file_subfolder is $backup_location/$log_file_subfolder. this location is writable. continuing."
1167 |
1168 | else
1169 |
1170 | notification_message "failure: log_file_subfolder is $backup_location/$log_file_subfolder. this location is not writable. exiting." "script failed" "alert"
1171 |
1172 | exit 1
1173 |
1174 | fi
1175 |
1176 | else
1177 |
1178 | notification_message "failure: log_file_subfolder is $backup_location/$log_file_subfolder. this location does not exist. exiting." "script failed" "alert"
1179 |
1180 | exit 1
1181 |
1182 | fi
1183 |
1184 |
1185 | # initialize error variable. assume no errors.
1186 | errors="0"
1187 |
1188 |
1189 | ### Logging Started ###
1190 | log_message "Start logging to log file."
1191 |
1192 |
1193 | #### logging and notifications ####
1194 |
1195 | # check to see if notifications should be sent. if yes, continue. if no, continue. if input invalid, exit.
1196 | if [[ "$send_notifications" =~ ^(0|1)$ ]]; then
1197 |
1198 | if [ "$send_notifications" -eq 0 ]; then
1199 |
1200 | log_message "information: send_notifications is $send_notifications. notifications will not be sent."
1201 |
1202 | elif [ "$send_notifications" -eq 1 ]; then
1203 |
1204 | log_message "information: send_notifications is $send_notifications. notifications will be sent."
1205 |
1206 | fi
1207 |
1208 | else
1209 |
1210 | log_message "failure: send_notifications is $send_notifications. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1211 |
1212 | exit 1
1213 |
1214 | fi
1215 |
1216 | # check to see if only error notifications should be sent. if yes, continue. if no, continue. if input invalid, exit.
1217 | if [[ "$only_send_error_notifications" =~ ^(0|1)$ ]]; then
1218 |
1219 | if [ "$only_send_error_notifications" -eq 0 ]; then
1220 |
1221 | log_message "information: only_send_error_notifications is $only_send_error_notifications. normal notifications will be sent if send_notifications is enabled."
1222 |
1223 | elif [ "$only_send_error_notifications" -eq 1 ]; then
1224 |
1225 | log_message "information: only_send_error_notifications is $only_send_error_notifications. only error notifications will be sent if send_notifications is enabled."
1226 |
1227 | fi
1228 |
1229 | else
1230 |
1231 | log_message "failure: only_send_error_notifications is $only_send_error_notifications. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1232 |
1233 | exit 1
1234 |
1235 | fi
1236 |
1237 | # notify user that script has started.
1238 | if [ "$send_notifications" -eq 1 ] && [ "$only_send_error_notifications" -eq 0 ]; then
1239 |
1240 | notification_message "information: unRAID VM Backup script is starting. Look for finished message." "script starting" "normal"
1241 |
1242 | fi
1243 |
1244 |
1245 | # check to see if log files should be kept. if yes, continue. if no, continue. if input invalid, exit.
1246 | if [[ "$keep_log_file" =~ ^(0|1)$ ]]; then
1247 |
1248 | if [ "$keep_log_file" -eq 0 ]; then
1249 |
1250 | log_message "information: keep_log_file is $keep_log_file. log files will not be kept."
1251 |
1252 | elif [ "$keep_log_file" -eq 1 ]; then
1253 |
1254 | log_message "information: keep_log_file is $keep_log_file. log files will be kept."
1255 |
1256 | fi
1257 |
1258 | else
1259 |
1260 | log_message "failure: keep_log_file is $keep_log_file. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1261 |
1262 | exit 1
1263 |
1264 | fi
1265 |
1266 |
1267 | # check to see how many log files should be kept. if yes, continue if no, continue if input invalid, exit.
1268 | if [[ "$number_of_log_files_to_keep" =~ ^[0-9]+$ ]]; then
1269 |
1270 | if [ "$number_of_log_files_to_keep" -eq 0 ]; then
1271 |
1272 | log_message "information: number_of_log_files_to_keep is $number_of_log_files_to_keep. an infinite number of log files will be kept. be sure to pay attention to how many log files there are."
1273 |
1274 | elif [ "$number_of_log_files_to_keep" -gt 40 ]; then
1275 |
1276 | log_message "information: number_of_log_files_to_keep is $number_of_log_files_to_keep. this is a lot of log files to keep."
1277 |
1278 | elif [ "$number_of_log_files_to_keep" -ge 1 ] && [ "$number_of_log_files_to_keep" -le 40 ]; then
1279 |
1280 | log_message "information: number_of_log_files_to_keep is $number_of_log_files_to_keep. this is probably a sufficient number of log files to keep."
1281 |
1282 | fi
1283 |
1284 | else
1285 |
1286 | log_message "failure: number_of_log_files_to_keep is $number_of_log_files_to_keep. this is not a valid format. expecting a number between [0 - 1000000]. exiting." "script failed" "alert"
1287 |
1288 | exit 1
1289 |
1290 | fi
1291 |
1292 | # check to see if vm specific log files are enabled. if yes, continue. if no, continue. if input invalid, exit.
1293 | if [[ "$enable_vm_log_file" =~ ^(0|1)$ ]]; then
1294 |
1295 | if [ "$enable_vm_log_file" -eq 0 ]; then
1296 |
1297 | log_message "information: enable_vm_log_file is $enable_vm_log_file. vm specific logs will not be created."
1298 |
1299 | elif [ "$enable_vm_log_file" -eq 1 ]; then
1300 |
1301 | log_message "information: enable_vm_log_file is $enable_vm_log_file. vm specific logs will be created."
1302 |
1303 | fi
1304 |
1305 | else
1306 |
1307 | log_message "failure: enable_vm_log_file is $enable_vm_log_file. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1308 |
1309 | exit 1
1310 |
1311 | fi
1312 |
1313 | # check to see if all vms should be backed up. if yes, continue. if no, continue. if input invalid, exit.
1314 | if [[ "$backup_all_vms" =~ ^(0|1)$ ]]; then
1315 |
1316 | if [ "$backup_all_vms" -eq 0 ]; then
1317 |
1318 | log_message "information: backup_all_vms is $backup_all_vms. only vms listed in vms_to_backup will be backed up."
1319 |
1320 | elif [ "$backup_all_vms" -eq 1 ]; then
1321 |
1322 | log_message "information: backup_all_vms is $backup_all_vms. vms_to_backup will be ignored. all vms will be backed up."
1323 |
1324 | fi
1325 |
1326 | else
1327 |
1328 | log_message "failure: backup_all_vms is $backup_all_vms. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1329 |
1330 | exit 1
1331 |
1332 | fi
1333 |
1334 | # check to see if snapshots should be used. if yes, continue. if no, continue. if input invalid, exit.
1335 | if [[ "$use_snapshots" =~ ^(0|1)$ ]]; then
1336 |
1337 | if [ "$use_snapshots" -eq 0 ]; then
1338 |
1339 | log_message "information: use_snapshots is $use_snapshots. vms will not be backed up using snapshots."
1340 |
1341 | elif [ "$use_snapshots" -eq 1 ]; then
1342 |
1343 | log_message "information: use_snapshots is $use_snapshots. vms will be backed up using snapshots if possible."
1344 |
1345 | fi
1346 |
1347 | else
1348 |
1349 | log_message "failure: use_snapshots is $use_snapshots. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1350 |
1351 | exit 1
1352 |
1353 | fi
1354 |
1355 | # check to see if vm should be killed if clean shutdown fails. if yes, continue. if no, continue. if input invalid, exit.
1356 | if [[ "$kill_vm_if_cant_shutdown" =~ ^(0|1)$ ]]; then
1357 |
1358 | if [ "$kill_vm_if_cant_shutdown" -eq 0 ]; then
1359 |
1360 | log_message "information: kill_vm_if_cant_shutdown is $kill_vm_if_cant_shutdown. vms will not be forced to shutdown if a clean shutdown can not be detected."
1361 |
1362 | elif [ "$kill_vm_if_cant_shutdown" -eq 1 ]; then
1363 |
1364 | log_message "information: kill_vm_if_cant_shutdown is $kill_vm_if_cant_shutdown. vms will be forced to shutdown if a clean shutdown can not be detected."
1365 |
1366 | fi
1367 |
1368 | else
1369 |
1370 | log_message "failure: kill_vm_if_cant_shutdown is $kill_vm_if_cant_shutdown. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1371 |
1372 | exit 1
1373 |
1374 | fi
1375 |
1376 |
1377 | # check to see if vm should be set to original state after backup. if yes, continue. if no, continue. if input invalid, exit.
1378 | if [[ "$set_vm_to_original_state" =~ ^(0|1)$ ]]; then
1379 |
1380 | if [ "$set_vm_to_original_state" -eq 0 ]; then
1381 |
1382 | log_message "information: set_vm_to_original_state is $set_vm_to_original_state. vms will not be set to their original state after backup."
1383 |
1384 | elif [ "$set_vm_to_original_state" -eq 1 ]; then
1385 |
1386 | log_message "information: set_vm_to_original_state is $set_vm_to_original_state. vms will be set to their original state after backup."
1387 |
1388 | fi
1389 |
1390 | else
1391 |
1392 | log_message "failure: set_vm_to_original_state is $set_vm_to_original_state. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1393 |
1394 | exit 1
1395 |
1396 | fi
1397 |
1398 |
1399 | # check to how many days backups should be kept. if yes, continue. if no, continue. if input invalid, exit.
1400 | if [[ "$number_of_days_to_keep_backups" =~ ^[0-9]+$ ]]; then
1401 |
1402 | if [ "$number_of_days_to_keep_backups" -lt 7 ]; then
1403 |
1404 | if [ "$number_of_days_to_keep_backups" -eq 0 ]; then
1405 |
1406 | log_message "information: number_of_days_to_keep_backups is $number_of_days_to_keep_backups. backups will be kept indefinitely. be sure to set number_of_backups_to_keep to keep backups storage usage down."
1407 |
1408 | else
1409 |
1410 | log_message "warning: number_of_days_to_keep_backups is $number_of_days_to_keep_backups. this is potentially an insufficient number of days to keep your backups."
1411 |
1412 | fi
1413 |
1414 | elif [ "$number_of_days_to_keep_backups" -gt 180 ]; then
1415 |
1416 | log_message "warning: number_of_days_to_keep_backups is $number_of_days_to_keep_backups. this is a long time to keep your backups."
1417 |
1418 | elif [ "$number_of_days_to_keep_backups" -ge 7 ] && [ "$number_of_days_to_keep_backups" -le 180 ]; then
1419 |
1420 | log_message "information: number_of_days_to_keep_backups is $number_of_days_to_keep_backups. this is probably a sufficient number of days to keep your backups."
1421 |
1422 | fi
1423 |
1424 | else
1425 |
1426 | log_message "failure: number_of_days_to_keep_backups is $number_of_days_to_keep_backups. this is not a valid format. expecting a number between [0 - 1000000]. exiting." "script failed" "alert"
1427 |
1428 | exit 1
1429 |
1430 | fi
1431 |
1432 |
1433 | # check to how many backups should be kept. if yes, continue. if no, continue. if input invalid, exit.
1434 | if [[ "$number_of_backups_to_keep" =~ ^[0-9]+$ ]]; then
1435 |
1436 | if [ "$number_of_backups_to_keep" -lt 2 ]; then
1437 |
1438 | if [ "$number_of_backups_to_keep" -eq 0 ]; then
1439 |
1440 | log_message "information: number_of_backups_to_keep is $number_of_backups_to_keep. an infinite number of backups will be kept. be sure to set number_of_days_to_keep_backups to keep backups storage usage down."
1441 |
1442 | else
1443 |
1444 | log_message "warning: number_of_backups_to_keep is $number_of_backups_to_keep. this is potentially an insufficient number of backups to keep."
1445 |
1446 | fi
1447 |
1448 | elif [ "$number_of_backups_to_keep" -gt 40 ]; then
1449 |
1450 | log_message "warning: number_of_backups_to_keep is $number_of_backups_to_keep. this is a lot of backups to keep."
1451 |
1452 | elif [ "$number_of_backups_to_keep" -ge 2 ] && [ "$number_of_backups_to_keep" -le 40 ]; then
1453 |
1454 | log_message "information: number_of_backups_to_keep is $number_of_backups_to_keep. this is probably a sufficient number of backups to keep."
1455 |
1456 | fi
1457 |
1458 | else
1459 |
1460 | log_message "failure: number_of_backups_to_keep is $number_of_backups_to_keep. this is not a valid format. expecting a number between [0 - 1000000]. exiting." "script failed" "alert"
1461 |
1462 | exit 1
1463 |
1464 | fi
1465 |
1466 |
1467 | # check to see if vdisks should be inline compressed.
1468 | if [[ ! "$inline_zstd_compress" =~ ^(0|1)$ ]]; then
1469 | log_message "failure: inline_zstd_compress is $inline_zstd_compress. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1470 | exit 1
1471 | elif [ "$inline_zstd_compress" -eq 0 ]; then
1472 | log_message "information: inline_zstd_compress is $inline_zstd_compress. vdisk images will not be inline compressed."
1473 | elif [ "$inline_zstd_compress" -eq 1 ]; then
1474 | log_message "information: inline_zstd_compress is $inline_zstd_compress. vdisk images will be inline compressed but will not be compared afterwards or post compressed."
1475 | fi
1476 |
1477 | # if inline_zstd_compress is enabled, check to see if zstd_level and zstd_threads are valid and in range.
1478 | if [ "$inline_zstd_compress" -eq 1 ]; then
1479 |
1480 | # check to see if zstd_level is valid and in range.
1481 | if [[ ! "$zstd_level" =~ ^[0-9]+$ ]]; then
1482 | log_message "failure: zstd_level is $zstd_level. this is not a valid format. expecting a number between [1 - 19]. exiting." "script failed" "alert"
1483 | exit 1
1484 | elif [ "$zstd_level" -lt 1 ] || [ "$zstd_level" -gt 19 ] ; then
1485 | log_message "failure: zstd_level is $zstd_level. expecting a number between [1 - 19]. exiting." "script failed" "alert"
1486 | exit 1
1487 | elif [ "$zstd_level" -gt 8 ] ; then
1488 | log_message "warning: zstd_level is $zstd_level. this will be slower and may not produce meaningfully smaller backup images."
1489 | else
1490 | log_message "information: zstd_level is $zstd_level."
1491 | fi
1492 |
1493 | # check to see if zstd_threads is valid and in range.
1494 | if [[ ! "$zstd_threads" =~ ^[0-9]+$ ]]; then
1495 | log_message "failure: zstd_threads is $zstd_threads. this is not a valid format. expecting a number between [0 - 200]. exiting." "script failed" "alert"
1496 | exit 1
1497 | elif [ "$zstd_threads" -lt 0 ] || [ "$zstd_threads" -gt 200 ] ; then
1498 | log_message "failure: zstd_threads is $zstd_threads. expecting a number between [0 - 200]. exiting." "script failed" "alert"
1499 | exit 1
1500 | elif [ "$zstd_threads" -gt 4 ] ; then
1501 | log_message "warning: zstd_threads is $zstd_threads. this is a lot of threads to use for compression."
1502 | elif [ "$zstd_threads" -eq 0 ] ; then
1503 | log_message "information: zstd_threads is $zstd_threads. the actual number of threads will be auto determined."
1504 | else
1505 | log_message "information: zstd_threads is $zstd_threads."
1506 | fi
1507 |
1508 | fi
1509 |
1510 | # if inline_zstd_compress is disabled, check to see if backups should be post compressed.
1511 | if [ "$inline_zstd_compress" -ne 1 ]; then
1512 | if [[ ! "$compress_backups" =~ ^(0|1)$ ]]; then
1513 | log_message "failure: compress_backups is $compress_backups. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1514 | exit 1
1515 | elif [ "$compress_backups" -eq 0 ]; then
1516 | log_message "information: compress_backups is $compress_backups. backups will not be post compressed."
1517 | elif [ "$compress_backups" -eq 1 ]; then
1518 | log_message "information: compress_backups is $compress_backups. backups will be post compressed."
1519 | fi
1520 | fi
1521 |
1522 | #### advanced variables ####
1523 |
1524 | # if snapshots are enabled, then check for a valid extension and add it to the extensions to skip.
1525 | if [ "$use_snapshots" -eq 1 ]; then
1526 |
1527 | # check to see if snapshot extension is empty. if yes, continue. if no exit.
1528 | if [[ -n "$snapshot_extension" ]]; then
1529 |
1530 | # remove any leading decimals from the extension.
1531 | snapshot_extension="${snapshot_extension##.}"
1532 |
1533 | log_message "information: snapshot_extension is $snapshot_extension. continuing."
1534 |
1535 | else
1536 |
1537 | log_message "failure: snapshot_extension is not set. exiting." "script failed" "alert"
1538 |
1539 | exit 1
1540 |
1541 | fi
1542 |
1543 | # add snapshot extension to extensions_to_skip if it is not already present.
1544 | # initialize vairable snap_exists as false
1545 | snap_exists=false
1546 |
1547 | # for each extension check to see if it is already in the array.
1548 | for extension in $vdisk_extensions_to_skip
1549 |
1550 | do
1551 |
1552 | # if the extension already exists in the array set snap_exists to true and break out of the current loop.
1553 | if [ "$extension" == "$snapshot_extension" ]; then
1554 |
1555 | snap_exists=true
1556 | break
1557 |
1558 | fi
1559 |
1560 | done
1561 |
1562 | # if snapshot extension was not found in the array, add it. else move on.
1563 | if [ "$snap_exists" = false ]; then
1564 |
1565 | vdisk_extensions_to_skip="$vdisk_extensions_to_skip"$'\n'"$snapshot_extension"
1566 |
1567 | log_message "information: snaphot extension not found in vdisk_extensions_to_skip. extension was added."
1568 |
1569 | else
1570 |
1571 | log_message "information: snapshot extension was not found in vdisk_extensions_to_skip. moving on."
1572 |
1573 | fi
1574 |
1575 | else
1576 |
1577 | log_message "information: use_snapshots disabled, not adding snapshot_extension to vdisk_extensions_to_skip."
1578 |
1579 | fi
1580 |
1581 | # check to see if snapshots should fallback to standard backups. if yes, continue. if no, continue. if input invalid, exit.
1582 | if [[ "$snapshot_fallback" =~ ^(0|1)$ ]]; then
1583 |
1584 | if [ "$snapshot_fallback" -eq 0 ]; then
1585 |
1586 | log_message "information: snapshot_fallback is $snapshot_fallback. snapshots will fallback to standard backups."
1587 |
1588 | elif [ "$snapshot_fallback" -eq 1 ]; then
1589 |
1590 | log_message "information: snapshot_fallback is $snapshot_fallback. snapshots will not fallback to standard backups."
1591 |
1592 | fi
1593 |
1594 | else
1595 |
1596 | log_message "failure: snapshot_fallback is $snapshot_fallback. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1597 |
1598 | exit 1
1599 |
1600 | fi
1601 |
1602 | # check to see if vms should be paused instead of shutdown. if yes, continue. if no, continue. if input invalid, exit.
1603 | if [[ "$pause_vms" =~ ^(0|1)$ ]]; then
1604 |
1605 | if [ "$pause_vms" -eq 0 ]; then
1606 |
1607 | log_message "information: pause_vms is $pause_vms. vms will be shutdown for standard backups."
1608 |
1609 | elif [ "$pause_vms" -eq 1 ]; then
1610 |
1611 | log_message "information: pause_vms is $pause_vms. vms will be paused for standard backups."
1612 |
1613 | fi
1614 |
1615 | else
1616 |
1617 | log_message "failure: pause_vms is $pause_vms. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1618 |
1619 | exit 1
1620 |
1621 | fi
1622 |
1623 | # check to see if reconstruct write should be enabled during backup. if yes, continue. if no, continue. if input invalid, exit.
1624 | if [[ "$enable_reconstruct_write" =~ ^(0|1)$ ]]; then
1625 |
1626 | if [ "$enable_reconstruct_write" -eq 0 ]; then
1627 |
1628 | log_message "information: enable_reconstruct_write is $enable_reconstruct_write. reconstruct write will not be enabled by this script."
1629 |
1630 | elif [ "$enable_reconstruct_write" -eq 1 ]; then
1631 |
1632 | log_message "information: enable_reconstruct_write is $enable_reconstruct_write. reconstruct write will be enabled during the backup."
1633 |
1634 | fi
1635 |
1636 | else
1637 |
1638 | log_message "failure: enable_reconstruct_write is $enable_reconstruct_write. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1639 |
1640 | exit 1
1641 |
1642 | fi
1643 |
1644 | # if inline_zstd_compress is disabled, check to see if files should be compared after backup.
1645 | if [ "$inline_zstd_compress" -ne 1 ]; then
1646 | if [[ ! "$compare_files" =~ ^(0|1)$ ]]; then
1647 | log_message "failure: compare_files is $compare_files. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1648 | exit 1
1649 | elif [ "$compare_files" -eq 0 ]; then
1650 | log_message "information: compare_files is $compare_files. files will not be compared after backups."
1651 | elif [ "$compare_files" -eq 1 ]; then
1652 | log_message "information: compare_files is $compare_files. files will be compared after backups."
1653 | fi
1654 | fi
1655 |
1656 | # check to see if config should be backed up. if yes, continue. if no, continue. if input invalid, exit.
1657 | if [[ "$backup_xml" =~ ^(0|1)$ ]]; then
1658 |
1659 | if [ "$backup_xml" -eq 0 ]; then
1660 |
1661 | log_message "warning: backup_xml is $backup_xml. vms will not have their xml configurations backed up."
1662 |
1663 | elif [ "$backup_xml" -eq 1 ]; then
1664 |
1665 | log_message "information: backup_xml is $backup_xml. vms will have their xml configurations backed up."
1666 |
1667 | fi
1668 |
1669 | else
1670 |
1671 | log_message "failure: backup_xml is $backup_xml. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1672 |
1673 | exit 1
1674 |
1675 | fi
1676 |
1677 |
1678 | # check to see if nvram should be backed up. if yes, continue. if no, continue. if input invalid, exit.
1679 | if [[ "$backup_nvram" =~ ^(0|1)$ ]]; then
1680 |
1681 | if [ "$backup_nvram" -eq 0 ]; then
1682 |
1683 | log_message "warning: backup_nvram is $backup_nvram. vms will not have their nvram backed up."
1684 |
1685 | elif [ "$backup_nvram" -eq 1 ]; then
1686 |
1687 | log_message "information: backup_nvram is $backup_nvram. vms will have their nvram backed up."
1688 |
1689 | fi
1690 |
1691 | else
1692 |
1693 | log_message "failure: backup_nvram is $backup_nvram. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1694 |
1695 | exit 1
1696 |
1697 | fi
1698 |
1699 |
1700 | # check to see if vdisks should be backed up. if yes, continue. if no, continue. if input invalid, exit.
1701 | if [[ "$backup_vdisks" =~ ^(0|1)$ ]]; then
1702 |
1703 | if [ "$backup_vdisks" -eq 0 ]; then
1704 |
1705 | log_message "warning: backup_vdisks is $backup_vdisks. vms will not have their vdisks backed up. compression will be set to off."
1706 |
1707 | compress_backups="0"
1708 |
1709 | elif [ "$backup_vdisks" -eq 1 ]; then
1710 |
1711 | log_message "information: backup_vdisks is $backup_vdisks. vms will have their vdisks backed up."
1712 |
1713 | fi
1714 |
1715 | else
1716 |
1717 | log_message "failure: backup_vdisks is $backup_vdisks. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1718 |
1719 | exit 1
1720 |
1721 | fi
1722 |
1723 |
1724 | # check to see if vms should be started after a successful backup. if yes, continue. if no, continue. if input invalid, exit.
1725 | if [[ "$start_vm_after_backup" =~ ^(0|1)$ ]]; then
1726 |
1727 | if [ "$start_vm_after_backup" -eq 0 ]; then
1728 |
1729 | log_message "information: start_vm_after_backup is $start_vm_after_backup. vms will not be started following successful backup."
1730 |
1731 | elif [ "$start_vm_after_backup" -eq 1 ]; then
1732 |
1733 | log_message "information: start_vm_after_backup is $start_vm_after_backup vms will be started following a successful backup."
1734 |
1735 | fi
1736 |
1737 | else
1738 |
1739 | log_message "failure: start_vm_after_backup is $start_vm_after_backup. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1740 |
1741 | exit 1
1742 |
1743 | fi
1744 |
1745 |
1746 | # check to see if vms should be started after an unsuccessful backup. if yes, continue. if no, continue. if input invalid, exit.
1747 | if [[ "$start_vm_after_failure" =~ ^(0|1)$ ]]; then
1748 |
1749 | if [ "$start_vm_after_failure" -eq 0 ]; then
1750 |
1751 | log_message "information: start_vm_after_failure is $start_vm_after_failure. vms will not be started following an unsuccessful backup."
1752 |
1753 | elif [ "$start_vm_after_failure" -eq 1 ]; then
1754 |
1755 | log_message "information: start_vm_after_failure is $start_vm_after_failure. vms will be started following an unsuccessful backup."
1756 |
1757 | fi
1758 |
1759 | else
1760 |
1761 | log_message "failure: start_vm_after_failure is $start_vm_after_failure. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1762 |
1763 | exit 1
1764 |
1765 | fi
1766 |
1767 |
1768 | # check to see if delta sync should be disabled. if yes, continue. if no, continue. if input invalid, exit.
1769 | if [[ "$disable_delta_sync" =~ ^(0|1)$ ]]; then
1770 |
1771 | if [ "$disable_delta_sync" -eq 0 ]; then
1772 |
1773 | log_message "information: disable_delta_sync is $disable_delta_sync. rsync will be used to perform delta sync backups."
1774 |
1775 | elif [ "$disable_delta_sync" -eq 1 ]; then
1776 |
1777 | log_message "information: disable_delta_sync is $disable_delta_sync. no delta syncs will be done."
1778 |
1779 | fi
1780 |
1781 | else
1782 |
1783 | log_message "failure: disable_delta_sync is $disable_delta_sync. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1784 |
1785 | exit 1
1786 |
1787 | fi
1788 |
1789 |
1790 | # check to see if only rsync should be used. if yes, continue. if no, continue. if input invalid, exit.
1791 | if [[ "$rsync_only" =~ ^(0|1)$ ]]; then
1792 |
1793 | if [ "$rsync_only" -eq 0 ]; then
1794 |
1795 | log_message "information: rsync_only is $rsync_only. cp will be used when applicable."
1796 |
1797 | elif [ "$rsync_only" -eq 1 ]; then
1798 |
1799 | log_message "information: rsync_only is $rsync_only. only rsync will be used."
1800 |
1801 | fi
1802 |
1803 | else
1804 |
1805 | log_message "failure: rsync_only is $rsync_only. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1806 |
1807 | exit 1
1808 |
1809 | fi
1810 |
1811 |
1812 | # validate the actually_copy_files option. if yes set the rsync command line option for dry-run. if input invalid, exit.
1813 | if [[ "$actually_copy_files" =~ ^(0|1)$ ]]; then
1814 |
1815 | if [ "$actually_copy_files" -eq 0 ]; then
1816 |
1817 | log_message "warning: actually_copy_files flag is $actually_copy_files. no files will be copied. setting rsync_only to 1."
1818 |
1819 | # create a variable which tells rsync to do a dry-run.
1820 | rsync_dry_run_option="n"
1821 |
1822 | # set rsync_only variable to 1 so that dry run will work.
1823 | rsync_only="1"
1824 |
1825 | elif [ "$actually_copy_files" -eq 1 ]; then
1826 |
1827 | log_message "information: actually_copy_files is $actually_copy_files. files will be copied."
1828 |
1829 | fi
1830 |
1831 | else
1832 |
1833 | log_message "failure: actually_copy_files is $actually_copy_files. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1834 |
1835 | exit 1
1836 |
1837 | fi
1838 |
1839 |
1840 | # check to see how many times vm's state should be checked for shutdown. if yes, continue if no, continue if input invalid, exit.
1841 | if [[ "$clean_shutdown_checks" =~ ^[0-9]+$ ]]; then
1842 |
1843 | if [ "$clean_shutdown_checks" -lt 5 ]; then
1844 |
1845 | log_message "warning: clean_shutdown_checks is $clean_shutdown_checks. this is potentially an insufficient number of shutdown checks."
1846 |
1847 | elif [ "$clean_shutdown_checks" -gt 50 ]; then
1848 |
1849 | log_message "warning: clean_shutdown_checks is $clean_shutdown_checks. this is a vast number of shutdown checks."
1850 |
1851 | elif [ "$clean_shutdown_checks" -ge 5 ] && [ "$clean_shutdown_checks" -le 50 ]; then
1852 |
1853 | log_message "information: clean_shutdown_checks is $clean_shutdown_checks. this is probably a sufficient number of shutdown checks."
1854 |
1855 | fi
1856 |
1857 | else
1858 |
1859 | log_message "failure: clean_shutdown_checks is $clean_shutdown_checks. this is not a valid format. expecting a number between [0 - 1000000]. exiting." "script failed" "alert"
1860 |
1861 | exit 1
1862 |
1863 | fi
1864 |
1865 |
1866 | # check to see how many seconds to wait between vm shutdown checks. messages to user only. if input invalid, exit.
1867 | if [[ "$seconds_to_wait" =~ ^[0-9]+$ ]]; then
1868 |
1869 | if [ "$seconds_to_wait" -lt 30 ]; then
1870 |
1871 | log_message "warning: seconds_to_wait is $seconds_to_wait. this is potentially an insufficient number of seconds to wait between shutdown checks."
1872 |
1873 | elif [ "$seconds_to_wait" -gt 600 ]; then
1874 |
1875 | log_message "warning: seconds_to_wait is $seconds_to_wait. this is a vast number of seconds to wait between shutdown checks."
1876 |
1877 | elif [ "$seconds_to_wait" -ge 30 ] && [ "$seconds_to_wait" -le 600 ]; then
1878 |
1879 | log_message "information: seconds_to_wait is $seconds_to_wait. this is probably a sufficient number of seconds to wait between shutdown checks."
1880 |
1881 | fi
1882 |
1883 | else
1884 |
1885 | log_message "failure: seconds_to_wait is $seconds_to_wait. this is not a valid format. expecting a number between [0 - 1000000]. exiting." "script failed" "alert"
1886 |
1887 | exit 1
1888 |
1889 | fi
1890 |
1891 |
1892 | # check to see if error log files should be kept. if yes, continue. if no, continue. if input invalid, exit.
1893 | if [[ "$keep_error_log_file" =~ ^(0|1)$ ]]; then
1894 |
1895 | if [ "$keep_error_log_file" -eq 0 ]; then
1896 |
1897 | log_message "information: keep_error_log_file is $keep_error_log_file. error log files will not be kept."
1898 |
1899 | elif [ "$keep_error_log_file" -eq 1 ]; then
1900 |
1901 | log_message "information: keep_error_log_file is $keep_error_log_file. error log files will be kept."
1902 |
1903 | fi
1904 |
1905 | else
1906 |
1907 | log_message "failure: keep_error_log_file is $keep_error_log_file. this is not a valid format. expecting [0 = no] or [1 = yes]. exiting." "script failed" "alert"
1908 |
1909 | exit 1
1910 |
1911 | fi
1912 |
1913 |
1914 | # check to see how many error log files should be kept. if yes, continue if no, continue if input invalid, exit.
1915 | if [[ "$number_of_error_log_files_to_keep" =~ ^[0-9]+$ ]]; then
1916 |
1917 | if [ "$number_of_error_log_files_to_keep" -lt 2 ]; then
1918 |
1919 | if [ "$number_of_error_log_files_to_keep" -eq 0 ]; then
1920 |
1921 | log_message "information: number_of_error_log_files_to_keep is $number_of_error_log_files_to_keep. an infinite number of error log files will be kept. be sure to pay attention to how many error log files there are."
1922 |
1923 | else
1924 |
1925 | log_message "warning: number_of_error_log_files_to_keep is $number_of_error_log_files_to_keep. this is potentially an insufficient number of error log files to keep."
1926 |
1927 | fi
1928 |
1929 | elif [ "$number_of_error_log_files_to_keep" -gt 40 ]; then
1930 |
1931 | log_message "information: number_of_error_log_files_to_keep is $number_of_error_log_files_to_keep. this is a error lot of log files to keep."
1932 |
1933 | elif [ "$number_of_error_log_files_to_keep" -ge 2 ] && [ "$number_of_error_log_files_to_keep" -le 40 ]; then
1934 |
1935 | log_message "information: number_of_error_log_files_to_keep is $number_of_error_log_files_to_keep. this is probably a sufficient error number of log files to keep."
1936 |
1937 | fi
1938 |
1939 | else
1940 |
1941 | log_message "failure: number_of_error_log_files_to_keep is $number_of_error_log_files_to_keep. this is not a valid format. expecting a number between [0 - 1000000]. exiting." "script failed" "alert"
1942 |
1943 | exit 1
1944 |
1945 | fi
1946 |
1947 | #### validate user variables end ####
1948 |
1949 |
1950 | #### code execution start ####
1951 |
1952 | # set this to force the for loop to split on new lines and not spaces.
1953 | IFS=$'\n'
1954 |
1955 | # check to see if backup_all_vms is enabled.
1956 | if [ "$backup_all_vms" -eq 1 ]; then
1957 | # since we are using backup_all_vms, ignore any vms listed in vms_to_backup.
1958 | vms_to_ignore="$vms_to_backup"
1959 |
1960 | # unset vms_to_backup
1961 | unset -v vms_to_backup
1962 |
1963 | # get a list of the vm names installed on the system.
1964 | vm_exists=$(virsh list --all --name)
1965 |
1966 | # check each vm on the system against the list of vms to ignore.
1967 | for vmname in $vm_exists
1968 |
1969 | do
1970 |
1971 | # assume the vm will not be ignored until it is found in the list of vms to ignore.
1972 | ignore_vm=false
1973 |
1974 | for vm in $vms_to_ignore
1975 |
1976 | do
1977 |
1978 | if [ "$vmname" == "$vm" ]; then
1979 |
1980 | # mark the vm as needing to be ignored.
1981 | ignore_vm=true
1982 |
1983 | # skips current loop.
1984 | continue
1985 |
1986 | fi
1987 |
1988 | done
1989 |
1990 | # if vm should not be ignored, add it to the list of vms to backup.
1991 | if [[ "$ignore_vm" = false ]]; then
1992 |
1993 | if [[ -z "$vms_to_backup" ]]; then
1994 |
1995 | vms_to_backup="$vmname"
1996 |
1997 | else
1998 |
1999 | vms_to_backup="$vms_to_backup"$'\n'"$vmname"
2000 |
2001 | fi
2002 |
2003 | fi
2004 |
2005 | done
2006 |
2007 | fi
2008 |
2009 | # create comma separated list of vms to backup for log file.
2010 | for vm_to_backup in $vms_to_backup
2011 |
2012 | do
2013 |
2014 | if [[ -z "$vms_to_backup_list" ]]; then
2015 |
2016 | vms_to_backup_list="$vm_to_backup"
2017 |
2018 | else
2019 |
2020 | vms_to_backup_list="$vms_to_backup_list, $vm_to_backup"
2021 |
2022 | fi
2023 |
2024 | done
2025 |
2026 | log_message "information: started attempt to backup $vms_to_backup_list to $backup_location"
2027 |
2028 | # check to see if reconstruct write should be enabled by this script. if so, enable and continue.
2029 | if [ "$enable_reconstruct_write" -eq 1 ]; then
2030 |
2031 | /usr/local/sbin/mdcmd set md_write_method 1
2032 | log_message "information: Reconstruct write enabled."
2033 |
2034 | fi
2035 |
2036 | # loop through the vms in the list and try and back up their associated configs and vdisk(s).
2037 | for vm in $vms_to_backup
2038 |
2039 | do
2040 |
2041 | # get a list of the vm names installed on the system.
2042 | vm_exists=$(virsh list --all --name)
2043 |
2044 | # assume the vm is not going to be backed up until it is found on the system
2045 | skip_vm="y"
2046 |
2047 | # check to see if the vm exists on the system to backup.
2048 | for vmname in $vm_exists
2049 |
2050 | do
2051 |
2052 | # if the vm doesn't match then set the skip flag to y.
2053 | if [ "$vm" == "$vmname" ] ; then
2054 |
2055 | # set a flag i am going to check later to indicate if i should skip this vm or not.
2056 | skip_vm="n"
2057 |
2058 | # skips current loop.
2059 | continue
2060 |
2061 | fi
2062 |
2063 | done
2064 |
2065 |
2066 | # if the skip flag was set in the previous section then we have to exit and move on to the next vm in the list.
2067 | if [ "$skip_vm" == "y" ]; then
2068 |
2069 | log_message "warning: $vm can not be found on the system. skipping vm." "script skipping $vm" "warning"
2070 |
2071 | skip_vm="n"
2072 |
2073 | # skips current loop.
2074 | continue
2075 |
2076 | else
2077 |
2078 | log_message "information: $vm can be found on the system. attempting backup."
2079 |
2080 | fi
2081 |
2082 | # see if a config file exists for the vm already and remove it.
2083 | if [[ -f "$vm.xml" ]]; then
2084 | log_message "information: removing old local $vm.xml."
2085 | rm -fv "$vm.xml"
2086 | fi
2087 |
2088 | # dump the vm config locally.
2089 | log_message "information: creating local $vm.xml to work with during backup."
2090 | virsh dumpxml "$vm" > "$vm.xml"
2091 |
2092 | # replace xmlns value with absolute URI to avoid namespace warning.
2093 | # sed isn't an ideal way to edit xml files, but few options are available on unraid.
2094 | # this only edits a temporary file that is removed at the end of the script.
2095 | sed -i 's|vmtemplate xmlns="unraid"|vmtemplate xmlns="http://unraid.net/xmlns"|g' "$vm.xml"
2096 |
2097 | # create a directory named after the vm within backup_location to store the backup files.
2098 | if [ ! -d "$backup_location/$vm" ] ; then
2099 |
2100 | log_message "information: $backup_location/$vm does not exist. creating it."
2101 |
2102 | # make the directory as it doesn't exist. added -v option to give a confirmation message to command line.
2103 | mkdir -vp "$backup_location/$vm"
2104 |
2105 | else
2106 |
2107 | log_message "information: $backup_location/$vm exists. continuing."
2108 |
2109 | fi
2110 |
2111 | if [ "$enable_vm_log_file" -eq 1 ]; then
2112 | vm_log_file="$backup_location/$vm/$timestamp""unraid-vmbackup.log"
2113 | fi
2114 |
2115 | # see if vdisks should be backed up, if the number of backups is more than 1, if snapshots are being used, and if delta sync is enabled.
2116 | if [ "$backup_vdisks" -eq 1 ] && [ "$number_of_backups_to_keep" -ne 1 ] && [ "$use_snapshots" -ne 1 ] && [ "$disable_delta_sync" -ne 1 ]; then
2117 |
2118 | # copy vdisks
2119 | copy_vdisks "existing_backup"
2120 |
2121 | fi
2122 |
2123 |
2124 | # get the state of the vm for making sure it is off before backing up.
2125 | vm_state=$(virsh domstate "$vm")
2126 |
2127 | # get the state of the vm for putting the VM in it's original state after backing up.
2128 | vm_original_state=$vm_state
2129 |
2130 | # initialize skip_vm_shutdown variable as false.
2131 | skip_vm_shutdown=false
2132 |
2133 | # determine if vm should be kept running.
2134 | # first check to see if vm exists in vms_to_backup_running variable.
2135 | for vm_to_keep_running in $vms_to_backup_running
2136 | do
2137 |
2138 | if [[ "$vm_to_keep_running" == "$vm" ]]; then
2139 | skip_vm_shutdown=true
2140 | fi
2141 |
2142 | done
2143 |
2144 | # if vm is not found in vms_to_backup_running and use_snapshots is disabled, then skip shutdown proceedure.
2145 | if [ "$skip_vm_shutdown" = false ] && [ "$use_snapshots" -eq 0 ]; then
2146 |
2147 | log_message "information: skip_vm_shutdown is false. beginning vm shutdown procedure."
2148 |
2149 | # prepare vm for backup.
2150 | prepare_vm "$vm" "$vm_state"
2151 |
2152 | elif [ "$skip_vm_shutdown" = true ] || [ "$use_snapshots" -eq 1 ]; then
2153 |
2154 | can_backup_vm="y"
2155 | log_message "information: skip_vm_shutdown is $skip_vm_shutdown and use_snapshots is $use_snapshots. skipping vm shutdown procedure. $vm is $vm_state. can_backup_vm set to $can_backup_vm."
2156 |
2157 | else
2158 |
2159 | log_message "failure: skip_vm_shutdown is $skip_vm_shutdown and use_snapshots is $use_snapshots. skipping vm shutdown procedure. $vm is $vm_state. can_backup_vm set to $can_backup_vm." "$vm backup failed" "alert"
2160 |
2161 | fi
2162 |
2163 |
2164 | # log if this is a dry run or not.
2165 | log_message "information: actually_copy_files is $actually_copy_files."
2166 |
2167 | # check whether to backup the vm or not.
2168 | if [[ "$can_backup_vm" == "y" ]]; then
2169 |
2170 | # start backing up vm configuration, nvram, and snapshots.
2171 | log_message "information: can_backup_vm flag is $can_backup_vm. starting backup of $vm configuration, nvram, and vdisk(s)." "script starting $vm backup" "normal"
2172 |
2173 | # see if config should be backed up.
2174 | if [ "$backup_xml" -eq 1 ]; then
2175 |
2176 | # check if only one non-timestamped backup is being kept. if so, perform rsync without a timestamp. if not, continue as normal.
2177 | if [ "$timestamp_files" -eq 0 ] && [ "$number_of_backups_to_keep" -eq 1 ]; then
2178 |
2179 | copy_file "$vm.xml" "$backup_location/$vm/$vm.xml" "$rsync_dry_run_option" "standard" "$rsync_only"
2180 |
2181 | # make sure copy has current date/time for modified attribute so that removing old backups by date will work.
2182 | touch -d "now" "$backup_location/$vm/$vm.xml"
2183 |
2184 | # run compare function. compare will not run if compare_files is disabled.
2185 | run_compare "$vm.xml" "$backup_location/$vm/$vm.xml" "config"
2186 |
2187 | else
2188 |
2189 | copy_file "$vm.xml" "$backup_location/$vm/$timestamp$vm.xml" "$rsync_dry_run_option" "standard" "$rsync_only"
2190 |
2191 | # make sure copy has current date/time for modified attribute so that removing old backups by date will work.
2192 | touch -d "now" "$backup_location/$vm/$timestamp$vm.xml"
2193 |
2194 | # run compare function. compare will not run if compare_files is disabled.
2195 | run_compare "$vm.xml" "$backup_location/$vm/$timestamp$vm.xml" "config"
2196 |
2197 | fi
2198 |
2199 | fi
2200 |
2201 |
2202 | # see if nvram should be backed up.
2203 | if [ "$backup_nvram" -eq 1 ]; then
2204 |
2205 | # extract nvram path from config file.
2206 | nvram_path=$(xmllint --xpath "string(/domain/os/nvram)" "$vm.xml")
2207 |
2208 | # get nvram file name from path.
2209 | nvram_filename=$(basename "$nvram_path")
2210 |
2211 | # get nvram file extension from filename.
2212 | nvram_extension="${nvram_filename##*.}"
2213 |
2214 | # check to see if nvram_path is empty.
2215 | if [[ -z "$nvram_path" ]]; then
2216 |
2217 | log_message "information: $vm does not appear to have an nvram file. skipping."
2218 |
2219 | else
2220 |
2221 | # check if only one non-timestamped backup is being kept. if so, perform rsync without a timestamp. if not, continue as normal.
2222 | if [ "$timestamp_files" -eq 0 ] && [ "$number_of_backups_to_keep" -eq 1 ]; then
2223 |
2224 | copy_file "$nvram_path" "$backup_location/$vm/$nvram_filename" "$rsync_dry_run_option" "standard" "$rsync_only"
2225 |
2226 | # make sure copy has current date/time for modified attribute so that removing old backups by date will work.
2227 | touch -d "now" "$backup_location/$vm/$nvram_filename"
2228 |
2229 | # run compare function. compare will not run if compare_files is disabled.
2230 | run_compare "$nvram_path" "$backup_location/$vm/$nvram_filename" "nvram"
2231 |
2232 | else
2233 |
2234 | copy_file "$nvram_path" "$backup_location/$vm/$timestamp$nvram_filename" "$rsync_dry_run_option" "standard" "$rsync_only"
2235 |
2236 | # make sure copy has current date/time for modified attribute so that removing old backups by date will work.
2237 | touch -d "now" "$backup_location/$vm/$timestamp$nvram_filename"
2238 |
2239 | # run compare function. compare will not run if compare_files is disabled.
2240 | run_compare "$nvram_path" "$backup_location/$vm/$timestamp$nvram_filename" "nvram"
2241 |
2242 | fi
2243 |
2244 | fi
2245 |
2246 | fi
2247 |
2248 |
2249 | # see if vdisks should be backed up.
2250 | if [ "$backup_vdisks" -eq 1 ]; then
2251 |
2252 | copy_vdisks "source_image"
2253 |
2254 | # check to see if the snapshot failed for the current vdisk and if snapshot fallback is disabled. if so, continue to the next vm to prevent data loss.
2255 | if [ "$snapshot_succeeded" = false ] && [ "$snapshot_fallback" -eq 0 ]; then
2256 |
2257 | continue
2258 |
2259 | fi
2260 |
2261 | fi
2262 |
2263 |
2264 | # list extensions of vdisks that were backed up.
2265 | log_message "information: the extensions of the vdisks that were backed up are ${vdisk_extensions[*]}."
2266 |
2267 | # check to see if set_vm_to_original_state is 1 and then check the vm's original state.
2268 | if [ "$set_vm_to_original_state" -eq 1 ]; then
2269 |
2270 | # get the current state of the vm for checking against its orginal state.
2271 | vm_state=$(virsh domstate "$vm")
2272 |
2273 | # start the vm after backup based on previous state.
2274 | if [ ! "$vm_state" == "$vm_original_state" ] && [ "$vm_original_state" == "running" ]; then
2275 |
2276 | log_message "information: vm_state is $vm_state. vm_original_state is $vm_original_state. starting $vm." "script starting $vm" "normal"
2277 |
2278 | if [ "$vm_state" == "paused" ]; then
2279 |
2280 | # resume vm
2281 | virsh resume "$vm"
2282 |
2283 | elif [ "$vm_state" == "shut off" ]; then
2284 |
2285 | # start vm
2286 | virsh start "$vm"
2287 |
2288 | else
2289 |
2290 | # there was an error
2291 | log_message "warning: vm_state is $vm_state. vm_original_state is $vm_original_state. unable to start $vm." "script cannot start $vm" "warning"
2292 |
2293 | fi
2294 |
2295 | else
2296 |
2297 | log_message "information: vm_state is $vm_state. vm_original_state is $vm_original_state. not starting $vm." "script not starting $vm" "normal"
2298 |
2299 | fi
2300 |
2301 | fi
2302 |
2303 |
2304 | # if start_vm_after_backup is set to 1 then start the vm but dont check that it has been successful.
2305 | if [ "$start_vm_after_backup" -eq 1 ]; then
2306 |
2307 | log_message "information: vm_state is $vm_state. start_vm_after_backup is $start_vm_after_backup. starting $vm." "script starting $vm" "normal"
2308 |
2309 | if [ "$vm_state" == "paused" ]; then
2310 |
2311 | # resume vm
2312 | virsh resume "$vm"
2313 |
2314 | elif [ "$vm_state" == "shut off" ]; then
2315 |
2316 | # start vm
2317 | virsh start "$vm"
2318 |
2319 | else
2320 |
2321 | # there was an error
2322 | log_message "warning: vm_state is $vm_state. vm_original_state is $vm_original_state. unable to start $vm." "script cannot start $vm" "warning"
2323 |
2324 | fi
2325 |
2326 | fi
2327 |
2328 | # check to see if backup files should be compressed.
2329 | if [ "$compress_backups" -eq 1 ] && [ "$inline_zstd_compress" -ne 1 ]; then
2330 |
2331 | # check if only one non-timestamped backup is being kept. if so, perform compression without a timestamp. if not, continue as normal.
2332 | if [ "$timestamp_files" -eq 0 ] && [ "$number_of_backups_to_keep" -eq 1 ]; then
2333 |
2334 | # build vdisk_extensions_find_cmd.
2335 | build_vdisk_extensions_find_cmd "$backup_location/$vm/"
2336 |
2337 | # make sure new image files exist before removing existing tarball
2338 | if [[ -n $("${vdisk_extensions_find_cmd[@]}") ]]; then
2339 |
2340 | new_image_files_exist=true
2341 |
2342 | log_message "information: found new image files."
2343 |
2344 | else
2345 |
2346 | new_image_files_exist=false
2347 |
2348 | log_message "warning: could not find new image files. backup may have failed." "no new image files for $vm" "warning"
2349 |
2350 | fi
2351 |
2352 | # make sure new config files exist before removing existing tarball
2353 | if [[ "$backup_xml" -eq 1 ]] && [[ -n $(find "$backup_location/$vm" -type f \( -name '*.xml' \) ) ]]; then
2354 |
2355 | new_xml_files_exist=true
2356 |
2357 | log_message "information: found new xml files."
2358 |
2359 | elif [[ "$backup_xml" -eq 0 ]]; then
2360 |
2361 | new_xml_files_exist=true
2362 |
2363 | log_message "information: xml files not set to backup. skipping check."
2364 |
2365 | else
2366 |
2367 | new_xml_files_exist=false
2368 |
2369 | log_message "warning: could not find new xml files. backup may have failed." "no new xml files for $vm" "warning"
2370 |
2371 | fi
2372 |
2373 | # make sure new nvram files exist before removing existing tarball
2374 | if [[ "$backup_nvram" -eq 1 ]] && [[ -n $(find "$backup_location/$vm" -type f \( -name '*.'"$nvram_extension" \) ) ]]; then
2375 |
2376 | new_nvram_files_exist=true
2377 |
2378 | log_message "information: found new nvram files."
2379 |
2380 | elif [[ "$backup_nvram" -eq 0 ]]; then
2381 |
2382 | new_nvram_files_exist=true
2383 |
2384 | log_message "information: nvram files not set to backup. skipping check."
2385 |
2386 | else
2387 |
2388 | new_nvram_files_exist=false
2389 |
2390 | log_message "warning: could not find new nvram files. backup may have failed." "no new nvram files for $vm" "warning"
2391 |
2392 | fi
2393 |
2394 | if [ "$new_image_files_exist" = true ] && [ "$new_xml_files_exist" = true ] && [ "$new_nvram_files_exist" = true ]; then
2395 |
2396 | log_message "information: found new backup files. removing existing tarball."
2397 |
2398 | # remove existing tarball
2399 | rm -fv "$backup_location/$vm/"*.tar.gz | tee -a "$backup_location/$log_file_subfolder$timestamp""unraid-vmbackup.log"
2400 |
2401 | # create new compressed tarball with latest backup.
2402 | log_message "information: started creating new tarball."
2403 |
2404 | # create list of files to be backed up.
2405 | backup_file_list="$backup_location/$vm/backup_file_list.txt"
2406 |
2407 | # remove any existing list of files to be backup and create a blank backup file list.
2408 | if [[ -f "$backup_file_list" ]]; then
2409 | rm -fv "$backup_file_list"
2410 | fi
2411 |
2412 | log_message "information: creating blank backup file list at $backup_file_list."
2413 | touch "$backup_file_list"
2414 |
2415 | # for each extension, add it to the list of files to be backed up.
2416 | for extension in "${vdisk_extensions[@]}"
2417 |
2418 | do
2419 |
2420 | find "$backup_location/$vm" -type f -name '*.'"$extension" -printf "%f\n" >> "$backup_file_list"
2421 |
2422 | done
2423 |
2424 | # see if config files should be backed up and then add any to to the list of files to be backed up.
2425 | if [[ "$backup_xml" -eq 1 ]]; then
2426 |
2427 | find "$backup_location/$vm" -type f -name '*.xml' -printf "%f\n" >> "$backup_file_list"
2428 |
2429 | fi
2430 |
2431 | # see if nvram files should be backed up and then any to to the list of files to be backed up.
2432 | if [[ "$backup_nvram" -eq 1 ]]; then
2433 |
2434 | find "$backup_location/$vm" -type f -name '*.'"$nvram_extension" -printf "%f\n" >> "$backup_file_list"
2435 |
2436 | fi
2437 |
2438 | # backup files based off of backup file list.
2439 | (cd "$backup_location/$vm/" && tar zcvSf "$backup_location/$vm/$vm.tar.gz" -C "$backup_location/$vm/" -T "$backup_file_list")
2440 |
2441 | # remove backup file list.
2442 | log_message "information: removing backup file list at $backup_file_list."
2443 | rm -fv "$backup_file_list"
2444 |
2445 | log_message "information: finished creating new tarball."
2446 |
2447 | # remove config, nvram, and image files that were compressed.
2448 | # build remove_old_files_cmd.
2449 | build_remove_old_files_cmd "$backup_location/$vm/"
2450 |
2451 | # execute remove_old_files_cmd to delelte files that were compressed.
2452 | "${remove_old_files_cmd[@]}"
2453 |
2454 | log_message "information: removed xml, nvram, and image files that were compressed."
2455 |
2456 | else
2457 |
2458 | log_message "warning: could not find new files to backup. backup may have failed. not removing existing tarball." "no new image files for $vm" "warning"
2459 |
2460 | fi
2461 |
2462 | else
2463 |
2464 | # create compressed tarball with ALL config, nvram, and image files.
2465 |
2466 | log_message "information: started creating new tarball."
2467 |
2468 | # create list of files to be backed up.
2469 | backup_file_list="$backup_location/$vm/backup_file_list.txt"
2470 |
2471 | # remove any existing list of files to be backup and create a blank file.
2472 | if [[ -f "$backup_file_list" ]]; then
2473 | rm -fv "$backup_file_list"
2474 | fi
2475 |
2476 | log_message "information: creating blank backup file list at $backup_file_list."
2477 | touch "$backup_file_list"
2478 |
2479 | # for each extension, add it to the list of files to be backed up.
2480 | for extension in "${vdisk_extensions[@]}"
2481 |
2482 | do
2483 |
2484 | find "$backup_location/$vm" -type f -name '*.'"$extension" -printf "%f\n" >> "$backup_file_list"
2485 |
2486 | done
2487 |
2488 | # see if config files should be backed up and then add any to to the list of files to be backed up.
2489 | if [[ "$backup_xml" -eq 1 ]]; then
2490 |
2491 | find "$backup_location/$vm" -type f -name '*.xml' -printf "%f\n" >> "$backup_file_list"
2492 |
2493 | fi
2494 |
2495 | # see if nvram files should be backed up and then any to to the list of files to be backed up.
2496 | if [[ "$backup_nvram" -eq 1 ]]; then
2497 |
2498 | find "$backup_location/$vm" -type f -name '*.'"$nvram_extension" -printf "%f\n" >> "$backup_file_list"
2499 |
2500 | fi
2501 |
2502 | # backup files based off of backup file list.
2503 | (cd "$backup_location/$vm/" && tar zcvSf "$backup_location/$vm/$timestamp$vm.tar.gz" -C "$backup_location/$vm/" -T "$backup_file_list")
2504 |
2505 | # remove backup file list.
2506 | log_message "information: removing backup file list at $backup_file_list."
2507 | rm -fv "$backup_file_list"
2508 |
2509 | log_message "information: finished creating new tarball."
2510 |
2511 | # remove config, nvram, and image files that were compressed.
2512 | # build remove_old_files_cmd.
2513 | build_remove_old_files_cmd "$backup_location/$vm/"
2514 |
2515 | # execute remove_old_files_cmd to delelte files that were compressed.
2516 | "${remove_old_files_cmd[@]}"
2517 |
2518 | log_message "information: removed xml, nvram, and image files that were compressed."
2519 |
2520 | fi
2521 |
2522 | fi
2523 |
2524 | else
2525 |
2526 | # start the vm based on previous state.
2527 | if [ "$vm_original_state" == "running" ]; then
2528 |
2529 | log_message "information: vm_state is $vm_state. vm_original_state is $vm_original_state. starting $vm." "script starting $vm" "normal"
2530 |
2531 | if [ "$vm_state" == "paused" ]; then
2532 |
2533 | # resume vm
2534 | virsh resume "$vm"
2535 |
2536 | elif [ "$vm_state" == "shut off" ]; then
2537 |
2538 | # start vm
2539 | virsh start "$vm"
2540 |
2541 | else
2542 |
2543 | # there was an error
2544 | log_message "warning: vm_state is $vm_state. vm_original_state is $vm_original_state. unable to start $vm." "script cannot start $vm" "warning"
2545 |
2546 | fi
2547 |
2548 | else
2549 |
2550 | log_message "information: vm_original_state is $vm_original_state. not starting $vm." "script not starting $vm" "normal"
2551 |
2552 | fi
2553 |
2554 | # for whatever reason the backup attempt failed.
2555 | log_message "failure: backup of $vm to $backup_location/$vm failed." "backup of $vm failed" "alert"
2556 |
2557 | # if start_vm_after_failure is set to 1 then start the vm but dont check that it has been successful.
2558 | if [ "$start_vm_after_failure" -eq 1 ]; then
2559 |
2560 | log_message "information: start_vm_after_failure is $start_vm_after_failure. vm_state is $vm_state. vm_original_state is $vm_original_state. starting $vm." "script starting $vm" "normal"
2561 |
2562 | if [ "$vm_state" == "paused" ]; then
2563 |
2564 | # resume vm
2565 | virsh resume "$vm"
2566 |
2567 | elif [ "$vm_state" == "shut off" ]; then
2568 |
2569 | # start vm
2570 | virsh start "$vm"
2571 |
2572 | else
2573 |
2574 | # there was an error
2575 | log_message "warning: vm_state is $vm_state. vm_original_state is $vm_original_state. unable to start $vm." "script cannot start $vm" "warning"
2576 |
2577 | fi
2578 |
2579 | fi
2580 |
2581 | fi
2582 |
2583 |
2584 | log_message "information: backup of $vm to $backup_location/$vm completed." "script completed $vm backup" "normal"
2585 |
2586 | # check to see how many days backups should be kept.
2587 | if [ "$number_of_days_to_keep_backups" -eq 0 ]; then
2588 |
2589 | log_message "information: number of days to keep backups set to indefinitely."
2590 |
2591 | else
2592 |
2593 | log_message "information: cleaning out backups older than $number_of_days_to_keep_backups days in location ONLY if newer files exist in $backup_location/$vm/" "script removing old backups" "normal"
2594 |
2595 |
2596 | # remove old config files if backup_xml is 1.
2597 | if [ "$backup_xml" -eq 1 ]; then
2598 | find_cmd=(find "$backup_location/$vm/" -type f -name '*.xml')
2599 | remove_old_files find_cmd "config"
2600 | fi
2601 |
2602 | # remove old nvram files if backup_nvram is 1.
2603 | if [ "$backup_nvram" -eq 1 ]; then
2604 | find_cmd=(find "$backup_location/$vm/" -type f -name '*.'"$nvram_extension")
2605 | remove_old_files find_cmd "nvram"
2606 | fi
2607 |
2608 | # remove old vdisk image files (including inline zstd compressed ones) if backup_vdisks is 1.
2609 | if [ "$backup_vdisks" -eq 1 ]; then
2610 | build_vdisk_extensions_find_cmd "$backup_location/$vm/"
2611 | remove_old_files vdisk_extensions_find_cmd "vdisk image"
2612 | fi
2613 |
2614 | # remove old tarballs if compress_backups is 1.
2615 | if [ "$compress_backups" -eq 1 ]; then
2616 | find_cmd=(find "$backup_location/$vm/" -type f -name '*.tar.gz')
2617 | remove_old_files find_cmd "tarball"
2618 | fi
2619 |
2620 | # remove old vm log files
2621 | find_cmd=(find "$backup_location/$vm/" -type f -name '*unraid-vmbackup.log')
2622 | remove_old_files find_cmd "vm log"
2623 |
2624 | fi
2625 |
2626 | # check to see how many backups should be kept.
2627 | if [ "$number_of_backups_to_keep" -eq 0 ]; then
2628 |
2629 | log_message "information: number of backups to keep set to infinite."
2630 |
2631 | else
2632 |
2633 | log_message "information: cleaning out backups over $number_of_backups_to_keep in location $backup_location/$vm/" "script removing old backups" "normal"
2634 |
2635 | # remove config files that are over the limit if backup_xml is 1.
2636 | if [ "$backup_xml" -eq 1 ]; then
2637 | find_cmd=(find "$backup_location/$vm/" -type f -name '*.xml')
2638 | remove_over_limit_files find_cmd "$number_of_backups_to_keep" "config"
2639 | fi
2640 |
2641 | # remove nvram files that are over the limit if backup_nvram is 1.
2642 | if [ "$backup_nvram" -eq 1 ]; then
2643 | find_cmd=(find "$backup_location/$vm/" -type f -name '*.'"$nvram_extension")
2644 | remove_over_limit_files find_cmd "$number_of_backups_to_keep" "nvram"
2645 | fi
2646 |
2647 | # remove image files that are over the limit if backup_vdisks is 1.
2648 | if [ "$backup_vdisks" -eq 1 ]; then
2649 |
2650 | # initialize variables to find number of vdisks
2651 | numberofvdisks="1"
2652 |
2653 | # find each vdisk extension and use it to build a regular expression to get a vdisk's trailing number.
2654 | for extension in "${vdisk_extensions[@]}"
2655 | do
2656 |
2657 | # if empty, build initial regular expression, otherwise append to it
2658 | if [[ -z "$vdisknumberextregex" ]]; then
2659 | vdisknumberextregex='[0-9]+\.('
2660 | vdisknumberextregex+="$extension"
2661 | else
2662 | vdisknumberextregex+="|$extension"
2663 | fi
2664 |
2665 | if [ "$inline_zstd_compress" -eq 1 ]; then
2666 | vdisknumberextregex+=".zst"
2667 | fi
2668 | done
2669 |
2670 | # put closing parenthesis on regular expression.
2671 | vdisknumberextregex+=")"
2672 |
2673 | # get number of vdisks
2674 | vdisknumberonlyregex="[0-9]+"
2675 | for imagefilename in "$backup_location/$vm/"*
2676 | do
2677 | # get highest number from vdisk count
2678 | if [[ $imagefilename =~ $vdisknumberextregex ]]; then
2679 | imagefilenamenumberext=${BASH_REMATCH[0]}
2680 | if [[ $imagefilenamenumberext =~ $vdisknumberonlyregex ]]; then
2681 | vdisk_numberonly=${BASH_REMATCH[0]}
2682 | fi
2683 | fi
2684 | if [[ "$numberofvdisks" =~ ^[0-9]+$ ]] && [[ "$vdisk_numberonly" =~ ^[0-9]+$ ]]; then
2685 | if [ "$numberofvdisks" -lt "$vdisk_numberonly" ]; then
2686 | numberofvdisks="$vdisk_numberonly"
2687 | fi
2688 | fi
2689 | done
2690 |
2691 | # create variable equal to number_of_backups_to_keep, times the number of vdisks, to make sure that the correct number of files are kept.
2692 | number_of_files_to_keep=$((number_of_backups_to_keep * numberofvdisks))
2693 |
2694 | build_vdisk_extensions_find_cmd "$backup_location/$vm/"
2695 | remove_over_limit_files vdisk_extensions_find_cmd "$number_of_files_to_keep" "vdisk image"
2696 | fi
2697 |
2698 | # remove tarball files that are over the limit if compress_backups is 1.
2699 | if [ "$compress_backups" -eq 1 ]; then
2700 | find_cmd=(find "$backup_location/$vm/" -type f -name '*.tar.gz')
2701 | remove_over_limit_files find_cmd "$number_of_backups_to_keep" "tarball"
2702 | fi
2703 |
2704 | # remove vm log files that are over the limit
2705 | # shellcheck disable=SC2034
2706 | find_cmd=(find "$backup_location/$vm/" -type f -name '*unraid-vmbackup.log')
2707 | remove_over_limit_files find_cmd "$number_of_backups_to_keep" "vm log"
2708 |
2709 | fi
2710 |
2711 | unset vm_log_file
2712 |
2713 | # delete the working copy of the config.
2714 | log_message "information: removing local $vm.xml."
2715 | rm -fv "$vm.xml"
2716 |
2717 | done
2718 |
2719 | log_message "information: finished attempt to backup $vms_to_backup_list to $backup_location."
2720 |
2721 |
2722 | # check to see if reconstruct write was enabled by this script. if so, disable and continue.
2723 | if [ "$enable_reconstruct_write" -eq 1 ]; then
2724 |
2725 | /usr/local/sbin/mdcmd set md_write_method 0
2726 | log_message "information: Reconstruct write disabled."
2727 |
2728 | fi
2729 |
2730 |
2731 | # check to see if log file should be kept.
2732 | if [ "$keep_log_file" -eq 1 ]; then
2733 |
2734 | if [ "$number_of_log_files_to_keep" -eq 0 ]; then
2735 |
2736 | log_message "information: number of logs to keep set to infinite."
2737 |
2738 | else
2739 |
2740 | log_message "information: cleaning out logs over $number_of_log_files_to_keep."
2741 |
2742 | # create variable equal to number_of_log_files_to_keep plus one to make sure that the correct number of files are kept.
2743 | log_files_plus_1=$((number_of_log_files_to_keep + 1))
2744 |
2745 | # remove log files that are over the limit.
2746 | if [ "$detailed_notifications" -eq 1 ] && [ "$send_notifications" -eq 1 ] && [ "$only_send_error_notifications" -eq 0 ]; then
2747 |
2748 | deleted_files=$(find "$backup_location/$log_file_subfolder"*unraid-vmbackup.log -type f -printf '%T@\t%p\n' | sort -t $'\t' -gr | tail -n +$log_files_plus_1 | cut -d $'\t' -f 2- | xargs -d '\n' -r rm -fv --)
2749 |
2750 | if [[ -n "$deleted_files" ]]; then
2751 |
2752 | for deleted_file in $deleted_files
2753 |
2754 | do
2755 |
2756 | log_message "information: $deleted_file." "script removing logs" "normal"
2757 |
2758 | done
2759 |
2760 | else
2761 |
2762 | log_message "information: did not find any log files to remove." "script removing logs" "normal"
2763 |
2764 | fi
2765 |
2766 | else
2767 |
2768 | deleted_files=$(find "$backup_location/$log_file_subfolder"*unraid-vmbackup.log -type f -printf '%T@\t%p\n' | sort -t $'\t' -gr | tail -n +$log_files_plus_1 | cut -d $'\t' -f 2- | xargs -d '\n' -r rm -fv --)
2769 |
2770 | if [[ -n "$deleted_files" ]]; then
2771 |
2772 | for deleted_file in $deleted_files
2773 |
2774 | do
2775 |
2776 | log_message "information: $deleted_file."
2777 |
2778 | done
2779 |
2780 | else
2781 |
2782 | log_message "information: did not find any log files to remove."
2783 |
2784 | fi
2785 |
2786 | fi
2787 |
2788 | fi
2789 |
2790 | fi
2791 |
2792 | # check to see if error log file should be kept.
2793 | if [ "$keep_error_log_file" -eq 1 ]; then
2794 |
2795 | if [ "$number_of_error_log_files_to_keep" -eq 0 ]; then
2796 |
2797 | log_message "information: number of error logs to keep set to infinite."
2798 |
2799 | else
2800 |
2801 | log_message "information: cleaning out error logs over $number_of_error_log_files_to_keep."
2802 |
2803 | # create variable equal to number_of_error_log_files_to_keep plus one to make sure that the correct number of files are kept.
2804 | error_log_files_plus_1=$((number_of_error_log_files_to_keep + 1))
2805 |
2806 | # remove error log files that are over the limit.
2807 | if [ "$detailed_notifications" -eq 1 ] && [ "$send_notifications" -eq 1 ] && [ "$only_send_error_notifications" -eq 0 ]; then
2808 |
2809 | deleted_files=$(find "$backup_location/$log_file_subfolder"*unraid-vmbackup_error.log -type f -printf '%T@\t%p\n' | sort -t $'\t' -gr | tail -n +$error_log_files_plus_1 | cut -d $'\t' -f 2- | xargs -d '\n' -r rm -fv --)
2810 |
2811 | if [[ -n "$deleted_files" ]]; then
2812 |
2813 | for deleted_file in $deleted_files
2814 |
2815 | do
2816 |
2817 | log_message "information: $deleted_file." "script removing error logs" "normal"
2818 |
2819 | done
2820 |
2821 | else
2822 |
2823 | log_message "information: did not find any error log files to remove." "script removing error logs" "normal"
2824 |
2825 | fi
2826 |
2827 | else
2828 |
2829 | deleted_files=$(find "$backup_location/$log_file_subfolder"*unraid-vmbackup_error.log -type f -printf '%T@\t%p\n' | sort -t $'\t' -gr | tail -n +$error_log_files_plus_1 | cut -d $'\t' -f 2- | xargs -d '\n' -r rm -fv --)
2830 |
2831 | if [[ -n "$deleted_files" ]]; then
2832 |
2833 | for deleted_file in $deleted_files
2834 |
2835 | do
2836 |
2837 | log_message "information: $deleted_file."
2838 |
2839 | done
2840 |
2841 | else
2842 |
2843 | log_message "information: did not find any error log files to remove."
2844 |
2845 | fi
2846 |
2847 | fi
2848 |
2849 | fi
2850 |
2851 | fi
2852 |
2853 | # check to see if there were any errors.
2854 | if [ "$errors" -eq 1 ]; then
2855 |
2856 | log_message "warning: errors found. creating error log file."
2857 |
2858 | rsync -av "$backup_location/$log_file_subfolder$timestamp""unraid-vmbackup.log" "$backup_location/$log_file_subfolder$timestamp""unraid-vmbackup_error.log"
2859 |
2860 | # get rsync result and send notification
2861 | if [[ $? -eq 1 ]]; then
2862 |
2863 | log_message "failure: $backup_location/$log_file_subfolder$timestamp""unraid-vmbackup_error.log create failed." "error log create failed" "alert"
2864 |
2865 | fi
2866 |
2867 | fi
2868 |
2869 | # check to see if log file should be removed.
2870 | if [ "$keep_log_file" -eq 0 ]; then
2871 |
2872 | if [ "$errors" -eq 1 ] && [ "$keep_error_log_file" -eq 1 ]; then
2873 |
2874 | echo "$(date '+%Y-%m-%d %H:%M:%S') warning: removing log file." | tee -a "$backup_location/$log_file_subfolder$timestamp""unraid-vmbackup_error.log"
2875 |
2876 | rm -fv "$backup_location/$log_file_subfolder$timestamp""unraid-vmbackup.log"
2877 |
2878 | else
2879 |
2880 | log_message "warning: removing log file."
2881 |
2882 | rm -fv "$backup_location/$log_file_subfolder$timestamp""unraid-vmbackup.log"
2883 |
2884 | fi
2885 |
2886 | fi
2887 |
2888 | # check to see if error log file should be removed.
2889 | if [ "$keep_error_log_file" -eq 0 ]; then
2890 |
2891 | if [ "$keep_log_file" -eq 1 ]; then
2892 |
2893 | log_message "warning: removing error log file."
2894 |
2895 | rm -fv "$backup_location/$log_file_subfolder$timestamp""unraid-vmbackup_error.log"
2896 |
2897 | else
2898 |
2899 | notification_message "warning: removing error log file."
2900 |
2901 | rm -fv "$backup_location/$log_file_subfolder$timestamp""unraid-vmbackup_error.log"
2902 |
2903 | fi
2904 |
2905 | fi
2906 |
2907 | ### Logging Stopped ###
2908 | if [ "$keep_log_file" -eq 1 ]; then
2909 |
2910 | log_message "Stop logging to log file."
2911 |
2912 | fi
2913 |
2914 | if [ "$errors" -eq 1 ] && [ "$keep_error_log_file" -eq 1 ]; then
2915 |
2916 | echo "$(date '+%Y-%m-%d %H:%M:%S') Stop logging to error log file." | tee -a "$backup_location/$log_file_subfolder$timestamp""unraid-vmbackup_error.log"
2917 |
2918 | fi
2919 |
2920 |
2921 | if [ "$send_notifications" -eq 1 ]; then
2922 |
2923 | if [ "$errors" -eq 1 ]; then
2924 |
2925 | /usr/local/emhttp/plugins/dynamix/scripts/notify -e "unraid-vmbackup_error" -s "unRAID VM Backup script" -d "script finished with errors" -i "alert" -m "$(date '+%Y-%m-%d %H:%M:%S') warning: unRAID VM Backup script finished with errors. See log files in $backup_location/$log_file_subfolder for details."
2926 |
2927 | elif [ "$only_send_error_notifications" -eq 0 ]; then
2928 |
2929 | /usr/local/emhttp/plugins/dynamix/scripts/notify -e "unraid-vmbackup_finished" -s "unRAID VM Backup script" -d "script finished" -i "normal" -m "$(date '+%Y-%m-%d %H:%M:%S') information: unRAID VM Backup script finished. See log files in $backup_location/$log_file_subfolder for details."
2930 |
2931 | fi
2932 |
2933 | fi
2934 |
2935 | exit 0
2936 |
2937 | #### code execution end ####
2938 |
2939 |
2940 | ######################################################### script end ###########################################################
2941 |
2942 | # Originally from unraid-autovmbackup by Daniel Jackson (danioj)
2943 | # Includes additions for removing old backups added by Deeks
2944 | # Includes additions for creating snapshots added by Dikkekop (thies88)
2945 |
2946 | # for original script go to:
2947 | # https://lime-technology.com/forum/index.php?topic=47986
2948 | # for Deeks script go to:
2949 | # https://lime-technology.com/forums/topic/46281-unraid-autovmbackup-automate-backup-of-virtual-machines-in-unraid-v04/?do=findComment&comment=589821
2950 | # for Dikkekop (thies88) script go to:
2951 | # https://github.com/thies88/unraid-vmbackup
--------------------------------------------------------------------------------