├── LICENSE ├── README.md ├── pisafe ├── pisafe_1.2.6 ├── pisafe_1.2.9 ├── pisafe_beta ├── pisafe_icon.png ├── snapshot.png └── test_pisafe /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 | 635 | Copyright (C) 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 | Copyright (C) 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PiSafe - Raspberry Pi imaging app 2 | 3 | Backup or Restore Raspberry Pi media (SD-cards, USB sticks, SSD drive, ...) to/from compressed image-files right on your Pi! 4 | 5 | - Backup an SD card (or other media) to an image-file. 6 | 7 | - Restore an image-file to an SD card (or other media). 8 | 9 | - Works entirely on a Raspberry Pi (or other linux). No Windows or Mac needed. 10 | 11 | - Creates a shrunk, compressed image-file that will resize to fill the new media to which it is restored. 12 | 13 | - Supports .img .zip .xz .gz and .zst file formats. 14 | 15 | - Images are compatible with popular imaging software such as Raspberry PI Imager. 16 | 17 | - Supports USB storage devices. 18 | 19 | - Supports backing up the live boot media - but be careful, backing up media that is being written to can result in a corrupted backup. You have to turn off "hide root device" in "settings", "options" to enable this. 20 | 21 | CAUTION: Reboot first, close everything and don't use the system while it is doing step 1 of backing up! You must send your backup image to a different drive. 22 | 23 | - Supports storing the image files on a mounted network device (ie an SMB server). Some help... 24 | 25 | - sudo apt install cifs-utils 26 | 27 | - mkdir shared 28 | 29 | - sudo mount.cifs //192.168.1.18/shared shared -o user=USERNAME,vers=1.0 30 | 31 | - or 32 | 33 | - sudo mount.cifs //omv.local/shared shared -o guest 34 | 35 | - Create a library of your own pi images, then restore them to whatever media you want, whenever you want. 36 | 37 | - Can be run from the command line or use the easy menu-driven interface. 38 | 39 | - Should work with most debian and arch distros, see below. 40 | 41 | - See Leepspvideo nice review on youtube: https://www.youtube.com/watch?v=XP6ycUR9Ih0 -- "Very Impressive", "Makes a nice small image", "Really good all-in-one solution" 42 | 43 | 44 | # Install 45 | 46 | Paste or type this into a terminal window: 47 | 48 | 49 | wget https://raw.githubusercontent.com/RichardMidnight/pi-safe/main/pisafe -O pisafe 50 | bash pisafe install 51 | 52 | 53 | Beta version: 54 | 55 | wget https://raw.githubusercontent.com/RichardMidnight/pi-safe/main/pisafe_beta -O pisafe 56 | bash pisafe install 57 | 58 | 59 | Old Stable (in case current ver has issues) 60 | 61 | wget https://raw.githubusercontent.com/RichardMidnight/pi-safe/main/pisafe_1.2.9 -O pisafe 62 | bash pisafe install 63 | 64 | 65 | # Simple Setup 66 | 67 | 1) Install Raspberry PI OS and PiSafe on an SD card that is large enough to hold some SD image-files. 32GB or 64GB will do. This is your "Master" SD-card. Set this aside. 68 | 69 | 2) Install Raspberry PI OS or whatever OS you want on the "Project" SD-card. Use as small of an SD-card as you can because the entire card is read before it is shrunk and compressed. I have been using Sandisk Industrial 8GB cards. Make changes, install other software, change the desktop, whatever you want. 70 | 71 | 3) To make an image of the "Project" (8GB) card that you can restore anytime in the future do the following... 72 | 73 | 74 | 75 | # Usage - make an image file 76 | 77 | 1) Boot your Pi with your "Master" SD-card as above. 78 | 79 | 2) Put your "Project" SD-card (8GB) in the USB SD reader and insert it in a Pi USB port. 80 | 81 | 3) Startup pisafe from the menu. Or in a terminal, type in 'pisafe' 82 | 83 | 4) Use the menu to select BACKUP, select your SD-card and give the image-file a name. 84 | 85 | 5) Watch your image-file get created, shrunk and compressed automatically. 86 | 87 | 88 | 89 | 90 | # Release notes 91 | 92 | Originally developed and tested on Raspberry pi 4 running Raspberry Pi OS Buster. 93 | 94 | Also tested on RaspiOS-arm64, Raspberry Pi Desktop, Raspbian Stretch, Ubuntu 20.20 for Rpi, Linux Mint, LMDE. 95 | 96 | v1.0.5 Added support for other terminals: lxterminal, gnome-terminal, xfce-terminal, mate-terminal, konsole, xterm, uxterm, qterminal. 97 | 98 | V1.0.5 Added support for other text editors: leafpad, mousepad, gedit, kwrite, pluma, featherpad, xed, geany, kate, nano. 99 | 100 | V1.1.0 Added support for Manjaro and probably other Arch-based distros. 101 | 102 | v1.2.0 Cleaned up the code a lot, added "ignore_freespace_at_end", added erase (fat32, exfat, ntfs, ext4). 103 | 104 | v1.2.3 Cleaned up more code. Fixed bug in available-space on network-shares which halted backup. 105 | 106 | v1.2.4 Cleaned up more code. Backup with -y bypasses check_for_updates. 107 | 108 | v1.2.5 Improved support for Manjaro, Arch, Suse and Fedora. 109 | 110 | v1.2.5m Fixed issue with non-english languages. Added support for zstd compression. Made gz, xz and zst only install as needed. 111 | 112 | v1.2.7 Fixed issue with pigz and xz. 113 | 114 | v 1.2.9 Started to add command-line settings-override options (undocumented at this point). 115 | 116 | v 1.2.10 Adds support for Bookworm and NVME media 117 | 118 | 119 | # Tips 120 | PiSafe is optimized for Raspberry Pis running Raspios around the year 2020. It will work with many other linux distributions and other hardware but some features may not be optimized or may not work at all depending on the configuration. The following tips can help you optimize PiSafe for your configuration. 121 | 122 | - Ignore Freespace at end of media 123 | - PiSafe will ignore freespace at the end of the media, speeding up the backup process and using less working space. 124 | - If you have a small amount of data on a large media (eg using 10GB of a 500GB drive), you can resize your partitions (with gparted) and leave freespace (unallocated) at the end of the media which PiSafe will ignore. Note, freespace not at the end of the media cannot be ignored. 125 | 126 | - Shrinking the filesystem on backup 127 | - Shrinking the filesystem is VERY VALUABLE because it creates a smaller image file, and allows you to restore the image to a different size media. 128 | - PiSafe will shrink your filesystem if your main partition is ext4 (or ext3 or ext2) and is the last partition on the media. 129 | - If your install does not default to this partitioning setup you should be able to custom partition the media during your OS installation and put the main ext4 partition at the end. This is needed with standard debian installations including RPD and Linux Mint because they put a swap partition at then end of the media which blocks PiSafe from shrinking the filesystem. 130 | - Alternatively if your main partition is the second to last one and the last one is a swap partition, you may be able to simply delete the swap partition. 131 | - Alternatively you can zero-out your deleted files with bleachbit to optimize compression and then shrink your partitions with gparted. 132 | 133 | - Auto-expand filesystem on restore 134 | - PiSafe will setup the image file to auto-expand to fill the new media on first boot after restoring if your distro supports rc.local. 135 | - If not, you can resize your partition manual by booting from another media and using gparted. 136 | - Note: Auto-expand may not work on an overlay filesystem. Recommend turning off auto-expand in Settings/Options. 137 | 138 | - Data compression 139 | - Compressing the image file with zip, xz, gz, or zst reduces the size of the image file to around 1/2. 140 | - Standard compression levels are 1 through 9. A higher number will compress the file a little more but take a lot more time. 141 | - fastest is "zst 1". 142 | - smallest is "xz 8" (or as high as memory allows). 143 | - PiSafe default is industry standard "zip 1". 144 | 145 | # References 146 | 147 | Thanks to the Raspberry Pi foundation for instructions on how to read and write an image file. 148 | 149 | Thanks to https://github.com/Drewsif/PiShrink for the PiShrink engine. 150 | 151 | Thanks to all the people who posted code snipets on the web. 152 | 153 | 154 | -------------------------------------------------------------------------------- /pisafe_1.2.6: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | COPYRIGHT="By Richard Reed 2018 - 2022" 4 | 5 | # This script basically 6 | # 1- copies a media-device with 'dd' 7 | # 2- shrinks the last partition with 'pishrink' 8 | # 3- compresses with 'zip' 9 | 10 | # references 11 | # https://www.raspberrypi.org/documentation/installation/installing-images/linux.md 12 | # https://www.instructables.com/id/How-to-BackUp-and-Shrink-Your-Raspberry-Pi-Image/ 13 | # https://github.com/Drewsif/PiShrink 14 | 15 | # https://jfearn.fedorapeople.org/fdocs/en-US/Documentation/0.1/html/Fedora_Multiboot_Guide/freespace-ntfs.html 16 | 17 | SCRIPTNAME="${0##*/}" 18 | SCRIPTVER="1.2.6" # 2022/09/29 19 | PRODUCTNAME="PiSafe" 20 | PRODUCTCOMMENT="Raspberry Pi Imaging App" 21 | PRODUCTHOME="https://github.com/RichardMidnight/pi-safe" 22 | CURRENT_DIR=$(pwd) 23 | USER=$(whoami) 24 | CONFIG="/home/$USER/.config/pisafe/pisafe.conf" 25 | LOG_FILE="/home/$USER/.config/pisafe/pisafe.log" 26 | LOG=on 27 | REQUIRED_TOOLS="dd pishrink.sh zip unzip pv bc whiptail file nano" 28 | OPTIONAL_TOOLS="pigz xz zstd" 29 | INDEV= 30 | OUTDEV= 31 | INFILE= 32 | OUTFILE= 33 | BACKTITLE="$PRODUCTNAME ver $SCRIPTVER === $PRODUCTCOMMENT === " 34 | INTERFACE="cli" 35 | 36 | 37 | WHITE='\033[1;37m' 38 | RED='\033[1;31m' 39 | GREEN='\033[0;32m' 40 | BLUE='\033[0;34m' 41 | LTBLUE='\033[1;34m' 42 | NC='\033[0m' # No Color, standard text 43 | echo_white() { (echo -e "${WHITE}$*${NC}") } 44 | echo_red() { (echo -e "${RED}$*${NC}") } 45 | echo_blue() { (echo -e "${LTBLUE}$*${NC}") } 46 | echo_green() { (echo -e "${GREEN}$*${NC}") } 47 | 48 | 49 | 50 | #exit status map 1-255 51 | # ($?) Expands to the exit status of the most recently executed foreground pipeline. 52 | 53 | #backup start at 10 54 | # 10 no media exists 55 | # 11 indev is blank 56 | # 12 indev doesn't exist 57 | 58 | # 13 outfile is blank 59 | # 14 outfile path does not exist 60 | # 15 unsupported extension 61 | # 16 outfile exists 62 | # 17 outfile_base.img exists 63 | # 18 outfile_base.img.ext exists 64 | # 19 Media is bigger than freespace 65 | 66 | # 20 error in dd 67 | # 21 error in pishrink 68 | # 22 error in compression 69 | 70 | #restore start at 71 | # 20 72 | 73 | # 74 | 75 | 76 | notes_desktop_environment(){ 77 | # this is just a reference of tested OSs from 2021 78 | 79 | DISTRO TERMINAL TEXT EDITOR 80 | RaspberryPiOS-stretch lxterminal leafpad? 81 | RaspberrypiOS-buster lxterminal mousepad 82 | Debian-GNOME gnome-terminal gedit 83 | Debian Xfce xfce4-terminal mousepad 84 | Debian KDE Plasma konsole kwrite 85 | Debian Cinnamon xterm / uxterm gedit 86 | Debian MATE mate-terminal pluma 87 | Debian LXDE lxterminal mousepad 88 | Debian LXQt qterminal featherpad 89 | ubuntu gnome gnome-terminal gedit 90 | lubuntu LXQt qterminal featherpad 91 | LMDE-4 Cinnamon gnome-terminal xed 92 | mint cinnamon gnome-terminal xed 93 | mint MATE mate-terminal xed 94 | Mint Xfce xfce4-terminal Xed 95 | 96 | bionic puppy - ppm lxterminal geany 97 | arch / manjaro - pacman -S 98 | zorin - ubuntu based 99 | manjaro kde konsole kate 100 | manjaro xfce xfce-terminal mousepad 101 | kali 2022 -apt xterm mousepad -ver 1.2.5 102 | 103 | 104 | Tested with 105 | --- ARM --- 106 | Raspios Buster good 107 | Raspbian Stretch good 108 | Raspbian Jessy can work with limitations 109 | Manjaro good no whiptail on lite ver 110 | Ubuntu good 111 | Note: overlay filesystem cannot be turned on 112 | 113 | --- x86-64 --- 114 | debian Strech, Buster no shrink, last is logical swap. 115 | RPD Buster no shrink, last is logical swap. 116 | mint 20.1 no shrink, last is ext logical. 117 | LMDE 4 good 118 | ubuntu good 119 | lubuntu good 120 | kali 2022 good, noshrink, swap 121 | Note: if you custom partition with ext4 as last, it shrinks. 122 | } 123 | 124 | 125 | notes_performance(){ 126 | creating image from raspi-os image expanded on an 8GB SD card. Size=837mb 127 | 128 | SETTING SIZE TIME 129 | 130 | img 3.9gb 4:30 131 | 132 | zip 1 1.37 8:10 133 | pigz 1 1.37 6:40 134 | xz 1 1.02 13:15 135 | zst 1 1.34 6:21 136 | 137 | zip 5 1.28g 10:20 138 | pigz 5 1.28g 6:52 139 | xz 5 912m 28:41 140 | zst 5 1.17g 8:53 141 | 142 | zip 9 1.26 23:15 143 | pigz 9 1.26 10:12 144 | xz 9 848 34:35 145 | zst 9 1.13 10:15 146 | 147 | pigz --best -9 1.26g 10:07 148 | pigz --best -11 149 | xz -e -9 848m 43:17 150 | zst 22 922 48:56 151 | 152 | } 153 | 154 | 155 | 156 | 157 | 158 | run_command(){ 159 | local CMD=$1 160 | local LINE=$2 161 | local QUIET=${3:-n} 162 | 163 | ui_echo "~ Running: $CMD" blue 164 | eval $CMD 165 | ES=$? 166 | if (( $ES )); then 167 | ui_echo "~ Exit_Status $ES running '$CMD'" red log 168 | return $ES 169 | fi 170 | } 171 | 172 | 173 | pisafe_about(){ 174 | echo "$PRODUCTNAME was started in 2017 as 'sd' by 'RichardMidnight on github'"\ 175 | "while working on a Raspberry Pi project and needing to make restore-points."\ 176 | "The image writers available at the time did not run on the pi itself"\ 177 | "and were not able to create a new image file." 178 | echo 179 | echo "It was originally called 'sd' and only had a CLI." 180 | echo 181 | echo "Then in 2021, the menu front-end was added to make it more usable by others."\ 182 | "It was then renamed it 'PiSafe' which is a reference to the 'ventilated cupboards for storing"\ 183 | "pies while protecting them from insects and vermin'. " 184 | echo 185 | echo "It is pretty clean bash code so it works on many debian and arch distros." 186 | echo 187 | echo "It's home is $PRODUCTHOME" 188 | echo 189 | echo "Use at your own risk." 190 | echo 191 | echo "Hopfully $PRODUCTNAME is useful to others." 192 | echo 193 | echo "Peace" 194 | } 195 | 196 | 197 | pisafe_help() { 198 | echo "$PRODUCTNAME v$SCRIPTVER - Designed for Raspberry Pi" 199 | echo " - Backup media (SD card) to an image file" 200 | echo " - Restore media (SD card) from an image file" 201 | echo 202 | echo "Usage: " 203 | echo " $SCRIPTNAME [function] [media/file] [file/media] [-y]" 204 | echo 205 | echo "CLI Function is:" 206 | echo " - with no arguments it launches the PiSafe menu (recommended)" 207 | echo " list - list media and image-files " 208 | echo " backup [media] [file] [-y] - backup media to image-file (-y also supresses check-for-updates)" 209 | echo " restore [file] [media] [-y] - restore image-file to media" 210 | echo " details [media] - show media details" 211 | echo " erase [media] [format] [-y] - format media as fat32 exfat ntfs or ext4" 212 | echo " install [-y] - install this script" 213 | echo " update [-y] - update script from website" 214 | echo " uninstall [-y] - uninstall this script" 215 | echo " settings - edit the settings file with nano" 216 | echo " defaults - restore settings to defaults" 217 | echo " log - view the log file with nano" 218 | echo " -v - display version" 219 | echo " about - display about file" 220 | echo " help | -h | --h - help" 221 | echo 222 | echo "Notes: " 223 | echo " - Supports .img .zip .xz .gz and .zst files. Appends '.img.$DEFAULT_EXTENSION' if no extension is specified " 224 | echo " - Specifying an '.img' extension is faster but twice the size because it is not compressed." 225 | echo " - -y answers 'y' to prompts." 226 | echo " - There are a number of settings that can be changed in the settings file. " 227 | echo 228 | echo 229 | echo "Examples:" 230 | echo "$SCRIPTNAME " 231 | echo "$SCRIPTNAME list" 232 | echo "$SCRIPTNAME backup sda newimage" 233 | echo "$SCRIPTNAME backup /dev/sdb newimage.xz -y" 234 | echo "$SCRIPTNAME restore newimage.img.zip sda" 235 | echo "$SCRIPTNAME backup /dev/sda /home/pi/Downloads/backup_\$(date +%Y-%d-%m_%I%M%p).img.gz -y" 236 | echo "$SCRIPTNAME format /dev/sda fat32" 237 | echo "$SCRIPTNAME settings" 238 | echo "$SCRIPTNAME log" 239 | echo 240 | } 241 | 242 | 243 | pisafe_install(){ 244 | SILENT=${1:-"-n"} 245 | 246 | if [[ $SILENT != -y ]] && [[ $(ui_yesno INSTALL "Install $PRODUCTNAME $SCRIPTVER?") != y ]]; then 247 | echo 248 | ui_msg NOTICE "~ $PRODUCTNAME not installed" "" "" log 249 | return 250 | fi 251 | 252 | echo 253 | ui_echo "Installing $PRODUCTNAME v $SCRIPTVER..." white 254 | 255 | # Install the CLI script 256 | sudo cp --backup=numbered $SCRIPTNAME /usr/local/bin/$SCRIPTNAME 257 | sudo chmod +x /usr/local/bin/$SCRIPTNAME 258 | echo Installed ver=$(/usr/local/bin/$SCRIPTNAME -v) 259 | 260 | # download the icon 261 | wget https://raw.githubusercontent.com/RichardMidnight/pi-safe/main/pisafe_icon.png -O pisafe_icon.png 262 | mv pisafe_icon.png /home/$USER/.config/pisafe/pisafe_icon.png 263 | 264 | # create the desktop file 265 | TERMINAL1=$(env_terminal) 266 | echo "\ 267 | [Desktop Entry] 268 | Type=Application 269 | Terminal=false 270 | Version=1.0 271 | Name=$PRODUCTNAME 272 | Comment=$PRODUCTCOMMENT 273 | #Icon=rpi-imager 274 | #Icon=media-removable 275 | Icon=/home/$USER/.config/pisafe/pisafe_icon.png 276 | #Exec=lxterminal --geometry=110x40 -e $SCRIPTNAME 277 | Exec=$TERMINAL1 $SCRIPTNAME 278 | Categories=Utility 279 | StartupNotify=false" > "$PRODUCTNAME.desktop" 280 | 281 | # for lxde, MATE and xfce put it in /usr/share/applications 282 | sudo mv "$PRODUCTNAME.desktop" "/usr/share/applications" 283 | # for just this user, put it in $HOME/.local/share/applications/ 284 | #mv "$PRODUCTNAME.desktop" "$HOME/.local/share/applications/" 285 | 286 | if [[ -f /usr/local/bin/$SCRIPTNAME ]]; then 287 | MSG="$PRODUCTNAME $SCRIPTVER installed. 288 | \nSelect it from the 'Accessories' menu. 289 | \n\nOr you can execute it from any directory by typing '$SCRIPTNAME'." 290 | 291 | if [[ $INTERFACE = cli ]]; then 292 | MSG="$PRODUCTNAME $SCRIPTVER installed. Select it from the 'Accessories' menu or type '$SCRIPTNAME' in a terminal." 293 | fi 294 | 295 | else 296 | MSG="$PRODUCTNAME $SCRIPTVER not installed. '/usr/local/bin/$SCRIPTNAME' not found. " 297 | fi 298 | 299 | ui_msg INSTALL "~ $MSG" "" "" log 300 | } 301 | 302 | 303 | pisafe_install_tool() { 304 | local TOOL=$1 305 | #SILENT=${1:-"-n"} 306 | local INSTALL=$(env_installer) 307 | 308 | if [[ -z $(env_which $TOOL) ]] ; then 309 | case $TOOL in 310 | 311 | xz) 312 | ui_echo "Installing xz-utils..." white 313 | sudo $INSTALL xz-utils 314 | if [[ -z $(env_which xz) ]]; then 315 | ui_msg_warning $LINENO "xz not installed." 316 | fi 317 | ;; 318 | 319 | pishrink.sh) 320 | ui_echo "Installing pishrink..." white 321 | wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh 322 | sudo chmod +x pishrink.sh 323 | sudo mv pishrink.sh /usr/local/bin 324 | 325 | if [[ -z $(env_which pishrink.sh) ]] ; then 326 | ui_msg_warning $LINENO "pishrink not installed." 327 | fi 328 | ;; 329 | 330 | *) 331 | #echo_white Installing $TOOL ... 332 | ui_echo "Installing $TOOL..." white 333 | sudo $INSTALL $TOOL 334 | if (( $? )); then 335 | ui_msg_warning $LINENO "$TOOL not installed." 336 | fi 337 | ;; 338 | esac 339 | fi 340 | } 341 | 342 | 343 | pisafe_install_tools() { 344 | local MISSING_TOOLS= 345 | 346 | #scan for missing tools 347 | for TOOL in $REQUIRED_TOOLS ; do 348 | if [[ -z $(env_which $TOOL) ]]; then 349 | MISSING_TOOLS="$MISSING_TOOLS $TOOL" 350 | fi 351 | done 352 | 353 | # if nothing missing, return; else silent or ask 354 | if [[ -z $MISSING_TOOLS ]]; then 355 | return 356 | elif [[ $SILENT = -n ]] && [[ $(ui_yesno "Install dependencies:'$MISSING_TOOLS '") = n ]]; then 357 | echo 358 | return 359 | fi 360 | echo 361 | 362 | for TOOL in $REQUIRED_TOOLS ; do 363 | pisafe_install_tool $TOOL 364 | done 365 | 366 | # if whiptail did not install then try installing newt... 367 | if ! [[ $(env_which whiptail) ]]; then 368 | local INSTALL=$(env_installer) 369 | run_command "sudo $INSTALL newt" # for fedora 370 | run_command "sudo $INSTALL libnewt" # for arch, manjaro 371 | fi 372 | } 373 | 374 | 375 | pisafe_update() { 376 | INTERFACE=${1:-"cli"} 377 | SUMMARY=${2:-"yes"} 378 | local YES=${3:-"-n"} 379 | 380 | if [[ $SUMMARY = yes ]]; then 381 | ui_echo "Checking for $PRODUCTNAME $SCRIPTVER updates..." white 382 | fi 383 | 384 | local SERVER_VER= 385 | wget https://raw.githubusercontent.com/RichardMidnight/pi-safe/main/$SCRIPTNAME -O $SCRIPTNAME.tmp 2> /dev/null 386 | ES=$? 387 | if (( $ES )); then 388 | echo_if_cli "ERROR: Can't connect to server..." 389 | return $ES 390 | fi 391 | 392 | # did we get the new version from the server? 393 | if [[ -f $SCRIPTNAME.tmp ]]; then 394 | SERVER_VER=$(bash $SCRIPTNAME.tmp -v) 395 | else 396 | return 1 397 | fi 398 | 399 | if [[ $(get_ver_to_int $SERVER_VER) -gt $(get_ver_to_int $SCRIPTVER) ]]; then 400 | if [[ $YES = -y ]]; then 401 | RESULT="y" 402 | else 403 | RESULT=$(ui_yesno "UPDATE AVAILABLE" "Update $PRODUCTNAME from '$SCRIPTVER' to '$SERVER_VER' ") 404 | echo 405 | fi 406 | 407 | if [[ $RESULT = y ]]; then 408 | ui_echo "Updating $PRODUCTNAME to v $SERVER_VER..." white 409 | sudo mv $SCRIPTNAME.tmp /usr/local/bin/$SCRIPTNAME 410 | sudo chmod +x /usr/local/bin/$SCRIPTNAME 411 | echo "$($SCRIPTNAME -v) installed. Press any key to exit... " 412 | read -s -n 1 413 | exit 0 414 | fi 415 | else 416 | rm $SCRIPTNAME.tmp 417 | if [[ $SUMMARY = yes ]]; then 418 | ui_msg UPDATE "~ $PRODUCTNAME $SCRIPTVER is up to date. Server ver is '$SERVER_VER' " "" "" log 419 | fi 420 | fi 421 | } 422 | 423 | 424 | pisafe_uninstall(){ 425 | SILENT=${1:-"-n"} 426 | 427 | if [[ -f /usr/local/bin/$SCRIPTNAME ]]; then 428 | INSTALLED_VER=$(/usr/local/bin/$SCRIPTNAME -v) 429 | fi 430 | 431 | if [[ $SILENT != -y ]] && [[ $(ui_yesno UNINSTALL "Uninstall $PRODUCTNAME $INSTALLED_VER" "--defaultno") != y ]] ; then 432 | echo 433 | ui_msg UNINSTALL "~ $PRODUCTNAME $INSTALLED_VER not uninstalled" "" "" log 434 | return 435 | fi 436 | 437 | echo 438 | ui_echo "Uninstalling $PRODUCTNAME v $INSTALLED_VER..." white 439 | 440 | # remove from the menu 441 | sudo rm -f "/usr/share/applications/$PRODUCTNAME.desktop" 442 | # next line cleans up an old configuration 443 | rm -f "$HOME/.local/share/applications/$PRODUCTNAME.desktop" 444 | 445 | # remove the script and config 446 | sudo rm -f "/usr/local/bin/$SCRIPTNAME" 447 | rm -f $CONFIG 448 | 449 | ui_msg UNINSTALL "~ $PRODUCTNAME $INSTALLED_VER uninstalled. $LOG_FILE not removed." "" "" log 450 | } 451 | 452 | 453 | # ---------- Config_var functions 454 | config_var_init_configfile(){ 455 | if [[ ! -d $(file_path $CONFIG) ]]; then 456 | mkdir -p $(file_path $CONFIG) 457 | fi 458 | 459 | if [[ ! -f $CONFIG ]]; then 460 | touch $CONFIG 461 | echo "# $CONFIG" > $CONFIG 462 | config_var_set_defaults 463 | else 464 | # update any missing values. false means dont force the update 465 | config_var_set_defaults false 466 | fi 467 | } 468 | 469 | 470 | config_var_set() { 471 | SETTING=$1 472 | VAL=$2 473 | local FORCE=${3:-true} # if false, will only update if var not set. 474 | 475 | #dont update the setting if thre is a value and we are not forcing new values 476 | if [[ $FORCE = false ]] && [[ ! -z $(config_var_get $SETTING) ]] ; then 477 | return 478 | fi 479 | 480 | case $(config_var_get $SETTING) in 481 | 482 | '') # add setting to end of file 483 | echo "$SETTING"="$VAL" >> $CONFIG 484 | ;; 485 | 486 | *) #update setting to val 487 | sed -i 's~'$SETTING'=.*$~'$SETTING'='$VAL'~' $CONFIG 488 | ;; 489 | 490 | esac 491 | } 492 | 493 | 494 | config_var_clear(){ 495 | SETTING=$1 496 | VAL=$2 497 | sed -i 's~'$SETTING'=.*$~'$VAR'=~' $CONFIG 498 | } 499 | 500 | 501 | config_var_get(){ 502 | SETTING=$1"=" 503 | cat $CONFIG | grep $SETTING | sed 's~'$SETTING'~~' 504 | } 505 | 506 | 507 | config_var_set_defaults(){ 508 | FORCE=${1:-true} # if false, will only update if var not set. 509 | 510 | # config_var_init_configfile 511 | config_var_set settings_script_ver $SCRIPTVER 512 | config_var_set default_path_cli_from_settings off $FORCE 513 | config_var_set default_path $HOME/Downloads $FORCE 514 | config_var_set check_for_updates_on_startup on $FORCE 515 | config_var_set check_dependencies_on_startup on $FORCE 516 | config_var_set debug_mode off $FORCE 517 | config_var_set sound on $FORCE 518 | config_var_set log on $FORCE 519 | config_var_set text_editor $(env_texteditor) $FORCE 520 | 521 | #backup settings 522 | config_var_set hide_root_device on $FORCE 523 | config_var_set shrink_fs on $FORCE 524 | config_var_set auto_expand_fs on $FORCE 525 | config_var_set default_extension zip $FORCE 526 | config_var_set compression_level 5 $FORCE 527 | config_var_set parallel_compression on $FORCE 528 | config_var_set large_device_read_warning 16gb $FORCE 529 | config_var_set skip_freespace on $FORCE 530 | 531 | #restore settings 532 | config_var_set large_device_write_warning 16gb $FORCE 533 | config_var_set safety on $FORCE 534 | } 535 | 536 | 537 | config_var_get_settings(){ 538 | SETTINGS_SCRIPT_VER=$(config_var_get settings_script_ver) 539 | DEFAULT_PATH_CLI_FROM_SETTINGS=$(config_var_get default_path_cli_from_settings) 540 | DEFAULT_PATH=$(config_var_get default_path) 541 | HIDE_ROOT_DEVICE=$(config_var_get hide_root_device) 542 | CHECK_FOR_UPDATES_ON_STARTUP=$(config_var_get check_for_updates_on_startup) 543 | CHECK_DEPENDENCIES_ON_STARTUP=$(config_var_get check_dependencies_on_startup) 544 | DEBUG_MODE=$(config_var_get debug_mode) 545 | SOUND=$(config_var_get sound) 546 | LOG=$(config_var_get log) 547 | VERIFY=off 548 | TEXT_EDITOR=$(config_var_get text_editor) 549 | 550 | # backup settings 551 | SHRINK_FS=$(config_var_get shrink_fs) 552 | AUTO_EXPAND_FS=$(config_var_get auto_expand_fs) 553 | DEFAULT_EXTENSION=$(config_var_get default_extension) 554 | COMPRESSION_LEVEL=$(config_var_get compression_level) 555 | LARGE_DEVICE_READ_WARNING=$(config_var_get large_device_read_warning) 556 | SKIP_FREESPACE=$(config_var_get skip_freespace) 557 | 558 | # restore settings 559 | LARGE_DEVICE_WRITE_WARNING=$(config_var_get large_device_write_warning) 560 | SAFETY=$(config_var_get safety) 561 | 562 | if [[ $INTERFACE = cli ]] && [[ $DEFAULT_PATH_CLI_FROM_SETTINGS != on ]]; then 563 | DEFAULT_PATH=$(pwd) 564 | fi 565 | mkdir -p "$DEFAULT_PATH" 566 | } 567 | 568 | 569 | # ---------- Enviroment functions 570 | env_installer(){ 571 | local INSTALLERS="apt pacman dnf" 572 | local INSTALLER= 573 | 574 | for INSTALLER in $INSTALLERS; do 575 | if [[ $(env_which $INSTALLER) ]]; then 576 | break 577 | fi 578 | done 579 | 580 | case $INSTALLER in 581 | apt|dnf) INSTALLER="$INSTALLER install -y" ;; 582 | pacman) INSTALLER="pacman -S --noconfirm" ;; 583 | esac 584 | echo "$INSTALLER" 585 | } 586 | 587 | 588 | env_root_device() { 589 | local ROOT_PARTITION 590 | local ROOT_DRIVE 591 | 592 | ROOT_MAJ=$(findmnt -n -e -o MAJ:MIN / | cut -d: -f1) 593 | ROOT_DEV=$(lsblk -p | grep $ROOT_MAJ:0 | cut -d" " -f1) 594 | echo $ROOT_DEV 595 | 596 | # new simpler way 597 | # ROOT_PARTITION=$(findmnt -no source /) 598 | } 599 | 600 | 601 | env_terminal(){ 602 | local TERMINAL1= 603 | #if [ -f /usr/bin/lxterminal ];then 604 | if [ $(env_which lxterminal) ];then 605 | TERMINAL1="lxterminal --geometry=110x40 --title='PiSafe' -e bash -c " 606 | elif [ $(env_which xfce4-terminal) ];then 607 | TERMINAL1="xfce4-terminal --geometry=110x40 --title='PiSafe' -x bash -c " 608 | elif [ $(env_which mate-terminal) ];then 609 | TERMINAL1="mate-terminal --geometry=110x40 --title='PiSafe' -x bash -c " 610 | elif [ $(env_which gnome-terminal) ];then 611 | TERMINAL1="gnome-terminal --geometry=110x40 --title='PiSafe' -- bash -c " 612 | elif [ $(env_which xterm) ];then 613 | TERMINAL1="xterm -geometry 110x40 -T 'PiSafe' -e bash -c " 614 | elif [ $(env_which terminator) ];then 615 | TERMINAL1="terminator --geometry=110x40 -T 'PiSafe' -x bash -c " 616 | elif [ $(env_which konsole) ];then 617 | TERMINAL1="konsole -e bash -c " 618 | elif [ $(env_which qterminal) ];then 619 | TERMINAL1="qterminal -e bash -c " 620 | elif [ $(env_which x-terminal-emulator) ];then 621 | TERMINAL1="$(readlink -f /usr/bin/x-terminal-emulator) -e bash -c " 622 | else 623 | echo "Failed to locate any terminal emulators. " 624 | return 1 625 | fi 626 | echo "$TERMINAL1" 627 | } 628 | 629 | 630 | env_texteditor(){ 631 | TEXT_EDITORS=" leafpad xed gedit featherpad kwrite pluma kate mousepad geany nano " 632 | for TEXT_EDITOR in $TEXT_EDITORS ; do 633 | if [[ $(env_which $TEXT_EDITOR) ]] ; then 634 | echo $TEXT_EDITOR 635 | return 636 | fi 637 | done 638 | } 639 | 640 | 641 | env_which () { 642 | #fixes manjaro's 'which' command 643 | local FILE=$1 644 | 645 | # if which not installed, try to install it 646 | which which 1>/dev/null 2>/dev/null 647 | if (( $? )); then 648 | sudo apt install which -y 649 | sudo pacman -S which --noconfirm 650 | sudo dnf install which -y 651 | fi 652 | 653 | which $FILE 1>/dev/null 2>/dev/null 654 | if (( $? )); then 655 | #echo false 656 | return 1 657 | else 658 | echo 1 659 | return 0 660 | fi 661 | } 662 | 663 | 664 | env_sysinfo(){ 665 | uname -a 666 | echo $(env_terminal) 667 | echo $(env_installer) 668 | echo $(env_texteditor) 669 | echo $(env_root_device) 670 | } 671 | 672 | 673 | # ---------- Misc functions 674 | do_beep(){ 675 | FREQ=${1:-600} 676 | TIME=${2:-.5} 677 | 678 | if [[ $SOUND = on ]] && [[ $(env_which speaker-test) ]]; then 679 | (speaker-test -t sign -f $FREQ > /dev/null & sleep $TIME && kill -9 $! ) > /dev/null 680 | # sleep .5 # to allow the beep to end 681 | fi 682 | } 683 | 684 | 685 | do_beep_up(){ 686 | do_beep 500 .3 687 | do_beep 600 .6 688 | } 689 | 690 | 691 | do_beep_down(){ 692 | do_beep 600 .3 693 | do_beep 500 .6 694 | } 695 | 696 | 697 | get_elapsed_time() { 698 | # paramaters are in seconds 699 | local BEG=$1 700 | local END=$2 701 | 702 | echo $(( $(( $END-$BEG ))/60)) min $(( $(( $END-$BEG ))%60 )) sec 703 | } 704 | 705 | 706 | do_countdown(){ 707 | local MAX=${1:-10} 708 | local MSG=${2:-"Pausing for $MAX seconds... Press y to continue immediately or any other key to stop"} 709 | local DEF=${3:-"y"} 710 | 711 | # echo "Pausing for $MAX seconds... Press y to continue immediately or any other key to stop" 712 | echo "$MSG" 713 | echo -n $MAX 714 | sleep 1 715 | for number in $(seq 1 $MAX) ; do 716 | echo -n ".$(($MAX-$number))" 717 | read -s -t 1 -N 1 INPUT 718 | if [[ ! -z $INPUT ]]; then 719 | if [[ $INPUT = y ]]; then 720 | echo Continuing... 721 | return 722 | else 723 | echo " $INPUT Stopping countdown..." 724 | return 1 725 | fi 726 | fi 727 | MSG=$(echo "$MSG.$i") 728 | done 729 | 730 | if [[ $DEF = n ]]; then 731 | return 1 732 | fi 733 | echo ... 734 | } 735 | 736 | 737 | get_ver_to_int() { 738 | local IFS=. 739 | parts=($1) 740 | 741 | let val=1000000*parts[0]+1000*parts[1]+parts[2] 742 | echo $val 743 | unset IFS 744 | } 745 | 746 | 747 | get_bytes(){ 748 | # input bytes or human and returns human or bytes 749 | # currently shows 3 significant digits. 750 | local BYTES=$1 751 | local OUTPUT=${2:-"-h"}; #human or bytes 752 | 753 | local BASE=$(echo $BYTES | tr -cd '[[:digit:]]' ) 754 | local SUFFIX=$(echo $BYTES | tr -cd '[[kmgtbKMGTB]]' ) 755 | 756 | if ! [[ $BASE =~ ^[0-9]+$ ]] ; then # not a number 757 | ui_msg_warning $LINENO "$BASE not a number" 758 | return 0 759 | fi 760 | 761 | # if no translation needed... then just echo the input and leave 762 | if [[ $OUTPUT != -h ]] && [[ -z $SUFFIX ]] ; then 763 | echo $BYTES 764 | return 765 | fi 766 | 767 | if [[ $OUTPUT = -h ]] && [[ ! -z $SUFFIX ]] ; then 768 | echo $BYTES 769 | return 770 | fi 771 | 772 | if [[ $OUTPUT = -h ]] ; then # translate to human readable 773 | 774 | if [[ -z $SUFFIX ]]; then 775 | local LEN=${#BYTES} 776 | local k_ilo=1024; 777 | local m_ega=$k_ilo*$k_ilo; 778 | local g_iga=$m_ega*$k_ilo; 779 | local t_era=$g_iga*$k_ilo; 780 | local p_eta=$t_era*$k_ilo; 781 | 782 | [[ -z $BYTES ]] && return # null, so exit 783 | 784 | if ! [[ $BYTES =~ ^[0-9]+$ ]] ; then # not a number 785 | echo 0 786 | return 1 787 | fi 788 | 789 | case $LEN in 790 | 4) echo $(echo "scale=2; $BYTES/($k_ilo)" | bc)kb ;; 791 | 5) echo $(echo "scale=1; $BYTES/($k_ilo)" | bc)kb ;; 792 | 6) echo $(echo "scale=0; $BYTES/($k_ilo)" | bc)kb ;; 793 | 794 | 7) echo $(echo "scale=2; $BYTES/($m_ega)" | bc)mb ;; 795 | 8) echo $(echo "scale=1; $BYTES/($m_ega)" | bc)mb ;; 796 | 9) echo $(echo "scale=0; $BYTES/($m_ega)" | bc)mb ;; 797 | 798 | 10) echo $(echo "scale=2; $BYTES/($g_iga)" | bc)gb ;; 799 | 11) echo $(echo "scale=1; $BYTES/($g_iga)" | bc)gb ;; 800 | 12) echo $(echo "scale=0; $BYTES/($g_iga)" | bc)gb ;; 801 | 802 | 13) echo $(echo "scale=2; $BYTES/($t_era)" | bc)tb ;; 803 | 14) echo $(echo "scale=1; $BYTES/($t_era)" | bc)tb ;; 804 | 15) echo $(echo "scale=0; $BYTES/($t_era)" | bc)tb ;; 805 | *) echo $BYTES ;; 806 | esac 807 | else 808 | echo $BYTES 809 | fi 810 | else # translate to bytes 811 | case $SUFFIX in 812 | b|B|"") echo $BASE ;; 813 | k|K|kb|KB) echo $(($BASE*1024)) ;; 814 | m|M|mb|MB) echo $(($BASE*1024*1024)) ;; 815 | g|G|gb|GB) echo $(($BASE*1024*1024*1024)) ;; 816 | t|T|tb|TB) echo $(($BASE*1024*1024*1024*2014)) ;; 817 | *) ui_msg_error $LINENO "bad byte suffix 'SUFFIX'" ;; 818 | esac 819 | fi 820 | } 821 | 822 | 823 | # ---------- file functions 824 | file_base(){ 825 | #returns the path and filename minus all extensions 826 | local DIR=$(dirname "$1") 827 | local BASE=$(basename "$1") 828 | 829 | # the %%. removes everything after the first . found 830 | BASE="${BASE%%.*}" 831 | 832 | echo "$DIR/$BASE" 833 | } 834 | 835 | 836 | file_device(){ 837 | MOUNT_POINT=$(df -a "$1" | sed 's/ */ /g' | grep -v Mounted | cut -d ' ' -f 6 ) 838 | BLK_DEV=$(lsblk -n -l -p -o PKNAME,MOUNTPOINT,NAME | sed 's/ */ /g' | grep " $MOUNT_POINT " | cut -d " " -f 1) 839 | echo $BLK_DEV 840 | } 841 | 842 | 843 | file_ext(){ 844 | echo $(basename "$*") | grep \\. | sed 's#.*\.##g' 845 | } 846 | 847 | 848 | file_folder_size() { 849 | local FOLDER=$* 850 | # returns human readable 851 | 852 | if [[ -d $FOLDER ]] ; then 853 | sudo du -sh "$FOLDER" | cut -d'/' -f1 | sed 's/\s\s*/ /g' 854 | ES=$? 855 | if (( $ES )); then 856 | return $ES 857 | fi 858 | else 859 | echo 0 860 | fi 861 | } 862 | 863 | 864 | file_fs_freespace() { 865 | local FILENAME=${1:-"."} 866 | local FORMAT=${2:-"-B1"} # -B1 for bytes -h for humanreadable 867 | local FREESPACE 868 | 869 | if [[ -d $FILENAME ]]; then 870 | #FREESPACE=$(df "$FILENAME" -B1 --output=avail | grep -v Avail) 871 | FREESPACE=$(df "$FILENAME" -B1 --output=avail | tr -cd '[[:digit:]]' ) 872 | ES=$? 873 | else 874 | #FREESPACE=$(df $(dirname "$FILENAME") -B1 --output=avail | grep -v Avail) 875 | FREESPACE=$(df $(dirname "$FILENAME") -B1 --output=avail | tr -cd '[[:digit:]]' ) 876 | ES=$? 877 | fi 878 | 879 | if (( $ES )) ; then 880 | return $ES 881 | fi 882 | 883 | if [[ $FORMAT = -h ]]; then 884 | FREESPACE=$(get_bytes $FREESPACE -h) 885 | fi 886 | 887 | echo $FREESPACE 888 | } 889 | 890 | 891 | file_image_size() { 892 | local INFILE="$1" 893 | local HUMAN=${2:-"no"} # or -h 894 | 895 | case $(file_ext "$INFILE") in 896 | img | iso) SIZE_BYTES=$(echo $(( $(ls -s "$INFILE" | cut -d' ' -f1 ) * 1024 )) ) ;; 897 | zip) SIZE_BYTES=$(zipinfo -t "$INFILE" 2> /dev/null | grep "%" | cut -d, -f2 | cut -d" " -f2 ) ;; 898 | xz) SIZE_BYTES=$(xz -l -v "$INFILE" 2> /dev/null | grep Uncompressed | sed 's/\s\s*/ /g' | cut -d'(' -f 2 | cut -d ' ' -f1 | sed 's/,//g') ;; 899 | gz) SIZE_BYTES=$(pigz -l "$INFILE" 2> /dev/null | grep -v compressed | sed 's/\s\s*/ /g' | sed -e 's/^[ \t]*//' | cut -d' ' -f 2 | sed 's/?/0/g') ;; 900 | # gz) SIZE_BYTES=$(pigz -l "$INFILE" 2> /dev/null | grep -v compressed | sed 's/\s\s*/ /g' | sed -e 's/^[ \t]*//' | cut -d' ' -f 2 ) ;; 901 | zst) SIZE_BYTES=$(zstd -v -l "$INFILE" 2> /dev/null | grep Decompressed | cut -d"(" -f2 | cut -d" " -f1) ;; 902 | *) echo 0 903 | return 1 ;; 904 | esac 905 | 906 | if [[ -z $SIZE_BYTES ]] ; then 907 | echo 0 908 | return 909 | fi 910 | 911 | if [[ $HUMAN = -h ]]; then 912 | echo $(get_bytes $SIZE_BYTES -h) 913 | else 914 | echo $SIZE_BYTES 915 | fi 916 | } 917 | 918 | 919 | file_list_image_files(){ 920 | # local DIR=$1 921 | local DIR=$DEFAULT_PATH 922 | 923 | OLD_PWD=$(pwd) 924 | cd $DIR 925 | 926 | FILES="FILE_NAME FILE_SIZE (IMAGE_SIZE) \n" 927 | IFS=$'\t\n' 928 | 929 | for FILE in $(ls *.img *.zip *.xz *.gz *.zst *.iso 2>/dev/null) ; do 930 | FILE_NS=$(echo "$FILE" | sed 's/ /_/g') 931 | FILES="$FILES $FILE_NS $(file_size "$FILE" -h) ($(file_image_size "$FILE" -h)) \n " 932 | done 933 | 934 | printf $FILES | column -t 935 | unset IFS 936 | 937 | cd $OLD_PWD 938 | } 939 | 940 | 941 | file_path(){ 942 | dirname "$*" 943 | } 944 | 945 | 946 | file_size() { 947 | local FILE="$1" 948 | local HUMAN=${2:-"no"} 949 | # $2 can be -h 950 | 951 | if [[ -f "$FILE" ]] ; then 952 | SIZE_BYTES="$(($(ls -s "$FILE" | cut -d' ' -f1) * 1024))" 953 | if (( $? )); then 954 | return 1 955 | fi 956 | if [[ $HUMAN = -h ]]; then 957 | echo $(get_bytes $SIZE_BYTES -h) 958 | else 959 | echo $SIZE_BYTES 960 | fi 961 | else 962 | echo 0 963 | fi 964 | } 965 | 966 | 967 | 968 | # ----------------------------------------------- 969 | 970 | do_list_info() { 971 | # DIR=${1:-$PWD} 972 | 973 | echo "~~~ CURRENT SYSTEM ~~~" 974 | echo $PRODUCTNAME ver $SCRIPTVER 975 | echo $(cat /etc/os-release | grep "PRETTY_NAME=" | cut -d '"' -f2) hw=$(uname -m) kernel=$(uname -r) 976 | echo "Root device = $(env_root_device)" 977 | echo "Root size = $(media_size $(env_root_device) -h )" 978 | echo "TrashSize = $(file_folder_size /home/$USER/.local/share/Trash)" 979 | echo "RootTrash = $(file_folder_size /root/.local/share/Trash)" 980 | echo 981 | echo 982 | echo "~~~ STORAGE MEDIA (hide root device = $HIDE_ROOT_DEVICE) ~~~" 983 | echo "$(media_list)" 984 | echo 985 | echo 986 | echo "~~~ IMAGE FILES in '$DEFAULT_PATH' ($(file_fs_freespace $DEFAULT_PATH -h) free) ~~~" 987 | file_list_image_files 988 | } 989 | 990 | 991 | 992 | # ---------- Media functions 993 | 994 | media_name(){ 995 | local VEN_MOD=$(lsblk -dpnlo VENDOR,MODEL $1 2> /dev/null | sed 's/ */ /g') 996 | ES=$? 997 | if (( $ES )); then 998 | return $ES 999 | fi 1000 | local SIZE=$(lsblk -dpnlo SIZE $1 2> /dev/null | sed 's/[ ]\+//g') 1001 | local DEVICE=$(lsblk -dpnlo NAME $1 2> /dev/null) 1002 | 1003 | echo "$VEN_MOD - $SIZE ($DEVICE)" 1004 | } 1005 | 1006 | 1007 | media_size() { 1008 | local DEV=$1 1009 | local HUMAN=${2:-"no"} 1010 | 1011 | # add '/dev/' if missing 1012 | if [[ ${DEV:0:5} != "/dev/" ]]; then 1013 | #echo "Device prefix not specified. Adding '/dev/'" 1014 | DEV="/dev/"$DEV 1015 | fi 1016 | 1017 | SIZE_BYTES=$(lsblk $DEV -dnbo size 2> /dev/null) 1018 | ES=$? 1019 | if (( $ES )); then 1020 | echo 0 1021 | return $ES 1022 | fi 1023 | 1024 | if [[ $HUMAN = -h ]]; then 1025 | echo $(get_bytes $SIZE_BYTES -h) 1026 | else 1027 | echo $SIZE_BYTES 1028 | fi 1029 | 1030 | # an alternative way: 1031 | # echo $(( $(sudo blockdev --getsize64 /dev/sda) )) 1032 | # echo $(( $(sudo blockdev --getsize64 /dev/sda)/1024/1024 ))mb 1033 | } 1034 | 1035 | 1036 | media_backup(){ 1037 | #echo 1 - sudo dd bs=4M if=/dev/$INDEV of=$OUTFILE.img status=progress conv=fsync 1038 | #echo 2 - sudo pishrink.sh $OUTFILE.img 1039 | #echo 3 - zip -db -dd -m $OUTFILE.zip $OUTFILE.img 1040 | 1041 | #local INDEV=$1 1042 | INDEV=$1 1043 | OUTFILE=$2 1044 | local SILENT=${3:-"-n"} # this lets us bypass some of the safety prompts 1045 | 1046 | 1047 | # preflight checklist. Be sure INDEV and OUTFILE are good. 1048 | ui_echo "$PRODUCTNAME $SCRIPTVER Backup... " white 1049 | media_backup_checklist $SILENT 1050 | ES=$? 1051 | if (( $ES )); then 1052 | return $ES 1053 | fi 1054 | 1055 | ui_echo "~ $PRODUCTNAME $SCRIPTVER Backup '$INDEV' to '$OUTFILE'" white 1056 | 1057 | ################################################# 1058 | # Step 1 reads the media to a file 1059 | ################################################# 1060 | OUTFILE_BASE=$(file_base "$OUTFILE") 1061 | 1062 | TIME1=$(date +%s) 1063 | ui_echo "~ Step 1 of 3 - Copying '$INDEV' to '$OUTFILE_BASE.img' ... " white log 1064 | date 1065 | 1066 | BS=$(( 4 * 1024 * 1024 )) 1067 | BYTES_TO_READ=$(media_backup_bytes_to_read) 1068 | BLOCKS_TO_READ=$(( $BYTES_TO_READ / $BS +1 )) 1069 | echo Media size=$(media_size $INDEV -h) 1070 | echo Skipping $(get_bytes $(( $(media_size $INDEV) - $BYTES_TO_READ )) -h ) of freespace at end of media. 1071 | echo Reading $(get_bytes $BYTES_TO_READ -h)... 1072 | 1073 | if [[ ! -z $(which pv) ]]; then 1074 | run_command "sudo pv $INDEV -p -s $BYTES_TO_READ $DEVICE | sudo dd bs=$BS of='$OUTFILE_BASE.img' count=$BLOCKS_TO_READ iflag=fullblock conv=fsync " $LINENO 1075 | ES=$? 1076 | else 1077 | ui_echo "No progress bar installed. Please wait..." 1078 | PROGRESS= 1079 | if (( $(dd --help | grep progress) )) ; then 1080 | PROGRESS="status=progress" 1081 | fi 1082 | run_command "sudo dd bs=$BS if=$INDEV of='$OUTFILE_BASE.img' count=$BLOCKS_TO_READ iflag=fullblock $PROGRESS conv=fsync " $LINENO 1083 | ES=$? 1084 | fi 1085 | if (( $ES )); then 1086 | do_beep_down 1087 | return 20 1088 | fi 1089 | sleep 5s 1090 | echo "Done copying media '$INDEV'" 1091 | echo_white "'$(ls -s -h "$OUTFILE_BASE".img)'" 1092 | TIME2=$(date +%s) 1093 | echo_white Step 1 took $(get_elapsed_time $TIME1 $TIME2) 1094 | echo 1095 | 1096 | ################################################# 1097 | # Step 2 shrinks the fs 1098 | ################################################# 1099 | ui_echo "~ Step 2 of 3 - Shrinking filesystem ..." white log 1100 | if [[ ! $(file_ext "$OUTFILE") = iso ]]; then 1101 | PI_SHRINK_OPTS= 1102 | if [[ $AUTO_EXPAND_FS = off ]] ; then 1103 | PI_SHRINK_OPTS="-s" 1104 | fi 1105 | if [[ $SHRINK_FS = off ]]; then 1106 | ui_echo "~ Filesystem not shrunk. Setting is 'off'." red log 1107 | else 1108 | run_command "sudo pishrink.sh $PI_SHRINK_OPTS '$OUTFILE_BASE.img' " $LINENO 1109 | if (( $? )); then 1110 | ui_echo "~ Continuing without shrinking the file system..." red log 1111 | fi 1112 | fi 1113 | sleep 5 # to allow file size to register with the OS 1114 | echo Done shrinking filesystem. 1115 | echo_white "'$(ls -s -h "$OUTFILE_BASE".img)'" 1116 | else 1117 | echo Not shrinking iso file 1118 | fi 1119 | TIME3=$(date +%s) 1120 | echo_white Step 2 took $(get_elapsed_time $TIME2 $TIME3) 1121 | 1122 | ################################################# 1123 | # Step 3 compresses the file 1124 | ################################################# 1125 | echo 1126 | ui_echo "~ Step 3 of 3 - Compressing '$OUTFILE_BASE.img' to '$OUTFILE' ... " white log 1127 | date 1128 | echo Compression set to level $COMPRESSION_LEVEL of 9 1129 | echo "$(file_size "$OUTFILE_BASE".img -h) to compress." 1130 | 1131 | BEST_COMPRESSION=on 1132 | BEST_COMPRESSION_SETTING= 1133 | 1134 | case $(file_ext "$OUTFILE") in 1135 | img) 1136 | echo Not compressing .img file ... 1137 | ES= 1138 | ;; 1139 | 1140 | iso) 1141 | echo Not compressing .iso file ... 1142 | mv "$OUTFILE_BASE".img "$OUTFILE_BASE".iso 1143 | ES=$? 1144 | OUTFILE="$OUTFILE_BASE".iso 1145 | ;; 1146 | 1147 | zip) 1148 | # not using pv because it supresses the name of the file in the archive. 1149 | # added sudo to the command so manjaro can write to a network folder. 1150 | local FILESIZE_M=$(( $(file_size "$OUTFILE_BASE".img)/1024/1024 )) 1151 | local DOTSIZE=$(( $FILESIZE_M / 50 ))m 1152 | echo -n " $OUTFILE_BASE.img" 1153 | echo " [........................|.........................] 100%" 1154 | run_command "zip -dbds $DOTSIZE -m -$COMPRESSION_LEVEL '$OUTFILE' '$OUTFILE_BASE.img'" $LINENO 1155 | ES=$? 1156 | ;; 1157 | 1158 | xz) 1159 | if [[ $BEST_COMPRESSION = on ]]; then 1160 | BEST_COMPRESSION_SETTING="-e" 1161 | fi 1162 | run_command "sudo xz -z -T0 -v $BEST_COMPRESSION_SETTING -$COMPRESSION_LEVEL '$OUTFILE_BASE.img'" $LINENO 1163 | ES=$? 1164 | ;; 1165 | 1166 | gz) 1167 | if [[ $BEST_COMPRESSION = on ]]; then 1168 | BEST_COMPRESSION_SETTING="--best" 1169 | fi 1170 | if [[ ! -z $(which pv) ]]; then 1171 | run_command "pv '$OUTFILE_BASE.img' -p | pigz $BEST_COMPRESSION_SETTING= -$COMPRESSION_LEVEL > '$OUTFILE'" $LINENO 1172 | ES=$? 1173 | rm -f "$OUTFILE_BASE".img 1174 | else 1175 | ui_echo "No progress bar installed. Please wait..." 1176 | sudo chmod 777 '$OUTFILE_BASE.img' 1177 | run_command "pigz -$COMPRESSION_LEVEL '$OUTFILE_BASE.img'" $LINENO 1178 | ES=$? 1179 | fi 1180 | ;; 1181 | 1182 | zst) 1183 | if [[ $COMPRESSION_LEVEL -gt 19 ]]; then 1184 | COMPRESSION_LEVEL="-ultra -$COMPRESSION_LEVEL" 1185 | fi 1186 | run_command "zstd -T0 -$COMPRESSION_LEVEL --rm '$OUTFILE_BASE.img'" $LINENO 1187 | ES=$? 1188 | ;; 1189 | 1190 | *) 1191 | echo 1192 | ui_msg_error $LINENO "Unsupported file extension '$(file_ext "$OUTFILE")'" 1193 | ES=1 1194 | ;; 1195 | esac 1196 | 1197 | if (( $ES )); then 1198 | do_beep_down 1199 | return 22 1200 | fi 1201 | 1202 | sleep 8 1203 | 1204 | echo "Done compressing '$OUTFILE_BASE.img' to '$OUTFILE' " 1205 | echo_white $(ls -s -h "$OUTFILE") 1206 | TIME4=$(date +%s) 1207 | echo_white Step 3 took $(get_elapsed_time $TIME3 $TIME4) 1208 | 1209 | # check if "$OUTFILE" was created 1210 | if [[ ! -f $OUTFILE ]]; then 1211 | ui_msg_error $LINENO "Error creating '$OUTFILE'" 1212 | return 23 1213 | fi 1214 | 1215 | do_beep_up 1216 | ui_msg BACKUP "~ Backup complete. \n'$(media_name $INDEV)' backed up to \n'$OUTFILE' \n$(media_size $INDEV -h) reduced to $(file_size "$OUTFILE" -h) in $(get_elapsed_time $TIME1 $TIME4)." white "" log 1217 | } 1218 | 1219 | 1220 | media_backup_bytes_to_read(){ 1221 | # returns the media size skipping the freespace at the end. 1222 | 1223 | BYTES_TO_READ=$(media_size $INDEV) 1224 | 1225 | if [[ $SKIP_FREESPACE = on ]]; then 1226 | START_OF_FREESPACE=$(sudo parted -ms "$INDEV" unit B print free) 1227 | ES=$? 1228 | if (( $ES )); then 1229 | ui_msg_warning $LINENO "parted failed with es $ES" 1230 | #exit 1 1231 | fi 1232 | START_OF_FREESPACE=$(tail -1 <<< "$START_OF_FREESPACE" | grep free | cut -d ':' -f 2 | tr -d 'B') 1233 | if (( $START_OF_FREESPACE )); then 1234 | BYTES_TO_READ=$START_OF_FREESPACE 1235 | fi 1236 | fi 1237 | 1238 | # consider if we can drop a swap file if at the very end. 1239 | 1240 | echo $BYTES_TO_READ 1241 | } 1242 | 1243 | 1244 | media_backup_check_indev(){ 1245 | #returns 0 if good 1246 | # check INDEV 1247 | 1248 | # stop if indev is blank 1249 | if [[ -z $INDEV ]] ; then 1250 | ui_msg_error $LINENO "IN-DEV '$INDEV' can't be blank" 1251 | return 11 1252 | fi 1253 | 1254 | # add '/dev/' if missing 1255 | if [[ ${INDEV:0:5} != "/dev/" ]]; then 1256 | echo "Device prefix not specified. Adding '/dev/'" 1257 | INDEV="/dev/"$INDEV 1258 | echo "IN-DEV='$INDEV'" 1259 | fi 1260 | 1261 | # Stop if INDEV does not exists 1262 | if [[ ! -e $INDEV ]] ; then 1263 | ui_msg_error $LINENO "IN-DEV '$INDEV' not found" 1264 | return 12 1265 | fi 1266 | 1267 | # Warning if INDEV is root device 1268 | if [[ $INDEV = $(env_root_device) ]]; then 1269 | ui_msg_warning $LINENO "SD-card '$(media_name $INDEV)' is root device '$(env_root_device)'." 1270 | fi 1271 | 1272 | # Warning if device filesystem has an overlay 1273 | OVERLAY=$(media_os $INDEV | grep OVERLAY) 1274 | if [[ ! -z $OVERLAY ]] && [[ $AUTO_EXPAND_FS = on ]]; then 1275 | if [[ $(ui_yesno WARNING "'$(media_name $INDEV)' filesystem has an overlay and cannot auto-expand. Turn off auto-expand?" ) = y ]]; then 1276 | echo 1277 | AUTO_EXPAND_FS=off 1278 | ui_msg_warning $LINENO "Overlay found. Auto-expand turned off" 1279 | fi 1280 | fi 1281 | 1282 | # Warning if media is large 1283 | if [[ $(media_size $INDEV) -gt $(get_bytes $LARGE_DEVICE_READ_WARNING -b) ]]; then 1284 | MESG="'$(media_name $INDEV)' is larger than $(get_bytes $LARGE_DEVICE_READ_WARNING -h). 1285 | Use a smaller media if you can. 1286 | It will be faster because the entire media must be read before it can be compressed. 1287 | A good 8gb card with RaspiOS in a Raspberry Pi 4 takes about 10 minutes" 1288 | ui_msg_warning $LINENO "$MESG" 1289 | fi 1290 | 1291 | 1292 | # warning if unused space is > something or some percent. 1293 | 1294 | 1295 | } 1296 | 1297 | 1298 | media_backup_check_outfile(){ 1299 | # uses global variable OUTFILE 1300 | # returns 0 if good 1301 | 1302 | #Quit if outfile is blank 1303 | if [[ -z $OUTFILE ]] ; then 1304 | ui_msg_error $LINENO "OutFile '$OUTFILE' can't be blank" 1305 | return 13 1306 | fi 1307 | 1308 | # Quit if path does not exist 1309 | if [[ ! -d $(file_path $OUTFILE) ]] ; then 1310 | ui_msg_error $LINENO "Directory does not exist '$(file_path $OUTFILE)'" 1311 | return 14 1312 | fi 1313 | 1314 | # add ext if missing 1315 | if [[ -z $(file_ext "$OUTFILE") ]]; then 1316 | echo "No extension specified. Adding '.img.$DEFAULT_EXTENSION'" 1317 | OUTFILE=$OUTFILE.img.$DEFAULT_EXTENSION 1318 | #OUTFILE_EXT=$(file_ext $OUTFILE) 1319 | echo "OUT-FILE='$OUTFILE'" 1320 | fi 1321 | 1322 | # insert .img if missing 1323 | if [[ $(echo "$OUTFILE" | grep img) = "" ]] && [[ $(echo "$OUTFILE" | grep iso) = "" ]]; then 1324 | echo "Inserting .img" 1325 | OUTFILE=$(file_base "$OUTFILE").img.$(file_ext "$OUTFILE") 1326 | echo "OUT-FILE='$OUTFILE'" 1327 | fi 1328 | 1329 | # quit if unsupported extension 1330 | FILE_EXT=$(file_ext "$OUTFILE") 1331 | case $FILE_EXT in 1332 | img | zip | iso | "" ) ;; 1333 | xz) pisafe_install_tool xz ;; 1334 | gz) pisafe_install_tool pigz ;; 1335 | zst) pisafe_install_tool zstd ;; 1336 | *) 1337 | ui_msg_error $LINENO "Unsupported file extension '$FILE_EXT'" 1338 | return 15 1339 | ;; 1340 | esac 1341 | 1342 | # Quit if outfile exists 1343 | if [[ -f $OUTFILE ]] ; then 1344 | ui_msg_error $LINENO "File '$OUTFILE' already exists" 1345 | return 16 1346 | fi 1347 | 1348 | # Quit if OUTFILE_BASE.img exists 1349 | if [[ -f $(file_base $OUTFILE).img ]] ; then 1350 | ui_msg_error $LINENO "File '$(file_base $OUTFILE).img' already exists" 1351 | return 17 1352 | fi 1353 | 1354 | # Quit if media is bigger than freespace 1355 | PARTED_OUTPUT=$(sudo parted $INDEV unit B print free ) 1356 | 1357 | MEDIA_SIZE=$(media_backup_bytes_to_read) 1358 | echo "$(get_bytes $MEDIA_SIZE -h) to read from '$INDEV'" 1359 | 1360 | AVAILABLE_SPACE=$(file_fs_freespace $(file_path "$OUTFILE")) 1361 | echo "$(get_bytes $AVAILABLE_SPACE -h) available space on '$(file_path "$OUTFILE")' " 1362 | if [[ $MEDIA_SIZE -gt $AVAILABLE_SPACE ]]; then 1363 | MSG="Not enough free space on '$(file_path "$OUTFILE")' 1364 | $(get_bytes $MEDIA_SIZE -h) to backup on '$(media_name $INDEV)' 1365 | $(get_bytes $AVAILABLE_SPACE -h) available space on '$(file_path "$OUTFILE")' " 1366 | ui_msg_error $LINENO "$MSG" 1367 | return 19 1368 | fi 1369 | 1370 | # Warning if one and a half times the media is bigger than freespace 1371 | SPACENEEDED=$(( $MEDIA_SIZE + $(( $MEDIA_SIZE / 2 )) )) 1372 | if [[ $SPACENEEDED -gt $AVAILABLE_SPACE ]]; then 1373 | MSG="Might not be enough free space for backup. 1374 | $(get_bytes $MEDIA_SIZE -h) to backup on '$(media_name $INDEV)' 1375 | $(get_bytes $AVAILABLE_SPACE -h) available space on '$(file_path "$OUTFILE")' " 1376 | ui_msg_warning $LINENO "$MSG" 1377 | fi 1378 | } 1379 | 1380 | 1381 | media_backup_checklist(){ 1382 | local SILENT=$1 1383 | 1384 | # get and check indev 1385 | if [[ -z $INDEV ]]; then 1386 | INDEV=$(menu_select_device "BACKUP") 1387 | ES=$? 1388 | if (( $ES )); then 1389 | # ui_msg_error $LINENO "No media found." 1390 | ui_msg_error $LINENO "Media '$INDEV' can't be blank." 1391 | return 10 1392 | fi 1393 | fi 1394 | media_backup_check_indev 1395 | ES=$? 1396 | if (( $ES )); then 1397 | return $ES 1398 | fi 1399 | 1400 | #get and check outfile 1401 | if [[ -z $OUTFILE ]]; then 1402 | menu_get_outfile # this includes media_backup_check_outfile 1403 | ES=$? 1404 | if (( $ES )); then 1405 | ui_msg_error $LINENO "Outfile '$OUTFILE' can't be blank" 1406 | return $ES 1407 | fi 1408 | else 1409 | media_backup_check_outfile 1410 | ES=$? 1411 | if (( $ES )); then 1412 | return $ES 1413 | fi 1414 | fi 1415 | 1416 | #confirm to continue 1417 | if [[ $SILENT != -y ]]; then 1418 | if [[ $INTERFACE = gui ]]; then 1419 | MSG="Backup '$(media_name $INDEV)' 1420 | to '$OUTFILE'. \n\n 1421 | A good 8GB SD-card with RaspiOS on a Pi4 takes about 11 minutes with zip 1422 | and about 8 minutes with gz. \n\n 1423 | Switch to terminal and create image-file now?" 1424 | else 1425 | MSG="Backup '$(media_name $INDEV)' to '$OUTFILE' now?" 1426 | fi 1427 | if [[ $(ui_yesno BACKUP "$MSG" --defaultno) != y ]]; then 1428 | ui_log "~ Backup canceled" 1429 | echo 1430 | return 1 1431 | else 1432 | echo 1433 | fi 1434 | fi 1435 | } 1436 | 1437 | 1438 | media_backup_estimate() { 1439 | local MEDIA=$1 1440 | local MSG= 1441 | 1442 | if [[ ! -e $MEDIA ]]; then 1443 | MSG="Error: Media '$MEDIA' does not exist" 1444 | return 1 1445 | fi 1446 | 1447 | MEDIA_SIZE=$(media_size $MEDIA) 1448 | PARTED_OUTPUT=$(sudo parted -ms $MEDIA print) # no freespace 1449 | MEDIA_PARTITION_TABLE=$(echo "$PARTED_OUTPUT" | head -n 2 | tail -n 1 | cut -d: -f6) # msdos, gpt, other 1450 | MEDIA_PARTITION_COUNT=$(echo "$PARTED_OUTPUT" | grep ^[1-9]: | grep -v ":::;" | cut -d: -f1 | wc -l) 1451 | MEDIA_PARTITION_LIST=$(echo "$PARTED_OUTPUT" | grep ^[1-9]: | grep -v ":::;" | cut -d: -f1) 1452 | MEDIA_LAST_PARTITION_NUMBER=$(echo "$PARTED_OUTPUT" | tail -1 | cut -d: -f1) 1453 | MEDIA_LAST_PARTITION_FS=$(echo "$PARTED_OUTPUT" | tail -1 | cut -d: -f5) 1454 | 1455 | case $MEDIA in 1456 | /dev/sd*) MEDIA_LAST_PARTITION_NAME=$MEDIA$MEDIA_LAST_PARTITION_NUMBER ;; 1457 | /dev/mmcblk*|/dev/nvme*) MEDIA_LAST_PARTITION_NAME=$MEDIA"p"$MEDIA_LAST_PARTITION_NUMBER ;; 1458 | esac 1459 | 1460 | PARTED_OUTPUT=$(sudo parted $MEDIA unit B print free ) 1461 | 1462 | MEDIA_FREESPACE_AT_END=$(echo "$PARTED_OUTPUT" | tail -1 | grep Free | sed 's/[ ]\+/ /g' | cut -d " " -f 4 | tr -d 'B' ) 1463 | if [[ -z $MEDIA_FREESPACE_AT_END ]]; then 1464 | MEDIA_FREESPACE_AT_END=0 1465 | fi 1466 | 1467 | if [[ -z $(echo "$PARTED_OUTPUT" | grep -v Free | tail -1 | sed 's/[ ]\+/ /g' | grep primary) ]]; then 1468 | MEDIA_LAST_PARTITION_TYPE=logical 1469 | else 1470 | MEDIA_LAST_PARTITION_TYPE=primary 1471 | fi 1472 | 1473 | # MEDIA_LAST_PARTITION_FS=$(lsblk -no FSTYPE $MEDIA$MEDIA_LAST_PARTITION_NUMBER ) 1474 | 1475 | MSG=$MSG"\n===== $PRODUCTNAME BACKUP ESTIMATE ===== \n" 1476 | MSG=$MSG"Media size = $(get_bytes $MEDIA_SIZE -h)\n" 1477 | 1478 | 1479 | #1 - free space 1480 | MSG=$MSG$(printf '%-35.34s' "Remove '$(get_bytes $MEDIA_FREESPACE_AT_END -h)' of freespace") 1481 | MSG=$MSG"=> $(get_bytes $(($MEDIA_SIZE - MEDIA_FREESPACE_AT_END )) -h ) \n" 1482 | 1483 | 1484 | # 2 - is it a complex file system? 1485 | if [[ $MEDIA_PARTITION_COUNT -gt 3 ]]; then 1486 | MSG=$MSG" Warning: Partitions=$MEDIA_PARTITION_COUNT. This looks like a complex \n" 1487 | MSG=$MSG" partition structure and may not shrink. Pisafe will still work.\n" 1488 | fi 1489 | 1490 | 1491 | # 3 - ext and not logical? 1492 | SHRINK_BYTES=0 1493 | UNCOMPRESSED_BYTES=0 1494 | case $MEDIA_LAST_PARTITION_FS in 1495 | 1496 | ext4|ext3|ext2) 1497 | if [[ $MEDIA_PARTITION_TABLE = gpt ]] || ( [[ $MEDIA_PARTITION_TABLE = msdos ]] && [[ $MEDIA_LAST_PARTITION_TYPE = primary ]] ) ; then 1498 | SHRINK_BYTES=$(sudo lsblk $MEDIA -lpbo name,fsavail | grep $MEDIA_LAST_PARTITION_NAME | sed 's/^[[:blank:]]*//;s/[ ]\+/ /g' | cut -d " " -f2) 1499 | else 1500 | MSG=$MSG" Warning: The last partition type is '$MEDIA_LAST_PARTITION_TYPE'. This partition-type will not shrink and cannot be restored to a smaller media. Pisafe will still work.\n" 1501 | fi 1502 | ;; 1503 | 1504 | *) 1505 | MSG=$MSG" Warning: The filesystem on the last partition is '$MEDIA_LAST_PARTITION_FS'. \n This filesystem will not shrink and cannot be restored to a smaller media. Pisafe will still work.\n" 1506 | # need to verify next line works. 1507 | ui_echo "~ Warning, The filesystem on the last partition is '$MEDIA_LAST_PARTITION_FS' and will not shrink." red log 1508 | ;; 1509 | esac 1510 | 1511 | if [[ -z $SHRINK_BYTES ]]; then 1512 | SHRINK_BYTES=0 1513 | fi 1514 | 1515 | 1516 | # 4 Shrink Filesystem 1517 | UNCOMPRESSED_BYTES=$(( $MEDIA_SIZE - $MEDIA_FREESPACE_AT_END - $SHRINK_BYTES )) 1518 | MSG=$MSG$(printf '%-35.34s' "Shrink filesystem by $(get_bytes $SHRINK_BYTES -h) ") 1519 | MSG=$MSG"=> $(get_bytes $UNCOMPRESSED_BYTES -h) \n" 1520 | 1521 | 1522 | # 5 Compression 1523 | MSG=$MSG"Compress about 2:1 => $(get_bytes $((UNCOMPRESSED_BYTES / 2 )) -h) \n" 1524 | 1525 | echo "$MSG" 1526 | } 1527 | 1528 | 1529 | media_restore(){ 1530 | local INFILE="$1" 1531 | local OUTDEV=$2 1532 | local SILENT=${3:-"-n"} # this lets us bypass some of the safety prompts 1533 | # INTERFACE=${4:-cli} #CLI or gui 1534 | 1535 | 1536 | # preflight checklist. Be sure INFILE and OUTDEV are good. 1537 | media_restore_checklist 1538 | if (( $? )); then 1539 | return 1 1540 | fi 1541 | 1542 | 1543 | echo 1544 | ui_countdown 10 "RESTORE" # "Overwriting '$(media_name $OUTDEV)' in 10 seconds...\n\n" 1545 | if (( $? )); then 1546 | ui_msg RESTORE "~ Restore stopped." "" "" log 1547 | return 2 1548 | fi 1549 | 1550 | ################################################# 1551 | # this section writes the data 1552 | ################################################# 1553 | 1554 | echo_if_cli "Unmounting device $OUTDEV ..." "white" 1555 | umount $OUTDEV? 2> /dev/null 1556 | 1557 | local INFILEEXT=$(file_ext "$INFILE") 1558 | 1559 | echo 1560 | echo_white "Writing '$INFILE' to '$OUTDEV' ... " 1561 | date 1562 | TIME1=$(date +%s) 1563 | echo_if_cli "$(file_image_size "$INFILE" -h) to write" 1564 | 1565 | ui_echo "~ Erasing MBR, signature, and partition table from '$OUTDEV'..." 1566 | run_command "sudo dd if=/dev/zero of=$OUTDEV bs=512 count=2 > /dev/null" $LINENO 1567 | 1568 | case $INFILEEXT in 1569 | img | iso) 1570 | if [[ $INTERFACE = cli ]]; then 1571 | run_command "pv '$INFILE' -p | sudo dd of=$OUTDEV bs=4M conv=fsync" 1572 | ES=$? 1573 | else 1574 | ui_echo "~ Running: sudo pv -n '$INFILE' | sudo dd of=$OUTDEV bs=4M conv=fsync" blue 1575 | (sudo pv -n "$INFILE" | sudo dd of=$OUTDEV bs=4M) 2>&1 | \ 1576 | whiptail --backtitle "$BACKTITLE" --title "RESTORE" --gauge "\nWriting '$INFILE' \n\nto '$(media_name $OUTDEV)' ..." $WT_HEIGHT $WT_WIDTH 0 1577 | ES=$? 1578 | fi 1579 | ;; 1580 | 1581 | zip) 1582 | RESTORE_BYTES=$(file_image_size "$INFILE") 1583 | if [[ $INTERFACE = cli ]]; then 1584 | run_command "unzip -p '$INFILE' | pv -p -s $RESTORE_BYTES | sudo dd of=$OUTDEV bs=4M conv=fsync" $LINENO 1585 | ES=$? 1586 | else 1587 | ui_echo "~ Running: unzip -p '$INFILE' | pv -s $RESTORE_BYTES | sudo dd of=$OUTDEV bs=4M conv=fsync" blue 1588 | (unzip -p "$INFILE" | pv -n -s $RESTORE_BYTES | sudo dd of=$OUTDEV bs=4M) 2>&1 | \ 1589 | whiptail --backtitle "$BACKTITLE" --title "RESTORE" --gauge "\nWriting '$INFILE' \n\nto '$(media_name $OUTDEV)' ..." $WT_HEIGHT $WT_WIDTH 0 1590 | ES=$? 1591 | fi 1592 | ;; 1593 | 1594 | xz) 1595 | if [[ $INTERFACE = cli ]]; then 1596 | run_command "pv '$INFILE' -p | xz -d -c | sudo dd of=$OUTDEV bs=4M conv=fsync" $LINENO 1597 | ES=$? 1598 | else 1599 | ui_echo "~ Running: pv -n '$INFILE' | xz -d -c | sudo dd of=$OUTDEV bs=4M conv=fsync" blue 1600 | (pv -n "$INFILE" | xz -d -c | sudo dd of=$OUTDEV bs=4M) 2>&1 | \ 1601 | whiptail --backtitle "$BACKTITLE" --title "RESTORE" --gauge "\nWriting '$INFILE' \n\nto '$(media_name $OUTDEV)' ..." $WT_HEIGHT $WT_WIDTH 0 1602 | ES=$? 1603 | fi 1604 | ;; 1605 | 1606 | gz) 1607 | if [[ $INTERFACE = cli ]]; then 1608 | run_command "pv '$INFILE' -p | pigz -d -k -c | sudo dd of=$OUTDEV bs=4M conv=fsync" $LINENO 1609 | ES=$? 1610 | else 1611 | ui_echo "~ Running: pv -n '$INFILE' | pigz -d -k -c | sudo dd of=$OUTDEV bs=4M conv=fsync" blue 1612 | (pv -n "$INFILE" | pigz -d -k -c | sudo dd of=$OUTDEV bs=4M conv=fsync) 2>&1 | \ 1613 | whiptail --backtitle "$BACKTITLE" --title "RESTORE" --gauge "\nWriting '$INFILE' \n\nto '$(media_name $OUTDEV)' ..." $WT_HEIGHT $WT_WIDTH 0 1614 | ES=$? 1615 | fi 1616 | ;; 1617 | 1618 | zst) 1619 | if [[ $INTERFACE = cli ]]; then 1620 | run_command "pv '$INFILE' -p | zstd -d -c | sudo dd of=$OUTDEV bs=4M conv=fsync" $LINENO 1621 | # run_command "zstd -c -d $INFILE | sudo dd of=$OUTDEV bs=4M conv=fsync" $LINENO 1622 | ES=$? 1623 | else 1624 | ui_echo "~ Running: pv -n '$INFILE' | zstd -d -c | sudo dd of=$OUTDEV bs=4M conv=fsync" blue 1625 | (pv -n "$INFILE" | zstd -d -c | sudo dd of=$OUTDEV bs=4M) 2>&1 | \ 1626 | whiptail --backtitle "$BACKTITLE" --title "RESTORE" --gauge "\nWriting '$INFILE' \n\nto '$(media_name $OUTDEV)' ..." $WT_HEIGHT $WT_WIDTH 0 1627 | ES=$? 1628 | fi 1629 | ;; 1630 | 1631 | *) 1632 | echo 1633 | echo_red ERROR. Unsupported file extension. 1634 | return 1 1635 | ;; 1636 | esac 1637 | TIME2=$(date +%s) 1638 | echo 1639 | 1640 | if (( $ES )); then 1641 | echo 1642 | ui_msg_error $LINENO "Restore failed. ES=$ES" 1643 | return $ES 1644 | else 1645 | do_beep_up 1646 | ui_msg RESTORE "~ Restore complete. \n'$INFILE' written to \n'$(media_name $OUTDEV)' \nin $(get_elapsed_time $TIME1 $TIME2)." white "" log 1647 | fi 1648 | } 1649 | 1650 | 1651 | media_restore_checklist(){ 1652 | #GET INFILE if missing ################################## 1653 | if [[ -z $INFILE ]]; then 1654 | if whiptail_fselect "RESTORE : Select an Image-file" "$DEFAULT_PATH" "zip" "no" ; then 1655 | INFILE=$FILE_SELECTED 1656 | else 1657 | return 1 1658 | fi 1659 | fi 1660 | 1661 | # quit if infile is blank 1662 | if [[ -z $INFILE ]] ; then 1663 | ui_msg_error $LINENO "File '$INFILE' cannot be blank" 1664 | return 1 1665 | fi 1666 | 1667 | # quit if infile not found 1668 | if [[ ! -f $INFILE ]] ; then 1669 | ui_msg_error $LINENO "File '$INFILE' not found" 1670 | return 1 1671 | fi 1672 | 1673 | # quit if unsupported extension 1674 | FILE_EXT=$(file_ext "$INFILE") 1675 | case $FILE_EXT in 1676 | img | zip | iso | "" ) ;; 1677 | xz) pisafe_install_tool xz ;; 1678 | gz) pisafe_install_tool pigz ;; 1679 | zst) pisafe_install_tool zstd ;; 1680 | *) 1681 | ui_msg_error $LINENO "Unsupported file extension '$FILE_EXT'" 1682 | return 15 1683 | ;; 1684 | esac 1685 | 1686 | #Warning if image file size is 0 1687 | if [[ $(file_image_size "$INFILE") = 0 ]]; then 1688 | ui_msg_warning $LINENO "Image file size cannot be read" 1689 | fi 1690 | 1691 | 1692 | # GET OUTDEV if missing ############################################# 1693 | if [[ -z $OUTDEV ]]; then 1694 | OUTDEV=$(menu_select_device "RESTORE" y ) # this sets global variable OUTDEV 1695 | if (( $? )); then 1696 | ui_msg_error $LINENO "No media found." 1697 | return 1 1698 | fi 1699 | fi 1700 | 1701 | # Quit if outdev is blank 1702 | if [[ -z $OUTDEV ]]; then 1703 | ui_msg_error $LINENO "Device '$OUTDEV' cannot be blank" 1704 | return 1 1705 | fi 1706 | 1707 | # Add '/dev/' if missing 1708 | if [[ ${OUTDEV:0:5} != "/dev/" ]]; then 1709 | ui_echo "Device prefix not specified. Adding '/dev/'" grey nolog 1710 | OUTDEV="/dev/"$OUTDEV 1711 | echo_if_cli "OUT-DEV='$OUTDEV'" 1712 | fi 1713 | 1714 | # Quit if OUTDEV is not found 1715 | if [[ ! -e $OUTDEV ]] ; then 1716 | ui_msg_error $LINENO "Device '$OUTDEV' not found" 1717 | return 1 1718 | fi 1719 | 1720 | # Stop if outdev is the root device 1721 | if [[ $OUTDEV = $(env_root_device) ]]; then 1722 | ui_msg_error $LINENO "Restore to root device not allowed" 1723 | return 1 1724 | fi 1725 | 1726 | # Stop if INFILE is on OUTDEV 1727 | if [[ $(file_device "$INFILE") = $OUTDEV ]]; then 1728 | ui_msg_error $LINENO "Restore to same device as '$INFILE' not allowed" 1729 | return 1 1730 | fi 1731 | 1732 | # Warning if device is large 1733 | if [[ $(media_size $OUTDEV) -gt $(get_bytes $LARGE_DEVICE_WRITE_WARNING -b) ]]; then 1734 | ui_msg_warning $LINENO "'$(media_name $OUTDEV)' is larger than $(get_bytes $LARGE_DEVICE_WRITE_WARNING -h)... 1735 | Be sure this is the right device" 1736 | fi 1737 | 1738 | # Stop if imagefile is larger than device 1739 | if [[ $(file_image_size "$INFILE") -gt $(media_size $OUTDEV) ]]; then 1740 | ui_msg_error $LINENO "The image in '$INFILE' is ' $(file_image_size "$INFILE" -h)' 1741 | and '$OUTDEV' is only '$(media_size $OUTDEV -h)'" 1742 | return 1 1743 | fi 1744 | 1745 | 1746 | ui_echo "$PRODUCTNAME $SCRIPTVER Restore '$INFILE' to '$OUTDEV'" white 1747 | echo "IN-FILE='$INFILE'" 1748 | echo "OUT-DEV='$OUTDEV'" 1749 | 1750 | if [[ $SILENT != -y ]]; then 1751 | do_beep 1752 | RESULT=$(ui_yesno RESTORE "WARNING... All existing data on '$(media_name $OUTDEV)' - will be erased! Continue y/n?" "--defaultno") 1753 | echo 1754 | if [[ $RESULT != y ]]; then 1755 | ui_msg RESTORE "~ You did not answer 'y'" "" "" log 1756 | return 1 1757 | fi 1758 | fi 1759 | } 1760 | 1761 | 1762 | media_list() { 1763 | HIDE_ROOT_DEVICE_OVERRIDE=$1 # y n or blank 1764 | local DEVICES= 1765 | 1766 | case $HIDE_ROOT_DEVICE_OVERRIDE in 1767 | y) ROOT_FILTER=$(env_root_device) ;; 1768 | 1769 | n) ROOT_FILTER="aspodiausdfpoiasdf" ;; 1770 | 1771 | *) if [[ $HIDE_ROOT_DEVICE = on ]]; then 1772 | ROOT_FILTER=$(env_root_device) 1773 | else 1774 | ROOT_FILTER="aspodiausdfpoiasdf" 1775 | fi 1776 | ;; 1777 | esac 1778 | 1779 | # the 254 is for a QTM virtual machine on Mac 1780 | DEVICES=$(lsblk -I 8,179,254 -dnpo NAME,VENDOR,MODEL,SIZE | grep -v $ROOT_FILTER) 1781 | ES=$? 1782 | if (( $ES )); then 1783 | return $ES 1784 | fi 1785 | 1786 | echo "${DEVICES[@]}" 1787 | } 1788 | 1789 | 1790 | media_mount(){ 1791 | MEDIA=$1 1792 | PARTED_OUTPUT=$(sudo parted -ms $MEDIA print) # no freespace 1793 | MEDIA_PARTITIONS=$(echo "$PARTED_OUTPUT" | grep ^[1-9]: | grep -v ":::;" | cut -d: -f1) 1794 | 1795 | for PARTITION in $MEDIA_PARTITIONS; do 1796 | udisksctl mount -b $MEDIA$PARTITION 1797 | if (( $? )); then 1798 | echo error mounting $MEDIA$PARTITION 1799 | fi 1800 | done 1801 | } 1802 | 1803 | 1804 | media_power_off(){ 1805 | MEDIA=$1 1806 | umount $MEDIA? 1807 | udisksctl power-off $MEDIA 1808 | } 1809 | 1810 | 1811 | # ---------- UI functions 1812 | ui_countdown(){ 1813 | local SECONDS=${1:-10} 1814 | local TITLE="${2:-Countdown}" 1815 | #local MESSAGE="${3:-Counting down...}" 1816 | local MESSAGE=${3:-"Pausing for $SECONDS seconds... Press y to continue immediately or any other key to stop"} 1817 | 1818 | if [[ $INTERFACE = cli ]]; then 1819 | do_countdown $SECONDS "$MESSAGE" 1820 | else 1821 | whiptail_countdown "$SECONDS" "$TITLE" "$MESSAGE" 1822 | fi 1823 | } 1824 | 1825 | 1826 | ui_echo(){ 1827 | # used to notify on the terminal what is going on. optionally log it. 1828 | # ui_echo "msg" red log 1829 | MSG="$1" 1830 | COLOR="${2:-grey}" 1831 | local LOGIT="${3:-log}" # or nolog 1832 | 1833 | if [[ $LOGIT = log ]]; then 1834 | ui_log "$MSG" 1835 | fi 1836 | 1837 | case $COLOR in 1838 | red) echo_red "$MSG" ;; 1839 | white) echo_white "$MSG" ;; 1840 | blue) echo_blue "$MSG" ;; 1841 | *) echo -e "$MSG" ;; 1842 | # *) echo "$MSG" ;; 1843 | esac 1844 | } 1845 | 1846 | 1847 | ui_log(){ 1848 | # writes msg to $LOG_FILE 1849 | # usage ui_log "message" 1850 | INFO=$* 1851 | 1852 | INFO=$(echo $INFO | sed 's/\\n/ -/g') #change newline for ~ 1853 | 1854 | if [[ $LOG = on ]]; then 1855 | touch $LOG_FILE 1856 | echo "$(date "+%Y-%m-%d %H:%M") $INFO" >> $LOG_FILE 1857 | fi 1858 | } 1859 | 1860 | 1861 | ui_msg() { 1862 | #general message function. Supports CLI whiptail and logging. 1863 | # usage ui_msg INSTALL "$MSG" white OK log 1864 | TITLE="$1" 1865 | MSG="${2:-no message}" 1866 | COLOR="${3:-grey}" 1867 | # OK_BUTTON="${4:-Back}" 1868 | OK_BUTTON="${4:-OK}" 1869 | local LOGIT="${5:-no}" # or log 1870 | 1871 | if [[ $LOGIT = log ]]; then 1872 | ui_log "$MSG" 1873 | fi 1874 | 1875 | if [[ $INTERFACE = cli ]]; then 1876 | ui_echo "$PRODUCTNAME $TITLE: $MSG" $COLOR nolog 1877 | else 1878 | whiptail --backtitle "$BACKTITLE" --title "$TITLE" --scrolltext --ok-button "$OK_BUTTON" --msgbox "$MSG" $WT_HEIGHT_TALL $WT_WIDTH_WIDE 1879 | fi 1880 | } 1881 | 1882 | 1883 | ui_msg_error() { 1884 | # usage: ui_msg_error $LINENO "message" 1885 | LINE=$1 1886 | shift 1887 | do_beep_down 1888 | MSG="$@" 1889 | ui_msg "ERROR" "~ Error at line $LINE. $MSG" red "" log 1890 | } 1891 | 1892 | 1893 | ui_msg_warning(){ 1894 | # usage: ui_msg_warning $LINENO "message" 1895 | LINE=$1 1896 | shift 1897 | do_beep 1898 | do_beep 1899 | MSG="$@" 1900 | ui_msg "WARNING" "~ $MSG" red "" nolog 1901 | } 1902 | 1903 | 1904 | ui_yesno(){ 1905 | # general yes/no function. Supports CLI and whiptail. 1906 | # note, this output is often assigned to a variable, so don't add any additional echo statements. 1907 | TITLE="$1" 1908 | MSG="$2" 1909 | DEFAULT="${3:-""}" # defaults to Yes. can specify --defaultno 1910 | TIMEOUT=${4:-0} 1911 | # COLOR="${4:-white}" color does not work with the 'read' command 1912 | 1913 | if [[ $INTERFACE = cli ]]; then 1914 | MSG="$PRODUCTNAME: $TITLE: $MSG [y/n]?" 1915 | # change 'newline' chars to ' -' 1916 | MSG=$(echo $MSG | sed 's/\\n/ -/g') 1917 | while true; do 1918 | read -s -n1 -p "$MSG" yn 1919 | case $yn in 1920 | [Yy]* ) echo y; return 0;; 1921 | [Nn]* ) echo n; return 0;; 1922 | * ) ;; 1923 | esac 1924 | done 1925 | else 1926 | whiptail --backtitle "$BACKTITLE" --title "$TITLE" $DEFAULT --yesno "$MSG" $WT_MB_HEIGHT $WT_MB_WIDTH 3>&1 1>&2 2>&3 1927 | ES=$? 1928 | if (( $ES )); then 1929 | echo n 1930 | else 1931 | echo y 1932 | fi 1933 | return $ES 1934 | fi 1935 | } 1936 | 1937 | 1938 | 1939 | ######## functions for advanced features 1940 | 1941 | media_format(){ 1942 | local DEVICE=$1 1943 | local FORMAT=${2:-"fat32"} # fat32, ntfs, ext4, exfat 1944 | local SILENT=${3:-"-n"} 1945 | 1946 | local ROOT="$(env_root_device)" 1947 | local TYPE="$(lsblk $DEVICE -dnpo TYPE)" 1948 | local NAME="$(lsblk $DEVICE -dnpo VENDOR,MODEL,SIZE,TYPE) ($DEVICE)" 1949 | local PARTITION=$DEVICE 1950 | 1951 | if [[ $FORMAT = fat32 ]] ; then 1952 | PART_TABLE=msdos 1953 | PART_TYPE=primary 1954 | PART_NAME= 1955 | else 1956 | PART_TABLE=gpt 1957 | PART_TYPE= 1958 | PART_NAME=$FORMAT 1959 | fi 1960 | 1961 | if [[ $FORMAT = exfat ]]; then 1962 | FS_TYPE=ntfs 1963 | else 1964 | FS_TYPE=$FORMAT 1965 | fi 1966 | 1967 | 1968 | if [[ -z $1 ]] ; then 1969 | ui_msg_error $LINENO "Device not specified." 1970 | lsblk -dp 1971 | return 1 1972 | fi 1973 | 1974 | if [[ ! -e $DEVICE ]]; then 1975 | ui_msg_error $LINENO "'$DEVICE' does not exist" 1976 | return 2 1977 | fi 1978 | 1979 | if [[ $1 = $ROOT ]] ; then 1980 | ui_msg_error $LINENO "Can not format root device" 1981 | return 3 1982 | fi 1983 | 1984 | # Stop if dev is the root device 1985 | if [[ $1 = $(env_root_device) ]]; then 1986 | ui_msg_error $LINENO "Format root device not allowed" 1987 | return 3 1988 | fi 1989 | 1990 | ui_echo "$PRODUCTNAME $SCRIPTVER Format '$(media_name $DEVICE)' to '$FORMAT'..." white 1991 | 1992 | # echo $SILENT 1993 | 1994 | if [[ $SILENT != -y ]]; then 1995 | RESULT=$(ui_yesno "ERASE MEDIA" "WARNING!! \n\nFormat '$DEVICE' to '$FORMAT' \n\nAll existing data on '$NAME' will be erased. \n\nAre you sure you want to continue? y/n?" "--defaultno") 1996 | echo 1997 | if [[ $RESULT != y ]]; then 1998 | ui_msg "ERASE MEDIA" "~ $DEVICE not erased. you answered '$RESULT'" "" "" log 1999 | return 1 2000 | fi 2001 | fi 2002 | 2003 | ui_countdown 10 "ERASE MEDIA" # "Erasing Media '$(media_name $DEVICE)' in 10 seconds...\n\n" 2004 | if (( $? )); then 2005 | # ui_msg "INSTALL:" "$MSG" "white" "OK" "log" 2006 | ui_msg "ERASE MEDIA" "~ Erase Media stopped." "" "" log 2007 | return 2 2008 | fi 2009 | 2010 | echo "Unmounting '$DEVICE'..." 2011 | sudo umount $DEVICE 2> /dev/null 2012 | sudo umount $DEVICE? 2> /dev/null 2013 | 2014 | 2015 | # if selected whole disk... wipe the partition table and make a new partition table. 2016 | if [[ $TYPE = disk ]]; then 2017 | 2018 | echo "Writing zeros to '$DEVICE'..." 2019 | run_command "sudo dd if=/dev/zero of=$DEVICE bs=512 count=4" $LINENO 2020 | 2021 | echo "Writing new '$PART_TABLE' partition table to '$DEVICE'..." 2022 | run_command "sudo parted -s $DEVICE mklabel $PART_TABLE" $LINENO 2023 | 2024 | echo "Creating empty partition on '$DEVICE'..." 2025 | run_command "sudo parted -s -a opt $DEVICE mkpart $PART_TYPE $PART_NAME $FS_TYPE 2M 100%" $LINENO 2026 | 2027 | PARTITION=$DEVICE\1 2028 | fi 2029 | 2030 | sleep 2 2031 | echo "Formatting the partition to '$FORMAT'..." 2032 | 2033 | case $FORMAT in 2034 | fat16) 2035 | if [[ -z $(which mkfs.vfat) ]] ; then 2036 | echo Installing vfat support ... 2037 | sudo $INSTALL dosfstools 2038 | fi 2039 | run_command "sudo mkfs.vfat -F16 -v -I -n FAT16 $PARTITION" $LINENO 2040 | ES=$? 2041 | ;; 2042 | 2043 | fat32) 2044 | if [[ -z $(which mkfs.vfat) ]] ; then 2045 | echo Installing vfat support ... 2046 | sudo $INSTALL dosfstools 2047 | fi 2048 | run_command "sudo mkfs.vfat -v -I -n FAT32 $PARTITION" $LINENO 2049 | ES=$? 2050 | ;; 2051 | 2052 | exfat) 2053 | #https://unix.stackexchange.com/questions/61209/create-and-format-exfat-partition-from-linux 2054 | if [[ -z $(which mkfs.exfat) ]] ; then 2055 | echo Installing exfat support ... 2056 | sudo $INSTALL exfat-utils 2057 | sudo $INSTALL exfat-fuse 2058 | #sudo modprobe fuse 2059 | #echo "fuse" | sudo tee -a /etc/modules 2060 | fi 2061 | run_command "sudo mkfs.exfat -n EXFAT $PARTITION" $LINENO 2062 | ES=$? 2063 | ;; 2064 | 2065 | ntfs) 2066 | if [[ -z $(which mkfs.ntfs) ]] ; then 2067 | #sudo apt-get install fuse ntfs-3g 2068 | echo Installing ntfs support... 2069 | sudo $INSTALL fuse 2070 | sudo $INSTALL ntfs-3g 2071 | sudo $INSTALL ntfsprogs #ntfsprogs is for arch 2072 | #sudo modprobe fuse 2073 | #echo "fuse" | sudo tee -a /etc/modules 2074 | fi 2075 | run_command "sudo mkfs.ntfs -f -L NTFS $PARTITION" $LINENO 2076 | ES=$? 2077 | ;; 2078 | 2079 | ext2 | ext3 | ext4) 2080 | run_command "sudo mkfs -t $FORMAT -L $FORMAT -E root_owner=$UID:$GID -F $PARTITION" $LINENO 2081 | ES=$? 2082 | ;; 2083 | 2084 | *) 2085 | ui_msg_error $LINENO "Unknown format '$FORMAT'" 2086 | 2087 | esac 2088 | 2089 | if (( $ES)); then 2090 | # ui_msg_error $LINENO "mkfs failed" 2091 | return 1 2092 | else 2093 | ui_msg "ERASE MEDIA" "~ Format complete. '$(media_name $DEVICE)' has been erased." white "" log 2094 | fi 2095 | 2096 | # mount the drive with udisksctl if installed 2097 | if [[ $(env_which udisksctl) ]]; then 2098 | echo "Mounting the drive..." 2099 | sleep 2 2100 | run_command "udisksctl mount -b $PARTITION" $LINENO 2101 | if (( $? )); then 2102 | ui_msg_warning $LINENO "mount failed" 2103 | #return 1 2104 | fi 2105 | fi 2106 | } 2107 | 2108 | 2109 | media_details(){ 2110 | local MEDIA=$1 2111 | local MSG= 2112 | MSG="\n===== Media details =====" 2113 | MSG="$MSG\nMedia: '$(media_name $MEDIA)' \n" 2114 | 2115 | echo "Searching for Operating Systems..." 2116 | MSG=$MSG"$(media_os $MEDIA)" 2117 | 2118 | echo "Calculating backup estimates..." 2119 | echo 2120 | MSG="$MSG\n$(media_backup_estimate $MEDIA)" 2121 | MSG="$MSG\n\n===== PARTITION TABLE DETAILS ===== \n$(sudo parted -s $MEDIA print free) \n\n" 2122 | MSG="$MSG$(lsblk $MEDIA -o maj:min,fssize,fsused,fsavail,fsuse%)\n\n" 2123 | 2124 | ui_msg "MEDIA DETAILS" "$MSG" 2125 | } 2126 | 2127 | 2128 | media_partition_info() { 2129 | local MEDIA=$1 2130 | local PARTITION=$2 2131 | 2132 | local PARTED_OUTPUT= 2133 | local PARTSTART= 2134 | local LOOPBACK= 2135 | local MOUNTDIR= 2136 | 2137 | local OS= 2138 | local ARCH= 2139 | local BITS= 2140 | local READONLY= 2141 | local OVERLAY= 2142 | 2143 | PARTED_OUTPUT=$(sudo parted -ms "$MEDIA" unit B print) 2144 | if (( $? )); then 2145 | return 1 2146 | fi 2147 | 2148 | # mount the filesystem 2149 | PARTSTART="$(echo "$PARTED_OUTPUT" | grep ^$PARTITION: | cut -d ':' -f 2 | tr -d 'B')" 2150 | LOOPBACK="$(sudo losetup -f --show -o "$PARTSTART" "$MEDIA")" 2151 | if (( $? )); then 2152 | echo ...retrying... 2153 | sleep 2 2154 | LOOPBACK="$(sudo losetup -f --show -o "$PARTSTART" "$MEDIA")" 2155 | if (( $? )); then 2156 | return 3 2157 | fi 2158 | fi 2159 | MOUNTDIR=$(mktemp -d) 2160 | sudo mount "$LOOPBACK" "$MOUNTDIR" 2161 | if (( $? )); then 2162 | sudo losetup -d "$LOOPBACK" 2163 | return 4 2164 | fi 2165 | 2166 | # look for OS name and architecture 2167 | if [[ -e $MOUNTDIR/etc/os-release ]]; then 2168 | OS="$(cat $MOUNTDIR/etc/os-release | grep PRETTY_NAME | cut -d= -f2)" 2169 | ARCH="$(file $MOUNTDIR/bin/bash | cut -d, -f2)" 2170 | BITS="$(file $MOUNTDIR/bin/bash | cut -d, -f1)" 2171 | if [[ ! -z $(echo "$BITS" | grep 32 ) ]]; then 2172 | BITS=32 2173 | elif [[ ! -z $(echo "$BITS" | grep 64 ) ]]; then 2174 | BITS=64 2175 | fi 2176 | if [[ ! $ARCH =~ $BITS ]]; then 2177 | ARCH=$ARCH"-"$BITS 2178 | fi 2179 | OS="$OS $ARCH" 2180 | fi 2181 | 2182 | # look for windows 2183 | if [[ -e $MOUNTDIR/Windows ]]; then 2184 | OS="'Windows'" 2185 | fi 2186 | 2187 | # look for read-only partition (may not be needed) 2188 | if [[ -e $MOUNTDIR/etc/fstab ]]; then 2189 | ROOTREADONLY="$(cat $MOUNTDIR/etc/fstab | grep /boot | grep ,ro )" 2190 | if [[ ! -z $ROOTREADONLY ]]; then 2191 | ROOTREADONLY="(READONLY)" 2192 | fi 2193 | fi 2194 | 2195 | # look for overlay file system 2196 | if [[ -e $MOUNTDIR/cmdline.txt ]]; then 2197 | OVERLAY="$(cat $MOUNTDIR/cmdline.txt | grep boot=overlay )" 2198 | if [[ ! -z $OVERLAY ]]; then 2199 | OVERLAY="(OVERLAY file system)" 2200 | fi 2201 | fi 2202 | 2203 | # unmount fs and cleanup 2204 | sudo umount "$MOUNTDIR" 2205 | sudo losetup -d "$LOOPBACK" 2206 | 2207 | echo "$OS$OVERLAY$ROOTREADONLY" 2208 | } 2209 | 2210 | 2211 | media_os() { 2212 | local MEDIA=$1 2213 | local MSG= 2214 | 2215 | PARTED_OUTPUT=$(sudo parted -ms $MEDIA print) # no freespace 2216 | MEDIA_PARTITIONS=$(echo "$PARTED_OUTPUT" | grep ^[1-9]: | grep -v ":::;" | cut -d: -f1) 2217 | 2218 | # echo Looking for operating systems on partitions... 2219 | for PARTITION in $MEDIA_PARTITIONS; do 2220 | OS=$(media_partition_info $MEDIA $PARTITION) 2221 | ES=$? 2222 | if [[ $ES = 0 ]] && [[ ! -z $OS ]] ; then 2223 | echo " - '$OS' found on Partition $PARTITION" 2224 | fi 2225 | done 2226 | } 2227 | 2228 | 2229 | ######################################## 2230 | ### WHIPTAIL GUI FUNCTIONS ############## 2231 | ######################################## 2232 | 2233 | whiptail_calc_wt_size() { 2234 | # NOTE: it's tempting to redirect stderr to /dev/null, so supress error 2235 | # output from tput. However in this case, tput detects neither stdout or 2236 | # stderr is a tty and so only gives default 80, 24 values 2237 | 2238 | WT_HEIGHT=20 2239 | WT_WIDTH=80 2240 | WT_HEIGHT_TALL=$(($(tput lines)-10)) 2241 | WT_WIDTH_WIDE=$(($(tput cols)-7)) 2242 | 2243 | if [[ $WT_WIDTH_WIDE -gt 120 ]]; then 2244 | WT_WIDTH_WIDE=120 2245 | fi 2246 | 2247 | if [[ -z $WT_WIDTH ]] || [[ $WT_WIDTH -lt 60 ]]; then 2248 | WT_WIDTH=80 2249 | fi 2250 | if [[ $WT_WIDTH -gt 178 ]]; then 2251 | WT_WIDTH=80 2252 | fi 2253 | 2254 | WT_MENU_HEIGHT=$(($WT_HEIGHT-7)) 2255 | WT_MENU_HEIGHT_TALL=$(($WT_HEIGHT_TALL-7)) 2256 | 2257 | WT_MB_HEIGHT=$WT_HEIGHT 2258 | WT_MB_WIDTH=$WT_WIDTH 2259 | } 2260 | 2261 | 2262 | whiptail_fselect() { 2263 | # 2264 | # Arguments 2265 | # 1 Dialog title 2266 | # 2 Source path to list files and directories 2267 | # 3 File mask (by default *) 2268 | # 4 "yes" to allow go back in the file system. 2269 | # 2270 | # Returns 2271 | # 0 if a file was selected and loads the FILE_SELECTED variable 2272 | # with the selected file. 2273 | # 1 if the user cancels. 2274 | # 2275 | #if whiptail_fselect "Please, select a file" /home/user ; then 2276 | # echo "File Selected: \"$FILE_SELECTED\"." 2277 | #else 2278 | # echo "Cancelled!" 2279 | #fi 2280 | # ---------------------------------------------------------------------- 2281 | 2282 | local TITLE=${1:-$MSG_INFO_TITLE} 2283 | local LOCAL_PATH=${2:-$(pwd)} 2284 | local FILE_MASK=${3:-"*"} 2285 | #local FILE_MASK='( -name "*.img" -o -name "*.zip" -o -name "*.xz" -o -name "*.gz" )' 2286 | local ALLOW_BACK=${4:-no} 2287 | local FILES=() 2288 | 2289 | [[ $ALLOW_BACK != no ]] && FILES+=(".." "..") 2290 | 2291 | IFS=$'\t\n' 2292 | 2293 | FILES=("FILENAME" " SIZE (IMAGESIZE)") 2294 | 2295 | # First add folders 2296 | for DIR in $(find $LOCAL_PATH -maxdepth 1 -mindepth 1 -name "[!.]*" -type d -printf "%f\t" | sort 2> /dev/null) 2297 | do 2298 | FILES+=($DIR " folder") 2299 | done 2300 | 2301 | # Then add files 2302 | for FILE in $(find $LOCAL_PATH -maxdepth 1 \ 2303 | \( -name "*.img" -o -name "*.zip" -o -name "*.xz" -o -name "*.gz" -o -name "*.iso" -o -name "*.zst" \) \ 2304 | -type f \ 2305 | -printf "%f\t %s\n" | sort 2> /dev/null) 2306 | do 2307 | FILES+=($FILE) 2308 | done 2309 | unset IFS 2310 | 2311 | # convert filesizes to human readable and add image size 2312 | num='^[0-9]+$' 2313 | arraylength=${#FILES[@]} 2314 | 2315 | if [[ $arraylength = 0 ]]; then 2316 | FILES=(no files) 2317 | fi 2318 | 2319 | for (( i=0; i<${arraylength}; i++ )); do 2320 | arrayelement=$(echo "${FILES[$i]}" | sed 's/ *//g') 2321 | if [[ $arrayelement =~ $num ]] ; then 2322 | # FILES[$i]=$(echo " " $(get_bytes $arrayelement -h)) 2323 | FILES[$i]=$(echo " " $(get_bytes $arrayelement -h) \($(file_image_size "$LOCAL_PATH/$PREVIOUS_ELEMENT" -h)\)) 2324 | else 2325 | PREVIOUS_ELEMENT=$(echo "${FILES[$i]}") 2326 | fi 2327 | done 2328 | 2329 | # let user select file 2330 | while true 2331 | do 2332 | FILE_SELECTED=$(whiptail --clear --backtitle "$BACKTITLE" --title "$LOCAL_PATH" \ 2333 | --menu "Choose a file" $WT_HEIGHT_TALL $WT_WIDTH_WIDE $WT_MENU_HEIGHT_TALL "${FILES[@]}" 3>&1 1>&2 2>&3) 2334 | 2335 | #exit if bad whiptail statement 2336 | FSX=${FILE_SELECTED:0:3} 2337 | # if [ ! -z $FSX ] && [ $FSX = "Box" ]; then 2338 | if [[ ~$FSX = ~Box ]]; then 2339 | echo "ERROR in whiptail in whiptail_fselect. Maybe a space in a filename?" 2340 | echo "${FILES[@]}" 2341 | exit 1 2342 | fi 2343 | 2344 | if [[ -z $FILE_SELECTED ]]; then 2345 | return 1 2346 | else 2347 | if [[ $FILE_SELECTED = ".." ]] && [[ $ALLOW_BACK != "no" ]]; then 2348 | return 0 2349 | 2350 | elif [[ -d $LOCAL_PATH/$FILE_SELECTED ]] ; then 2351 | if whiptail_fselect "$TITLE" "$LOCAL_PATH/$FILE_SELECTED" "$FILE_MASK" "yes" ; then 2352 | if [[ $FILE_SELECTED != ".." ]]; then 2353 | return 0 2354 | fi 2355 | else 2356 | return 1 2357 | fi 2358 | 2359 | elif [[ -f $LOCAL_PATH/$FILE_SELECTED ]] ; then 2360 | FILE_SELECTED="$LOCAL_PATH/$FILE_SELECTED" 2361 | return 0 2362 | fi 2363 | fi 2364 | done 2365 | } 2366 | 2367 | 2368 | whiptail_countdown(){ 2369 | local MAX=${1:-10} 2370 | local TITLE=${2:-"COUNTDOWN... press any key to stop or 'y' to continue now"} 2371 | local MSG="${3:-Countdown for $MAX seconds.\n\n}" 2372 | 2373 | for ((i=$MAX; i>=0; i--)) ; do 2374 | TERM=vt220 whiptail --backtitle "$BACKTITLE" --title "$TITLE" --infobox "$MSG" $WT_MB_HEIGHT $WT_MB_WIDTH 2375 | read -s -t 1 -N 1 INPUT 2376 | if [[ ! -z $INPUT ]]; then 2377 | if [[ $INPUT = y ]]; then 2378 | echo Continuing... 2379 | return 0 2380 | else 2381 | echo $INPUT 2382 | return 2 2383 | fi 2384 | fi 2385 | MSG=$(echo "$MSG.$i") 2386 | done 2387 | } 2388 | 2389 | 2390 | echo_if_cli (){ 2391 | local MSG=$1 2392 | local COLOR=$2 2393 | if [[ $INTERFACE = cli ]]; then 2394 | case $COLOR in 2395 | white) echo_white $MSG ;; 2396 | red) echo_red $MSG ;; 2397 | *) echo $MSG ;; 2398 | esac 2399 | fi 2400 | } 2401 | 2402 | 2403 | 2404 | ######## MENU FUNCTIONS ###################### 2405 | menu_cli(){ 2406 | INTERFACE="cli" 2407 | case $1 in 2408 | 2409 | backup) media_backup "$2" "$3" $4 ;; 2410 | 2411 | restore) media_restore "$2" "$3" $4 ;; 2412 | 2413 | list) do_list_info ;; 2414 | 2415 | install) pisafe_install $2 ;; 2416 | 2417 | update) pisafe_update cli yes $2 ;; 2418 | 2419 | uninstall) pisafe_uninstall $2 ;; 2420 | 2421 | log) nano $LOG_FILE ;; 2422 | 2423 | settings) nano $CONFIG ;; 2424 | 2425 | defaults) if [[ $(ui_yesno "Reset settings to factory defaults") = y ]]; then echo; config_var_set_defaults; fi ;; 2426 | 2427 | details) media_details $2 ;; 2428 | 2429 | erase|format) media_format $2 $3 $4 ;; 2430 | 2431 | beep) 2432 | #SOUND=on 2433 | echo beep ; do_beep ; sleep 2 2434 | echo beep_down ; do_beep_down ; sleep 2 2435 | echo beep_up ; do_beep_up;; 2436 | 2437 | sysinfo) env_sysinfo ;; 2438 | 2439 | 2440 | about) pisafe_about ;; 2441 | 2442 | help|-h|--h) pisafe_help ;; 2443 | 2444 | -v) echo $SCRIPTVER ;; 2445 | 2446 | "") # echo $(env_which whiptail) 2447 | if [[ $(env_which whiptail) ]]; then 2448 | menu_gui 2449 | else 2450 | pisafe_help 2451 | fi ;; 2452 | 2453 | *) pisafe_help ;; 2454 | esac 2455 | } 2456 | 2457 | 2458 | menu_get_outfile(){ 2459 | OUTFILEDEFAULT="$DEFAULT_PATH/$(date +%Y-%m-%d-pisafe)" 2460 | EXT=$DEFAULT_EXTENSION 2461 | OUTFILE= 2462 | 2463 | MEDIA_OS="$(media_os $INDEV)" 2464 | 2465 | # echo "Calculating backup estimates..." 2466 | ESTIMATES=$(media_backup_estimate $INDEV) 2467 | 2468 | while true; do 2469 | 2470 | #get filename 2471 | MSG="$MEDIA_OS \ 2472 | \n$ESTIMATES\n===== BACKUP FILENAME ===== \ 2473 | \nIf you don't include an extension '.img.$DEFAULT_EXTENSION' will be added \ 2474 | \n\nSupported extensions are: .img .zip .xz .gz .zst\ 2475 | \n.zip is the baseline \n.img is ~2x faster but twice the size (NOT compressed) \n.xz is ~25% smaller but takes 3x-4x longer \n.gz is ~25% faster \ 2476 | \n\nRecommended to have NO SPACES in the name \ 2477 | \n\nEnter the image filename (eg: 2020-12-15-buster32)" 2478 | NEWFILE=$(whiptail --backtitle "$BACKTITLE" --title "BACKUP '$(media_name $INDEV)'" --inputbox "$MSG" $WT_HEIGHT_TALL $WT_WIDTH_WIDE "$OUTFILEDEFAULT" 3>&1 1>&2 2>&3) 2479 | ES=$? 2480 | if (( $ES )); then 2481 | return $ES 2482 | fi 2483 | 2484 | 2485 | #fill out variables based on selected filename 2486 | OUTFILEDEFAULT="$NEWFILE" 2487 | OUTFILE="$NEWFILE" 2488 | if [[ ! -z $(file_ext "$OUTFILE") ]]; then 2489 | EXT=$(file_ext "$OUTFILE") 2490 | else 2491 | EXT=$DEFAULT_EXTENSION 2492 | fi 2493 | 2494 | media_backup_check_outfile 2495 | ES=$? 2496 | if ! (( $ES )); then 2497 | #return $ES 2498 | break # break out of the while loop 2499 | fi 2500 | done 2501 | 2502 | # add .img.zip if no extension 2503 | if [[ ! -z $OUTFILE ]] && [[ -z $(file_ext $OUTFILE) ]] ; then 2504 | echo "Adding '.img.$DEFAULT_EXTENSION'" 2505 | OUTFILE=$OUTFILE.img.$DEFAULT_EXTENSION 2506 | #echo "added ext '$OUTFILE'" 2507 | fi 2508 | 2509 | # check and fix outfile name..... 2510 | if [[ $(file_ext "$OUTFILE") = "img" ]] ; then 2511 | OUTFILE=$(file_base "$OUTFILE").$(file_ext "$OUTFILE") 2512 | else 2513 | OUTFILE=$(file_base "$OUTFILE").img.$(file_ext "$OUTFILE") 2514 | fi 2515 | } 2516 | 2517 | 2518 | menu_gui(){ 2519 | INTERFACE="gui" 2520 | config_var_get_settings 2521 | while true 2522 | do 2523 | whiptail_calc_wt_size 2524 | MENU_CHOICE=$(whiptail --clear --backtitle "$BACKTITLE" --title "MAIN MENU" --ok-button "Select" --cancel-button "Exit" --menu " " $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT \ 2525 | "BACKUP" " Backup media to an image-file" \ 2526 | "RESTORE" " Restore media from an image-file" \ 2527 | "LIST" " List media and image files" \ 2528 | " " " " \ 2529 | "SETTINGS" " Change settings" \ 2530 | "TOOLS" " Various tools"\ 2531 | " " " " \ 2532 | "HELP" " Help"\ 2533 | 3>&1 1>&2 2>&3) 2534 | 2535 | whiptail_calc_wt_size 2536 | case $MENU_CHOICE in 2537 | BACKUP) media_backup ;; 2538 | 2539 | RESTORE) media_restore ;; 2540 | 2541 | LIST) echo "Getting list of images ready..." 2542 | ui_msg LIST "$(do_list_info $DEFAULT_PATH)" ;; 2543 | 2544 | SETTINGS) menu_settings ;; 2545 | 2546 | TOOLS) menu_tools ;; 2547 | 2548 | HELP) ui_msg HELP "$(pisafe_help)" ;; 2549 | 2550 | ABOUT) ui_msg ABOUT "$(pisafe_about)" ;; 2551 | 2552 | " "*) ;; 2553 | 2554 | * ) return ;; 2555 | esac 2556 | done 2557 | } 2558 | 2559 | 2560 | menu_select_device(){ 2561 | # note, don't have any output from inside, just returns and the echo at the end. 2562 | TITLE=$1 2563 | HIDE_ROOT_DEVICE_OVERRIDE=$2 # y n or blank 2564 | FIELD_SEPERATOR="|" 2565 | 2566 | #get list of sd devices and add a field seperator | 2567 | IFS=$'\n' 2568 | options=($(media_list $2 | sed 's/ / '$FIELD_SEPERATOR' /')) 2569 | ES=$? 2570 | if (( $ES )); then 2571 | return $ES 2572 | fi 2573 | unset IFS 2574 | 2575 | arraylength=${#options[@]} 2576 | if [[ $arraylength = 0 ]]; then 2577 | return 1 2578 | fi 2579 | 2580 | IFS=$FIELD_SEPERATOR 2581 | DEVICE_SELECTED=$(whiptail --clear --backtitle "$BACKTITLE" --title "$TITLE"\ 2582 | --menu "Select media (Hide root = $HIDE_ROOT_DEVICE)" \ 2583 | $WT_MB_HEIGHT $WT_MB_WIDTH 10 ""${options[@]}"" 3>&1 1>&2 2>&3) 2584 | ES=$? 2585 | unset IFS 2586 | if (( $ES )); then 2587 | return $ES 2588 | fi 2589 | 2590 | # OUTDEV=$DEVICE_SELECTED 2591 | # INDEV=$DEVICE_SELECTED 2592 | echo $DEVICE_SELECTED 2593 | } 2594 | 2595 | 2596 | menu_set_compression(){ 2597 | CHOICE=$(whiptail --title "SETTINGS-COMPRESSION" --radiolist \ 2598 | "Choose compression level (your milage may vary)" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT \ 2599 | "1" "Fastest time" off \ 2600 | "2" "" off \ 2601 | "3" "" off \ 2602 | "4" "" off \ 2603 | "5" "(default)" on \ 2604 | "6" "" off \ 2605 | "7" "" off \ 2606 | "8" "" off \ 2607 | "9" "Smallest file (MUCH slower)" off 3>&1 1>&2 2>&3 ) 2608 | 2609 | if [[ ! -z $CHOICE ]]; then 2610 | COMPRESSION_LEVEL=$CHOICE 2611 | config_var_set compression_level $CHOICE 2612 | fi 2613 | } 2614 | 2615 | 2616 | menu_settings(){ 2617 | while true; do 2618 | SELECTION=$(whiptail --backtitle "$BACKTITLE" --title "SETTINGS" --cancel-button "Back" --menu " " $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT \ 2619 | "Default directory " "$DEFAULT_PATH" \ 2620 | "Default extension " "$DEFAULT_EXTENSION" \ 2621 | "Compression level " "$COMPRESSION_LEVEL of 9" \ 2622 | "Options " "Several options to turn on/off " \ 2623 | " " " " \ 2624 | "Settings file " "Edit the settings file manually (pisafe.conf)" \ 2625 | "Factory defaults " "Reset settings to factory defaults" \ 2626 | 3>&1 1>&2 2>&3) 2627 | 2628 | ES=$? 2629 | if (( $ES )); then 2630 | return $ES 2631 | else 2632 | case "$SELECTION" in 2633 | "Default d"*) menu_settings_default_path ;; 2634 | "Default e"*) menu_settings_default_extension ;; 2635 | Compression*) menu_set_compression ;; 2636 | Text*) ;; 2637 | Options*) menu_settings_options ;; 2638 | 2639 | " "*) ;; 2640 | Settings*) $TEXT_EDITOR $CONFIG 2>/dev/null 2641 | config_var_get_settings ;; 2642 | Factory*) 2643 | if [[ $(ui_yesno WARNING "Are you sure you want to reset the settings to factory defaults?" "--defaultno") = y ]]; then 2644 | echo 2645 | ui_log "Setting $PRODUCTNAME $SCRIPTVER to Factory Defaults" 2646 | config_var_set_defaults 2647 | config_var_get_settings 2648 | fi ;; 2649 | *) whiptail --msgbox "Programmer error: unrecognized option" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT ;; 2650 | esac 2651 | fi 2652 | done 2653 | } 2654 | 2655 | 2656 | menu_settings_default_extension(){ 2657 | CHOICE=$(whiptail --backtitle "$BACKTITLE" --title "SETTINGS-EXTENSION" --radiolist \ 2658 | "Choose default extension (compression format)" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT \ 2659 | "zip" "Default" on \ 2660 | "xz" "~25% smaller, 3x-4x more time" off \ 2661 | "gz" "~25% faster, same size as zip" off \ 2662 | "img" "Twice the size, half the time (No compression)" off \ 2663 | "zst" "Set compression level to 1-22 " off \ 2664 | 3>&1 1>&2 2>&3 ) 2665 | 2666 | if [[ ! -z $CHOICE ]]; then 2667 | DEFAULT_EXTENSION=$CHOICE 2668 | config_var_set default_extension $CHOICE 2669 | fi 2670 | } 2671 | 2672 | 2673 | menu_settings_default_path(){ 2674 | MSG="Enter new default path '$DEFAULT_PATH'" 2675 | TEMP_PATH="$DEFAULT_PATH" 2676 | while true; do 2677 | NEWPATH=$(whiptail --backtitle "$BACKTITLE" --title "SETTINGS" --inputbox "$MSG" $WT_HEIGHT $WT_WIDTH "$TEMP_PATH" 3>&1 1>&2 2>&3) 2678 | ES=$? 2679 | if (( $ES )); then 2680 | return $ES 2681 | fi 2682 | 2683 | if [[ ! -z $NEWPATH ]]; then 2684 | if [[ -d $NEWPATH ]]; then 2685 | DEFAULT_PATH=$(echo "$NEWPATH" | sed 's/[/\t]*$//') 2686 | config_var_set default_path $DEFAULT_PATH 2687 | return 0 2688 | else 2689 | ui_msg_error $LINENO "ERROR Directory '$NEWPATH' does not exist" 2690 | TEMP_PATH="$NEWPATH" 2691 | fi 2692 | fi 2693 | done 2694 | } 2695 | 2696 | 2697 | menu_settings_options(){ 2698 | #"hide_mmc_device" "Hide internal sd card (CAUTION)" "$HIDE_MMC_DEVICE" \ 2699 | #echo "starting menu_settings_options" 2700 | 2701 | ON=$(whiptail --title "SETTINGS-OPTIONS" --checklist "Enable options" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT \ 2702 | "shrink_fs" "Shrink filesystem on backup" "$SHRINK_FS" \ 2703 | "auto_expand_fs" "Create shrunk backup that auto-expands" "$AUTO_EXPAND_FS" \ 2704 | "hide_root_device" "Hide root device (CAUTION)" "$HIDE_ROOT_DEVICE" \ 2705 | "check_for_updates_on_startup" "" "$CHECK_FOR_UPDATES_ON_STARTUP" \ 2706 | "sound" "enable sounds" "$SOUND" \ 2707 | "log" "log activity" "$LOG" \ 2708 | "debug_mode" " " "$DEBUG_MODE" \ 2709 | 3>&1 1>&2 2>&3) 2710 | 2711 | if (( $? )); then 2712 | return 2713 | fi 2714 | 2715 | # dump the quotes in the string. 2716 | ON=$(echo $ON | sed 's/"//g') 2717 | 2718 | # set all to off 2719 | config_var_set shrink_fs off 2720 | config_var_set auto_expand_fs off 2721 | config_var_set hide_root_device off 2722 | config_var_set sound off 2723 | config_var_set log off 2724 | config_var_set debug_mode off 2725 | config_var_set check_for_updates_on_startup off 2726 | 2727 | for SELECTED in $ON; do 2728 | config_var_set $SELECTED on 2729 | done 2730 | config_var_get_settings 2731 | } 2732 | 2733 | 2734 | menu_tools(){ 2735 | local SELECTION 2736 | local DEVICE 2737 | local FORMAT 2738 | while true; do 2739 | SELECTION=$(whiptail --backtitle "$BACKTITLE" --title "TOOLS" --cancel-button "Back" --menu " Choose " $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT \ 2740 | "INSTALL" "Install $PRODUCTNAME into the menu" \ 2741 | "UPDATE" "Check for updates" \ 2742 | "UNINSTALL" "Uninstall $PRODUCTNAME" \ 2743 | " " " " \ 2744 | " " "=== MEDIA TOOLS (beta) ===" \ 2745 | "DETAILS" "Media details" \ 2746 | "ERASE" "Quick format media" \ 2747 | " " " " \ 2748 | "TERMINAL" "View the terminal (for debugging)" \ 2749 | "LOG" "View the log file" \ 2750 | "ABOUT" "About $PRODUCTNAME"\ 2751 | 3>&1 1>&2 2>&3) 2752 | 2753 | ES=$? 2754 | if (( $ES )); then 2755 | return $ES 2756 | else 2757 | case "$SELECTION" in 2758 | INSTALL) pisafe_install ;; 2759 | 2760 | UPDATE) pisafe_update gui ;; 2761 | 2762 | UNINSTALL) pisafe_uninstall ;; 2763 | 2764 | DETAILS) 2765 | DEVICE=$(menu_select_device "MEDIA DETAILS") 2766 | if (( $? )); then 2767 | ui_msg_error $LINENO "No media found." 2768 | #ui_msg BACKUP "No media found." 2769 | return 2 2770 | fi 2771 | media_details "$DEVICE" 2772 | ;; 2773 | 2774 | ERASE) 2775 | DEVICE=$(menu_select_device "ERASE MEDIA" "y") 2776 | if (( $? )); then 2777 | ui_msg_error $LINENO "No media found." 2778 | #ui_msg BACKUP "No media found." 2779 | return 2 2780 | fi 2781 | 2782 | FORMAT=$(whiptail --clear --backtitle "$BACKTITLE" --title "ERASE MEDIA: Select format" --ok-button "Select" --cancel-button "Back" --menu " " $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT \ 2783 | "fat32" " Most universal format for media under 32GB" \ 2784 | "exfat" " Most universal format for media over 32GB and files over 4GB" \ 2785 | "ntfs" " Native Windows format" \ 2786 | "ext4" " Native Linux format" 3>&1 1>&2 2>&3) 2787 | ES=$? 2788 | if (( $ES )); then 2789 | return $ES 2790 | fi 2791 | media_format "$DEVICE" "$FORMAT" 2792 | ;; 2793 | 2794 | TERMINAL) read -s -n 1 -p "Press any key to return" ; echo ;; 2795 | 2796 | LOG) $TEXT_EDITOR $LOG_FILE 2>/dev/null ;; # the error redirect is because mousepad kicks off a bunch of errors. 2797 | 2798 | HELP) whiptail --backtitle "$BACKTITLE" --title "HELP" --scrolltext --msgbox "$(pisafe_help)" $WT_HEIGHT_TALL $WT_WIDTH_WIDE ;; 2799 | 2800 | ABOUT) whiptail --backtitle "$BACKTITLE" --title "ABOUT" --scrolltext --msgbox "$(pisafe_about)" $WT_HEIGHT_TALL $WT_WIDTH_WIDE ;; 2801 | 2802 | " ") ;; 2803 | 2804 | *) whiptail --msgbox "Programmer error: unrecognized option" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT ;; 2805 | esac 2806 | fi 2807 | done 2808 | } 2809 | 2810 | 2811 | ########################################################## 2812 | 2813 | # if -v, show version and exit 2814 | if [[ $1 = -v ]]; then 2815 | echo $SCRIPTVER 2816 | exit 2817 | fi 2818 | 2819 | echo "$PRODUCTNAME $SCRIPTVER" 2820 | 2821 | # if uninstall then uninstall and exit 2822 | if [[ $1 = uninstall ]]; then 2823 | pisafe_uninstall $2 2824 | exit 2825 | fi 2826 | 2827 | config_var_init_configfile 2828 | config_var_get_settings 2829 | 2830 | # check dependencies 2831 | if [[ $CHECK_DEPENDENCIES_ON_STARTUP != off ]]; then 2832 | # if 'install -y' then install tools silently 2833 | if [[ $1 = install ]] && [[ $2 = -y ]]; then 2834 | pisafe_install_tools -y 2835 | else 2836 | pisafe_install_tools -n 2837 | fi 2838 | fi 2839 | 2840 | # check-for-updates if not "backup and -y" 2841 | if [[ $CHECK_FOR_UPDATES_ON_STARTUP != off ]] && ! ( [[ $1 = backup ]] && [[ $4 = -y ]] ) ; then 2842 | ui_echo "Checking for $PRODUCTNAME updates..." "" nolog 2843 | pisafe_update cli no 2844 | fi 2845 | 2846 | menu_cli $1 "$2" "$3" $4 2847 | -------------------------------------------------------------------------------- /pisafe_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RichardMidnight/pi-safe/90565ba441bbe892c2195c17512b97e4fe076a97/pisafe_icon.png -------------------------------------------------------------------------------- /snapshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RichardMidnight/pi-safe/90565ba441bbe892c2195c17512b97e4fe076a97/snapshot.png -------------------------------------------------------------------------------- /test_pisafe: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | # 2022/02/22 3 | 4 | # for manjaro 5 | # sudo pacman -S wget 6 | 7 | # exfat for suse https://software.opensuse.org/download/package?package=exfat-utils&project=filesystems 8 | # exfat for suse leap https://software.opensuse.org/download/package?package=exfat-utils&project=filesystems 9 | 10 | WHITE='\033[1;37m' 11 | RED='\033[1;31m' 12 | GREEN='\033[0;32m' 13 | BLUE='\033[0;34m' 14 | LTBLUE='\033[1;34m' 15 | COLOR_BROWN='\e[0;33m' 16 | NC='\033[0m' # No Color, standard text 17 | 18 | echo_white() { (echo -e "${WHITE}$*${NC}") } 19 | echo_red() { (echo -e "${RED}$*${NC}") } 20 | echo_blue() { (echo -e "${LTBLUE}$*${NC}") } 21 | echo_green() { (echo -e "${GREEN}$*${NC}") } 22 | echo_brown() { (echo -e "${COLOR_BROWN}$*${NC}")} 23 | 24 | 25 | ui_yesno(){ 26 | # general yes/no function. Supports CLI and whiptail. 27 | # note, this output is often assigned to a variable, so don't add any additional echo statements. 28 | TITLE="$1" 29 | MSG="$2" 30 | DEFAULT="${3:-""}" # defaults to Yes. can specify --defaultno 31 | TIMEOUT=${4:-0} 32 | # COLOR="${4:-white}" color does not work with the 'read' command 33 | 34 | if [[ $INTERFACE != gui ]]; then 35 | MSG="$PRODUCTNAME: $TITLE: $MSG [y/n]?" 36 | # change 'newline' chars to ' -' 37 | MSG=$(echo $MSG | sed 's/\\n/ -/g') 38 | while true; do 39 | read -s -n1 -p "$MSG" yn 40 | case $yn in 41 | [Yy]* ) echo y; return 0;; 42 | [Nn]* ) echo n; return 1;; 43 | * ) ;; 44 | esac 45 | done 46 | else 47 | whiptail --backtitle "$BACKTITLE" --title "$TITLE" $DEFAULT --yesno "$MSG" $WT_MB_HEIGHT $WT_MB_WIDTH 3>&1 1>&2 2>&3 48 | ES=$? 49 | if (( $ES )); then 50 | echo n 51 | else 52 | echo y 53 | fi 54 | return $ES 55 | fi 56 | } 57 | 58 | 59 | test_it(){ 60 | NAME="$1" 61 | CMD="$2" 62 | PAUSE=${3:--y} 63 | 64 | 65 | 66 | echo_green "$NAME" 67 | echo_blue "$CMD" 68 | eval "$CMD" 69 | if (( $? )); then 70 | echo_red ERROR 71 | fi 72 | 73 | if [[ $PAUSE = -y ]]; then 74 | echo_brown "Press any key ..." 75 | read -n1 76 | fi 77 | } 78 | 79 | 80 | shared(){ 81 | local SHARED_USER=user 82 | 83 | if [[ $(ui_yesno "setup share") = y ]]; then 84 | sudo apt install cifs-utils 85 | 86 | mkdir ~/shared 87 | 88 | sudo mount.cifs //192.168.1.18/shared ~/shared -o user=$SHARED_USER,vers=1.0 89 | fi 90 | } 91 | 92 | 93 | test_install(){ 94 | if [[ $(ui_yesno "test install") = y ]]; then 95 | 96 | echo_green GETTING PISAFE_BETA as PISAFE 97 | #wget https://raw.githubusercontent.com/RichardMidnight/pi-safe/main/pisafe_beta -O pisafe 98 | #if (( $? )); then echo_red ERROR; fi 99 | cp pisafe_beta pisafe 100 | 101 | 102 | # test_it "INSTALL..ANSWER N" "bash pisafe install" 103 | test_it "INSTALL -y" "bash pisafe install -y" 104 | test_it "UPDATE" "pisafe update" 105 | test_it "UNINSTALL -y" "pisafe uninstall -y" 106 | # test_it "INSTALL..should fail" "pisafe install -y" 107 | test_it "INSTALL -y" "bash pisafe install -y" 108 | fi 109 | } 110 | 111 | 112 | test_other(){ 113 | if [[ $(ui_yesno "test other") = y ]]; then 114 | test_it "BEEP" "pisafe beep" 115 | test_it HELP "pisafe help" 116 | test_it ABOUT "pisafe about" 117 | 118 | test_it LIST "pisafe list" 119 | test_it SETTINGS "pisafe settings" 120 | test_it LOG "pisafe log" 121 | 122 | test_it "DETAILS SDA" "pisafe details $DEVICE" 123 | 124 | # test_it "SETTINGS - change" "pisafe settings" 125 | # test_it DEFAULTS "pisafe defaults" 126 | # test_it "SETTINGS - review" "pisafe settings" 127 | fi 128 | } 129 | 130 | 131 | test_backup(){ 132 | # DATE=$(date +%Y-%d-%m_%I%M%p) 133 | if [[ $(ui_yesno "test backup") = y ]]; then 134 | 135 | rm test.img test.img.zip test.img.gz test.img.xz -y 136 | 137 | test_it "backup -y" "pisafe backup $DEVICE $OUTFILE -y" -n 138 | test_it "backup gz -y" "pisafe backup $DEVICE $OUTFILE.gz -y" -n 139 | test_it "backup xz -y" "pisafe backup $DEVICE $OUTFILE.xz -y" -n 140 | test_it "backup zst -y" "pisafe backup $DEVICE $OUTFILE.zst -y" -n 141 | test_it LIST "pisafe list" 142 | fi 143 | } 144 | 145 | 146 | test_format(){ 147 | if [[ $(ui_yesno "test format") = y ]]; then 148 | test_it "FORMAT $DEVICE FAT32 -y" "pisafe format $DEVICE fat32 -y" 149 | test_it "FORMAT $DEVICE EXFAT -y" "pisafe format $DEVICE exfat -y" 150 | test_it "FORMAT $DEVICE NTFS -y" "pisafe format $DEVICE ntfs -y" 151 | test_it "FORMAT $DEVICE EXT4 -y" "pisafe format $DEVICE ext4 -y" 152 | fi 153 | } 154 | 155 | 156 | test_restore(){ 157 | if [[ $(ui_yesno "test restore") = y ]]; then 158 | test_it "restore zip -y" "pisafe restore $OUTFILE.img.zip $DEVICE -y" -n 159 | test_it "restore gz -y" "pisafe restore $OUTFILE.img.gz $DEVICE -y" -n 160 | test_it "restore xz -y" "pisafe restore $OUTFILE.img.xz $DEVICE -y" -n 161 | test_it "restore zst -y" "pisafe restore $OUTFILE.img.zst $DEVICE -y" -n 162 | test_it "details" "pisafe details $DEVICE" 163 | fi 164 | } 165 | 166 | 167 | DEVICE=${1:-/dev/sda} 168 | OUTFILE=test 169 | 170 | echo_red Warning, Test_pisafe will will overwrite $DEVICE 171 | echo ctrl-c to quit, or any other key to contiue 172 | read -n1 173 | 174 | 175 | #shared 176 | 177 | 178 | test_it "VER" "bash pisafe -v" 179 | 180 | test_install 181 | test_other 182 | test_backup 183 | test_format 184 | test_restore 185 | 186 | pisafe log 187 | echo_green DONE TESTING 188 | 189 | 190 | --------------------------------------------------------------------------------