├── .github ├── FUNDING.yml └── workflows │ └── shellcheck.yml ├── LICENSE ├── README.md ├── backup ├── easybackup.sh ├── tarbackup2-conf.sh └── tarbackup2.sh ├── monitoring ├── domainwatch.sh ├── portswatch.sh ├── serviceswatch.sh ├── syswatch.sh └── webwatch.sh └── photo ├── pimport.sh ├── prename.sh └── presize.sh /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: jonaski 2 | patreon: jonaskvinge 3 | custom: https://paypal.me/jonaskvinge 4 | -------------------------------------------------------------------------------- /.github/workflows/shellcheck.yml: -------------------------------------------------------------------------------- 1 | name: shellcheck 2 | on: [push, pull_request] 3 | 4 | jobs: 5 | shellcheck: 6 | name: Shell Check 7 | runs-on: ubuntu-latest 8 | container: 9 | image: opensuse/leap:15.3 10 | steps: 11 | - uses: actions/checkout@v1.2.0 12 | - name: Update packages 13 | run: zypper --non-interactive --gpg-auto-import-keys ref 14 | - name: Install openSUSE dependencies 15 | run: zypper --non-interactive --gpg-auto-import-keys install ShellCheck 16 | 17 | - name: shellcheck 18 | run: | 19 | set -x 20 | for i in $(find . -type f -name '*.sh'); do 21 | shellcheck -s bash --exclude=SC2181 \ 22 | --exclude=SC1090 \ 23 | --exclude=SC2006 \ 24 | --exclude=SC2086 \ 25 | --exclude=SC2044 \ 26 | --exclude=SC2045 \ 27 | --exclude=SC2001 \ 28 | --exclude=SC2162 \ 29 | --exclude=SC2035 \ 30 | --exclude=SC2034 \ 31 | --exclude=SC2029 \ 32 | --exclude=SC2062 \ 33 | --exclude=SC2153 \ 34 | $i 35 | done 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | :shell: Unix Shell Scripts [![Build Status](https://github.com/jonaski/shellscripts/workflows/shellcheck/badge.svg)](https://github.com/jonaski/shellscripts/actions) 2 | ========================== 3 | 4 | These are some of the various unix shell scripts I've made over the years. 5 | 6 | The rsyncbackup3.sh script has moved to its own repository at https://github.com/jonaski/rsyncbackup 7 | -------------------------------------------------------------------------------- /backup/easybackup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # easybackup.sh - EASY RSYNC BACKUP SCRIPT 4 | # Copyright (C) 2011-2012 Jonas Kvinge 5 | # 6 | # This program is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU General Public License 17 | # along with this program. If not, see . 18 | # 19 | # 20 | # This is a very BASIC terminal rsync backup script. 21 | # You must understand basic shell scripts and rsync. 22 | # Modify it as you wish. USE AT YOUR OWN RISK!!! 23 | # 24 | # rsync options: 25 | # 26 | # Verbose: -v 27 | # Archive: -a (Same as -rlptgoD) 28 | # Recursive: -r 29 | # Checksum: -c 30 | # Time: -t 31 | # Dry run: -n 32 | # Delete: --delete 33 | # Relative path names: -R 34 | # 35 | 36 | version="0.2.5" 37 | 38 | rsyncargs="-va --delete --delete-excluded --itemize-changes" 39 | rsyncargsdry="-van --delete --delete-excluded --itemize-changes" 40 | logfile="/tmp/easybackup-${RANDOM}.log" 41 | backupexclude="\ 42 | /tmp 43 | /dev 44 | /proc 45 | /run 46 | /sys 47 | /media 48 | /lost+found 49 | /var/cache 50 | /var/tmp 51 | /var/run 52 | /var/spool/postfix/private 53 | /var/spool/postfix/public 54 | /var/lib/named/lib 55 | /var/lib/named/proc 56 | /var/lib/named/var 57 | /var/lib/named/dev 58 | /var/lib/mysql/mysql.sock 59 | /var/lib/ntp/proc 60 | /var/lib/ntp/dev 61 | /var/lib/dhcp/dev 62 | /var/lib/dhcp/proc 63 | /var/lib/dhcp6/dev 64 | /var/lib/dhcp6/proc 65 | /home/*/.xsession-errors 66 | /home/*/.local/share/Trash 67 | /home/*/.local/share/akonadi 68 | /home/*/.config/google-chrome/Default/Local Storage/* 69 | /home/*/.config/google-chrome/Cookies* 70 | /home/*/.config/google-chrome/History* 71 | /home/*/.config/google-chrome/Default/Application Cache 72 | /home/*/.cache 73 | /home/*/.thumbnails 74 | /home/*/.qt 75 | /home/*/.gvfs 76 | /home/*/.dbus 77 | /home/*/.netx 78 | /home/*/.kde/cache-* 79 | /home/*/.kde/tmp-* 80 | /home/*/.kde/socket-* 81 | /home/*/.kde4/cache-* 82 | /home/*/.kde4/tmp-* 83 | /home/*/.kde4/socket-* 84 | /home/*/.beagle 85 | /home/*/.pulse-cookie 86 | /home/*/.pulse 87 | /home/*/.xine 88 | /home/*/.mcop 89 | /home/*/.java 90 | /home/*/.adobe 91 | /home/*/.macromedia 92 | /home/*/.mozilla/firefox/*/Cache* 93 | /home/*/.opera/cache 94 | /home/*/.mythtv/*cache 95 | /home/*/.xmltv/cache 96 | /home/*/.googleearth 97 | /home/*/.wine 98 | /home/*/.wine_* 99 | /home/*/.kde4/share/apps/nepomuk 100 | /home/*/.kde/share/apps/amarok/albumcovers/cache 101 | /home/*/VirtualBox 102 | /mnt/data/Images 103 | /mnt/data/Backup 104 | /mnt/data/Xerox 105 | " 106 | 107 | ##################### DONT CHANGE ANYTHING BELOW HERE ##################### 108 | 109 | backup_logo() { 110 | 111 | tput bold 112 | tput setaf 7 113 | 114 | echo " 115 | ▓█████ ▄▄▄ ██████▓██ ██▓ ▄▄▄▄ ▄▄▄ ▄████▄ ██ ▄█▀ █ ██ ██▓███ 116 | ▓█ ▀▒████▄ ▒██ ▒ ▒██ ██▒▓█████▄ ▒████▄ ▒██▀ ▀█ ██▄█▒ ██ ▓██▒▓██░ ██▒ 117 | ▒███ ▒██ ▀█▄ ░ ▓██▄ ▒██ ██░▒██▒ ▄██▒██ ▀█▄ ▒▓█ ▄ ▓███▄░ ▓██ ▒██░▓██░ ██▓▒ 118 | ▒▓█ ▄░██▄▄▄▄██ ▒ ██▒ ░ ▐██▓░▒██░█▀ ░██▄▄▄▄██ ▒▓▓▄ ▄██▒▓██ █▄ ▓▓█ ░██░▒██▄█▓▒ ▒ 119 | ░▒████▒▓█ ▓██▒▒██████▒▒ ░ ██▒▓░░▓█ ▀█▓ ▓█ ▓██▒▒ ▓███▀ ░▒██▒ █▄▒▒█████▓ ▒██▒ ░ ░ 120 | ░░ ▒░ ░▒▒ ▓▒█░▒ ▒▓▒ ▒ ░ ██▒▒▒ ░▒▓███▀▒ ▒▒ ▓▒█░░ ░▒ ▒ ░▒ ▒▒ ▓▒░▒▓▒ ▒ ▒ ▒▓▒░ ░ ░ 121 | ░ ░ ░ ▒ ▒▒ ░░ ░▒ ░ ░▓██ ░▒░ ▒░▒ ░ ▒ ▒▒ ░ ░ ▒ ░ ░▒ ▒░░░▒░ ░ ░ ░▒ ░ 122 | ░ ░ ▒ ░ ░ ░ ▒ ▒ ░░ ░ ░ ░ ▒ ░ ░ ░░ ░ ░░░ ░ ░ ░░ 123 | ░ ░ ░ ░ ░ ░ ░ ░ ░ ░░ ░ ░ ░ ░ v${version} 124 | ░ ░ ░ ░ 125 | 126 | " 127 | 128 | #echo " 129 | #███████╗ █████╗ ███████╗██╗ ██╗██████╗ █████╗ ██████╗██╗ ██╗██╗ ██╗██████╗ 130 | #██╔════╝██╔══██╗██╔════╝╚██╗ ██╔╝██╔══██╗██╔══██╗██╔════╝██║ ██╔╝██║ ██║██╔══██╗ 131 | #█████╗ ███████║███████╗ ╚████╔╝ ██████╔╝███████║██║ █████╔╝ ██║ ██║██████╔╝ 132 | #██╔══╝ ██╔══██║╚════██║ ╚██╔╝ ██╔══██╗██╔══██║██║ ██╔═██╗ ██║ ██║██╔═══╝ 133 | #███████╗██║ ██║███████║ ██║ ██████╔╝██║ ██║╚██████╗██║ ██╗╚██████╔╝██║ 134 | #╚══════╝╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ 135 | #" 136 | 137 | tput sgr0 138 | 139 | } 140 | 141 | backup_init() { 142 | 143 | # Find out how to echo without newline 144 | 145 | c='' 146 | n='' 147 | if [ "$(eval echo -n 'a')" = "-n a" ] ; then 148 | c='\c' 149 | else 150 | n='-n' 151 | fi 152 | 153 | # See if the system has the needed commands to continue. 154 | 155 | for cmd in "which" "ls" "mkdir" "mv" "cp" "rm" "sed" "awk" "bc" "tr" "date" "md5sum" "udisksctl" "mount" "blkid" 156 | do 157 | which $cmd >/dev/null 2>&1 158 | if [ $? != 0 ] ; then 159 | echo "ERROR: Missing the \"${cmd}\" command!" 160 | backup_exit 161 | exit 1 162 | fi 163 | done 164 | 165 | # Parse parameters 166 | 167 | script=$(echo "$0" | sed 's/.*\///g') 168 | 169 | while getopts hvRcp:l: o 170 | do case "$o" in 171 | h) echo "Usage: $script -hvdpl " 172 | echo "" 173 | echo "-h Display this help and exit" 174 | echo "-v Display script version and exit" 175 | echo "-p Backup directory on backup disk, ie.: /Backup" 176 | echo "-l Logfile" 177 | echo "-R Relative names (rsync -R)" 178 | echo "-c Checksum (rsync -c)" 179 | echo "" 180 | echo "Examples:" 181 | echo "Source directories = /mnt/data /home/jonas" 182 | echo "Backupdisk labels = backupdisk1 backupdisk2" 183 | echo "The script will pick the first disk in the system that matches one of the disklabels." 184 | echo "" 185 | exit 0;; 186 | v) echo "$script v$version"; backup_exit; exit 0;; 187 | p) backupdir="$OPTARG";; 188 | l) logfile="$OPTARG";; 189 | c) rsyncargs="$rsyncargs -c"; rsyncargsdry="$rsyncargsdry -c";; 190 | R) rsyncargs="$rsyncargs -R"; rsyncargsdry="$rsyncargsdry -R";; 191 | \?) echo "ERROR: Unknown option: $script -h for help."; backup_exit; exit 1;; 192 | esac 193 | done 194 | # Reset $@ 195 | shift "$(echo "$OPTIND" - 1 | bc)" 196 | 197 | params="$*" 198 | for param in $params ; do 199 | echo "$param" | grep '/' >/dev/null 2>&1 200 | if [ $? = 0 ]; then 201 | if ! [ "$backupdisks" = "" ]; then 202 | echo "ERROR: You need to specify backup source directory before backup disk labels." 203 | backup_exit 204 | exit 1 205 | fi 206 | ls "$param" >/dev/null 2>&1 207 | if ! [ $? -eq 0 ] ; then 208 | echo "ERROR: File $param does not exist!." 209 | backup_exit 210 | exit 1 211 | fi 212 | if [ "$backupsource" = "" ]; then 213 | backupsource=$param 214 | else 215 | backupsource="$backupsource $param" 216 | fi 217 | else 218 | if [ "$backupdisks" = "" ]; then 219 | backupdisks=$param 220 | else 221 | backupdisks="$backupdisks $param" 222 | fi 223 | fi 224 | done 225 | 226 | if [ "$backupsource" = "" ]; then 227 | echo "ERROR: Missing backupsource." 228 | backup_exit 229 | exit 1 230 | fi 231 | if [ "$backupdisks" = "" ]; then 232 | echo "ERROR: Missing backupdisks." 233 | backup_exit 234 | exit 1 235 | fi 236 | backupsource="$backupsource /dev/null" 237 | 238 | echo "Source: $backupsource" 239 | echo "Backupdisks: $backupdisks" 240 | 241 | rm -f "$backupexcludestmpfile" 242 | 243 | backupexcludestmpfile="/tmp/easybackup-excludes-${RANDOM}.txt" 244 | echo "$backupexclude" >$backupexcludestmpfile || { backup_exit; } 245 | 246 | } 247 | 248 | backup_device() { 249 | 250 | found=0 251 | 252 | while true; do 253 | for i in $backupdisks 254 | do 255 | IFS=$'\n' 256 | for x in $(blkid) 257 | do 258 | device=$(echo "$x" | awk '{print $1}' | sed 's/://g') 259 | label=$(echo "$x" | awk '{print $2}') 260 | echo "$label" | grep '^LABEL=".*"$' >/dev/null 2>&1 261 | if ! [ $? = 0 ]; then 262 | continue 263 | fi 264 | label=$(echo "$label" | sed 's/LABEL=//g' | sed 's/"//g') 265 | if [ "$label" = "$i" ]; then 266 | found=1 267 | break 268 | fi 269 | done 270 | IFS=$' \t\n' 271 | if [ "$found" = "1" ]; then 272 | break 273 | fi 274 | done 275 | if ! [ "$found" = "1" ]; then 276 | read -e -n 1 -p "Insert backupdisk with labels \"$backupdisks\" and press any key to try again." 277 | continue 278 | fi 279 | break 280 | done 281 | 282 | echo "Using backupdisk \"$label\": \"$device\"." 283 | 284 | } 285 | 286 | backup_mount() { 287 | 288 | mountpoint= 289 | mounted= 290 | automounted= 291 | mount=$(mount | grep "^${device}") 292 | if [ "$mount" = "" ]; then 293 | echo $n "Mounting \"$device\". $c" 294 | udisksctl mount -b "$device" || { 295 | echo "ERROR: Unable to mount \"$device\"." 296 | backup_exit 297 | exit 1 298 | } 299 | # udisks may return 0 even when it's not mounted. 300 | mount=$(mount | grep ^${device}.*) 301 | if [ "$mount" = "" ]; then 302 | echo "Failed." 303 | backup_exit 304 | exit 1 305 | fi 306 | mountpoint=$(echo "$mount" | awk '{print $3}') 307 | if [ "$mountpoint" = "" ]; then 308 | echo "Failed to find mountpoint." 309 | echo "ERROR: Unable to find mountpoint for \"$device\"." 310 | backup_exit 311 | exit 1 312 | fi 313 | automounted=1 314 | #echo "$mountpoint." 315 | else 316 | echo $n "Probing \"$device\". $c" 317 | mountpoint=$(echo "$mount" | awk '{print $3}') 318 | if [ "$mountpoint" = "" ]; then 319 | echo "Failed to find mountpoint." 320 | echo "ERROR: Unable to find mountpoint for \"$device\"." 321 | backup_exit 322 | exit 1 323 | fi 324 | echo "$mountpoint." 325 | automounted=0 326 | fi 327 | 328 | echo "Device \"$device\" mounted on \"$mountpoint\"." 329 | mounted=1 330 | 331 | if [ "$backupdir" = "" ]; then 332 | backupdst=$mountpoint 333 | else 334 | echo "$backupdir" | grep '^/.*$' >/dev/null 2>&1 335 | if [ $? = 0 ]; then 336 | backupdst=${mountpoint}${backupdir} 337 | else 338 | backupdst=$mountpoint/$backupdir 339 | fi 340 | if ! [ -d "$backupdst" ]; then 341 | echo "Device \"$device\" missing directory \"$backupdir\"." 342 | backup_exit 343 | exit 1 344 | fi 345 | fi 346 | 347 | while true; do 348 | read -e -n 1 -p "Use $device (Y/N) " answer 349 | case $answer in 350 | [Yy]* ) answer=1;break;; 351 | [Nn]* ) answer=0;break;; 352 | * ) echo "Invalid answer. Press Y or N.";; 353 | esac 354 | if [ "$answer" = "0" ]; then 355 | backup_exit 356 | exit 1 357 | fi 358 | break 359 | done 360 | 361 | } 362 | 363 | backup_dryrun() { 364 | 365 | while true; do 366 | read -e -n 1 -p "DRY RUN first? (Y/N) " answer 367 | case $answer in 368 | [Yy]* ) break;; 369 | [Nn]* ) return 0;; 370 | * ) echo "Invalid answer. Press Y or N.";; 371 | esac 372 | done 373 | 374 | while true; do 375 | read -e -n 1 -p "Running rsync -n $rsyncargsdry --exclude-from=$backupexcludestmpfile $backupsource $backupdst (DRY RUN), continue? (Y/N) " answer 376 | case $answer in 377 | [Yy]* ) break;; 378 | [Nn]* ) backup_exit; exit 1;; 379 | * ) echo "Invalid answer. Press Y or N.";; 380 | esac 381 | done 382 | 383 | echo "BACKUPSOURCE: $backupsource" 384 | echo "BACKUPDST: $backupdst" 385 | 386 | echo "COMMAND: rsync -n $rsyncargsdry --exclude-from=$backupexcludestmpfile $backupsource $backupdst" >>"$logfile" 387 | rsync -n $rsyncargsdry --exclude-from="$backupexcludestmpfile" "$backupsource" "$backupdst" 2>&1 | tee -a "$logfile" 388 | 389 | while true; do 390 | read -e -n 1 -p "Dry run complete, view logfile ($logfile). (Y/N) " answer 391 | case $answer in 392 | [Yy]* ) 393 | kate "$logfile" >/dev/null 2>&1 & 394 | break;; 395 | [Nn]* ) break;; 396 | * ) echo "Invalid answer. Press Y or N.";; 397 | esac 398 | done 399 | 400 | while true; do 401 | read -e -n 1 -p "Dry run complete, continue with these changes?. (Y/N) " answer 402 | case $answer in 403 | [Yy]* ) break;; 404 | [Nn]* ) backup_exit; exit 1;; 405 | * ) echo "Invalid answer. Press Y or N.";; 406 | esac 407 | done 408 | 409 | return 0 410 | 411 | } 412 | 413 | backup_run() { 414 | 415 | while true; do 416 | read -e -n 1 -p "Run rsync $rsyncargs --exclude-from=$backupexcludestmpfile $backupsource $backupdst, continue? (Y/N) " answer 417 | case $answer in 418 | [Yy]* ) break;; 419 | [Nn]* ) backup_exit; exit 1;; 420 | * ) echo "Invalid answer. Press Y or N.";; 421 | esac 422 | done 423 | 424 | echo "COMMAND: rsync $rsyncargs --exclude-from=$backupexcludestmpfile $backupsource $backupdst" >>"$logfile" 425 | rsync "$rsyncargs" --exclude-from="$backupexcludestmpfile" "$backupsource" "$backupdst" 2>&1 | tee -a "$logfile" 426 | 427 | return 0 428 | 429 | } 430 | 431 | backup_confirm() { 432 | 433 | while true; do 434 | read -e -n 1 -p "Using backup destination $backupdst, EVERYTHING IN THERE MIGHT BE DELETED OR OVERWRITTEN. ARE YOU SURE? (Y/N) " answer 435 | case $answer in 436 | [Yy]* ) break;; 437 | [Nn]* ) backup_exit; exit 1;; 438 | * ) echo "Invalid answer. Press Y or N.";; 439 | esac 440 | done 441 | 442 | return 0 443 | 444 | } 445 | 446 | backup_exit() { 447 | 448 | rm -f $backupexcludestmpfile 449 | if [ "$automounted" = "1" ]; then 450 | umount "$device" 451 | fi 452 | read -e -n 1 -p "Press any key to exit..." 453 | exit 0 454 | 455 | } 456 | 457 | backup_logo || exit 1 458 | backup_init "$@" || exit 1 459 | backup_device || exit 1 460 | backup_mount || exit 1 461 | backup_confirm || exit 1 462 | backup_dryrun || exit 1 463 | backup_run || exit 1 464 | backup_exit || exit 1 465 | -------------------------------------------------------------------------------- /backup/tarbackup2-conf.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # tarbackup2-conf.sh - Configuration file for tarbackup2.sh 4 | # Copyright (C) 2011-2012 Jonas Kvinge 5 | # 6 | # This program is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU General Public License 17 | # along with this program. If not, see . 18 | # 19 | # 20 | # tarbackup.sh creates a backup specified by backupfileprefix and 21 | # backupfilerevision in b2zipped tar archives. 22 | # 23 | # Files to include in the backups are specified by backupfiles 24 | # the list of files in backupexclude are files to exclude from the 25 | # backup, such as temp files, sockets and cache. 26 | # 27 | # New in version 2: "backuplocations" allows you to specify unlimited 28 | # number of locations and also supports mounting smb and ftp. 29 | # With new success/failure reporting. 30 | # 31 | 32 | backupid="$(hostname -s)" # <--- An unique ID for the backup filename ---> 33 | backupdate="$(date '+%Y%m%d-%H%M%S')" # <--- Datestamp in reverse used as the revision for the backup filename ---> 34 | 35 | backupfileprefix="abackup-$backupid" # <--- A prefix for the backup filename ---> 36 | backupfilerevision=$backupdate # <--- A revision that will increase on each run ---> 37 | 38 | backupemailsuccess="root" # <--- Where to send backup success report, set to "0" for no report ---> 39 | backupemailfailure="root" # <--- Where to send backup failure report, set to "0" for no report ---> 40 | backupemailfailureall="root" # <--- Where to send backup report, set to "0" for no report ---> 41 | 42 | backupold2keep=3 # <--- Number of OLD backups to keep after new backup is done, 0 will only keep the NEW backup file and erase all previous backups! ---> 43 | 44 | backupmysql=1 # <--- Enable backup of local mysql databases 0/1 ---> 45 | backupmysqlfileprefix="abackup-$backupid-mysql" # <--- File prefix for the mysql dump file ---> 46 | backupmysqlfilerevision=$backupdate # <--- File revision for the mysql dump file ---> 47 | backupmysqlcmd="/usr/bin/mysqldump --all-databases -u root" # <--- Command to dump database ---> 48 | 49 | backuppgsql=1 # <--- Enable backup of local postgres databases 0/1 */ ---> 50 | backuppgsqlfileprefix="abackup-$backupid-pgsql" # <--- File prefix for the postgres dump file --.> 51 | backuppgsqlfilerevision=$backupdate # <--- File revision for the postgres dump file -.-> 52 | backuppgsqlcmd="/usr/local/pgsql/bin/pg_dumpall -U postgres" # <--- Command to dump database ---> 53 | 54 | backupsshfsargs= 55 | backupsshargs= 56 | 57 | # Locations is where you store backups, the first one listed will be used to store the actual files, the next ones are secondary 58 | # locations where you want to copy the backups to. 59 | # The backups can be storted locally, via ssh/sftp, via smb, or via ftp. 60 | # Syntax are, Local: /mnt/backup, SSH(SFTP): host:/mnt/backup, SMB: smb://user:pass@host/dir, FTP: ftp://user:pass@host/dir 61 | # If the directory starts with /mnt or /media, it will be automatically mounted and umounted if it is not mounted already. 62 | # The FIRST listed location should not be FTP. 63 | 64 | backuplocations="\ 65 | host1:/mnt/datadisk/backup/abackup/$backupid 66 | host2:/mnt/backup/abackup/$backupid 67 | host3:/mnt/store/abackup/$backupid 68 | " 69 | 70 | # Enter files to backup here 71 | 72 | backupfiles="\ 73 | etc 74 | var/spool/mail 75 | var/spool/cron 76 | var/lib/samba 77 | var/lib/named 78 | var/lib/mysql 79 | var/lib/pgsql 80 | lib/firmware/dvb-fe-* 81 | srv 82 | home 83 | usr/local 84 | home/jonas/temp 85 | " 86 | 87 | # Enter files to exclude here, like temp files and sockets 88 | 89 | backupexclude="\ 90 | home/*/temp 91 | home/*/Temp 92 | home/*/backup 93 | home/*/Backup 94 | home/*/build 95 | home/*/.xsession-errors 96 | home/*/.local/share/Trash 97 | home/*/.cache 98 | home/*/.thumbnails 99 | home/*/.qt 100 | home/*/.gvfs 101 | home/*/.dbus 102 | home/*/.netx 103 | home/*/.kde/cache-* 104 | home/*/.kde/tmp-* 105 | home/*/.kde/socket-* 106 | home/*/.kde4/cache-* 107 | home/*/.kde4/tmp-* 108 | home/*/.kde4/socket-* 109 | home/*/.beagle 110 | home/*/.pulse-cookie 111 | home/*/.pulse 112 | home/*/.xine 113 | home/*/.mcop 114 | home/*/.java 115 | home/*/.adobe 116 | home/*/.macromedia 117 | home/*/.mozilla/firefox/*/Cache* 118 | home/*/.opera/cache 119 | home/*/.mythtv/*cache 120 | home/*/.xmltv/cache 121 | home/*/.googleearth 122 | home/*/.wine 123 | home/*/.wine_* 124 | home/*/.kde4/share/apps/nepomuk 125 | home/*/.kde/share/apps/amarok/albumcovers/cache 126 | var/cache 127 | var/tmp 128 | var/run 129 | var/spool/postfix/private 130 | var/spool/postfix/public 131 | var/lib/named/lib 132 | var/lib/named/proc 133 | var/lib/named/var 134 | var/lib/named/dev 135 | var/lib/mysql/mysql.sock 136 | var/lib/ntp/proc 137 | var/lib/samba/unexpected 138 | home/jonas/photos 139 | " 140 | -------------------------------------------------------------------------------- /backup/tarbackup2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # tarbackup2.sh - TAR BACKUP SCRIPT 4 | # Copyright (C) 2011-2012 Jonas Kvinge 5 | # 6 | # This program is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU General Public License 17 | # along with this program. If not, see . 18 | # 19 | 20 | backupversion="2.1.1" 21 | 22 | backupid=$(hostname -s) 23 | backupdate=$(date '+%Y%m%d-%H%M%S') 24 | 25 | backupsshfsargs= 26 | filelocation= 27 | backupexclude= 28 | backupmountpoint= 29 | backupmysqlcmd= 30 | backuppgsqlcmd= 31 | backupfileprefix= 32 | backupold2keep= 33 | backupmysqlfileprefix= 34 | backuppgsqlfileprefix= 35 | backupemailsuccess= 36 | backupemailfailure= 37 | backupemailfailureall= 38 | backupfilerevision= 39 | backupmysqlfilerevision= 40 | backuppgsqlfilerevision= 41 | 42 | # Functions below here 43 | 44 | backup_local() { 45 | 46 | backupdir=$backuplocation 47 | mountpoint=$backuplocation 48 | mountpointdir=$backuplocation 49 | mounted= 50 | 51 | topdir=$(echo "$backupdir" | cut -d'/' -f2) 52 | if [ "$topdir" = "mnt" ] || [ "$topdir" = "media" ] ; then 53 | mountpoint=$(echo "$backupdir" | cut -d'/' -f-3) 54 | mountpoint -q "$mountpoint" 55 | if [ $? != 0 ] ; then 56 | mount "$mountpoint" || { 57 | if [ "$count" = "1" ]; then 58 | exit_failure 59 | fi 60 | return 1 61 | } 62 | mounted=1 63 | if [ "$count" = "1" ]; then 64 | mounted1=1 65 | mountpoint1=$mountpoint 66 | fi 67 | fi 68 | fi 69 | mkdir -p "$backupdir" || exit_failure 70 | 71 | if [ "$count" = "1" ]; then 72 | backup_create 73 | else 74 | backup_copy || { 75 | if [ "$mounted" = "1" ] ; then 76 | umount "$mountpoint" 77 | fi 78 | return 1 79 | } 80 | fi 81 | 82 | backup_delete 83 | 84 | if ! [ "$count" = "1" ] && [ "$mounted" = "1" ] ; then 85 | umount "$mountpoint" 86 | fi 87 | 88 | return 0 89 | 90 | } 91 | 92 | backup_ssh() { 93 | 94 | backuphost=$(echo "$backuplocation" | cut -d':' -f1) 95 | backupdir=$(echo "$backuplocation" | cut -d':' -f2-) 96 | 97 | if [ "$backuphost" = "$backupid" ] ; then 98 | backuplocation=$backupdir 99 | backup_local 100 | return $? 101 | fi 102 | 103 | mountpoint="/tmp/ssh-$backupid-$backuphost-$backupdate-$RANDOM" 104 | mountpointdir=$mountpoint 105 | 106 | if [ "$count" = "1" ]; then 107 | backup_ssh_mount || { rmdir "$mountpoint" >/dev/null 2>&1; exit_failure; } 108 | mounted1=1 109 | mountpoint1=$mountpoint 110 | backup_create 111 | else 112 | #ssh $backupsshargs $backuphost mkdir -p $backupdir || return 1 113 | backup_ssh_copy || return 1 114 | backup_ssh_mount || { rmdir "$mountpoint" >/dev/null 2>&1; return 1; } 115 | fi 116 | 117 | backup_delete 118 | 119 | if ! [ "$count" = "1" ]; then 120 | fusermount -u "$mountpoint" && rmdir "$mountpoint" 121 | fi 122 | 123 | return 0 124 | 125 | } 126 | 127 | backup_ssh_copy() { 128 | 129 | echo "$n" "Copying tar backup \"$backuplocationsrc/$backupfile.bz2\" to location \"$backuplocation\". $c" 130 | scp -qp "$backupfileorig" "$backuplocation"/ || return 1 131 | echo "Done." 132 | scp -qp "$backupfileorigmd5" "$backuplocation"/ || return 1 133 | if [ "$backupmysql" = "1" ] ; then 134 | echo "$n" "Copying mysql backup \"$backuplocationsrc/$backupmysqlfile.bz2\" to location \"$backuplocation\". $c" 135 | scp -qp "$backupmysqlfileorig" "$backuplocation" || return 1 136 | echo "Done." 137 | scp -qp "$backupmysqlfileorigmd5" "$backuplocation" || return 1 138 | fi 139 | if [ "$backuppgsql" = "1" ] ; then 140 | echo "$n" "Copying pgsql backup \"$backuplocationsrc/$backuppgsqlfile.bz2\" to location \"$backuplocation\". $c" 141 | scp -qp "$backuppgsqlfileorig" "$backuplocation" || return 1 142 | echo "Done." 143 | scp -qp "$backuppgsqlfileorigmd5" "$backuplocation" || return 1 144 | fi 145 | 146 | return 0 147 | 148 | } 149 | 150 | backup_ssh_mount() { 151 | 152 | mkdir -p "$mountpoint" || return 1 153 | #ssh $backupsshargs $backuphost mkdir -p $backupdir || return 1 154 | sshfs "$backupsshfsargs" "$backuplocation" "$mountpoint" || return 1 155 | 156 | return 0 157 | 158 | } 159 | 160 | backup_smb() { 161 | 162 | backupproto="$(echo "$backuplocation" | grep '://' | sed -e 's,^\(.*://\).*,\1,g')" 163 | backupurl=$(echo "$backuplocation" | sed -e s,${backupproto},,g) 164 | backupuser="$(echo "$backupurl" | grep '@' | cut -d'@' -f1 | cut -d':' -f1)" 165 | backuppass="$(echo "$backupurl" | grep '@' | cut -d'@' -f1 | cut -d':' -f2)" 166 | backuphost="$(echo "$backupurl" | sed -e s,${backupuser}.*@,,g | cut -d/ -f1)" 167 | backupshare="$(echo "$backupurl" | cut -d'/' -f2)" 168 | backupdir="$(echo "$backupurl" | grep '/' | cut -d'/' -f3-)" 169 | backupdir="/$backupdir" 170 | 171 | mountpoint="/tmp/smb-$backupid-$backuphost-$backupdate-$RANDOM" 172 | mountpointdir="$mountpoint$backupdir" 173 | 174 | mkdir -p "$mountpoint" || return 1 175 | 176 | if ! [ "$backupuser" = "" ] && ! [ "$backuppass" = "" ] ; then 177 | backupoptions="-o username=$backupuser,password=$backuppass" 178 | elif ! [ "$backupuser" = "" ] ; then 179 | backupoptions="-o username=$backupuser" 180 | else 181 | backupoptions= 182 | fi 183 | 184 | mount -t cifs "//$backuphost/$backupshare" "$backupoptions" "$mountpoint" || { rmdir "$mountpoint"; return 1; } 185 | 186 | if [ "$count" = "1" ]; then 187 | mounted1=1 188 | mountpoint1=$mountpoint 189 | backup_create 190 | else 191 | backup_copy || { umount "$mountpoint" && rmdir "$mountpoint"; return 1; } 192 | fi 193 | 194 | backup_delete 195 | 196 | if ! [ "$count" = "1" ]; then 197 | umount "$mountpoint" && rmdir "$mountpoint" 198 | fi 199 | 200 | return 0 201 | 202 | } 203 | 204 | backup_ftp() { 205 | 206 | if [ "$count" = "1" ]; then 207 | echo "ERROR: Can't use FTP as the first backup location!" >&2 208 | exit_failure 209 | fi 210 | 211 | backuplocation2=$(echo "$backuplocation" | sed -e 's/ftp:\/\///g') 212 | if [ "$backuplocation2" = "" ]; then 213 | echo "ERROR: Cant figure out URL for \"$backuplocation\"." >&2 214 | return 1 215 | fi 216 | 217 | mountpoint="/tmp/ftp-$backupid-$backuphost-$backupdate-$RANDOM" 218 | mountpointdir=$mountpoint 219 | mkdir -p "$mountpoint" || return 1 220 | 221 | curlftpfs "$backuplocation2" "$mountpoint" || { rmdir "$mountpoint"; return 1; } 222 | 223 | if [ "$count" = "1" ]; then 224 | mounted1=1 225 | mountpoint1=$mountpoint 226 | backup_create 227 | else 228 | backup_copy || { umount "$mountpoint" && rmdir "$mountpoint"; return 1; } 229 | fi 230 | 231 | backup_delete 232 | 233 | if ! [ "$count" = "1" ]; then 234 | umount "$mountpoint" && rmdir "$mountpoint" 235 | fi 236 | 237 | return 0 238 | 239 | } 240 | 241 | backup_create() { 242 | 243 | backuplocationsrc=$backuplocation 244 | backupfileorig="$mountpointdir/$backupfile" 245 | 246 | for i in "$backupfileorig" "$backupfileorig.bz2" 247 | do 248 | if [ -f "$i" ] ; then 249 | echo "ERROR: File $i already exist on \"$filelocation\"!" >&2 250 | exit_failure 251 | fi 252 | done 253 | 254 | echo "tar -cvf $backupfileorig -C / --ignore-failed-read -X $backupexcludestmpfile $backupfiles" 255 | 256 | echo "$n" "Creating tar archive as \"$backuplocationsrc/$backupfile\". $c" 257 | echo "$backupexclude" >"$backupexcludestmpfile" || exit_failure 258 | echo "$backupmountpoint" >>"$backupexcludestmpfile" || exit_failure 259 | tar -cvf "$backupfileorig" -C / --ignore-failed-read -X "$backupexcludestmpfile" "$backupfiles" >"$backupfilestmpfile" || { rm -f "$backupfileorig"; exit_failure; return 1; } 260 | echo "Done." 261 | echo "$n" "Compressing tar archive as \"$backuplocationsrc/$backupfile.bz2\". $c" 262 | bzip2 "$backupfileorig" || { rm -f "$backupfileorig"; rm -f "${backupfileorig}.bz2"; exit_failure; return 1; } 263 | echo "Done." 264 | backupfileorig="$backupfileorig.bz2" 265 | backupfileorigmd5="$backupfileorig.md5" 266 | echo "$n" "Creating MD5 checksum file \"$backuplocationsrc/$backupfile.bz2.md5\". $c" 267 | md5sum "$backupfileorig" >"$backupfileorigmd5" && echo "Done." 268 | 269 | # MYSQL 270 | 271 | if [ "$backupmysql" = "1" ] ; then 272 | 273 | backupmysqlfileorig="$mountpointdir/$backupmysqlfile" 274 | 275 | for i in "$backupmysqlfileorig" "$backupmysqlfileorig.bz2" 276 | do 277 | if [ -f "$i" ] ; then 278 | echo "ERROR: File $i already exist on \"$filelocation\"!" >&2 279 | return 280 | fi 281 | done 282 | 283 | echo "$n" "Dumping mysql database to \"$backuplocationsrc/$backupmysqlfile\". $c" 284 | "$backupmysqlcmd" > "$backupmysqlfileorig" 285 | if [ $? != 0 ] ; then 286 | backupmysql=0 287 | else 288 | echo "Done." 289 | echo "$n" "Compressing mysql database as \"$backuplocationsrc/$backupmysqlfile.bz2\". $c" 290 | bzip2 "$backupmysqlfileorig" 291 | if [ $? != 0 ] ; then 292 | backupmysql=0 293 | else 294 | echo "Done." 295 | backupmysqlfileorig="$backupmysqlfileorig.bz2" 296 | backupmysqlfileorigmd5="$backupmysqlfileorig.md5" 297 | echo "$n" "Creating mysql database MD5 checksum file \"$backuplocationsrc/$backupmysqlfile.bz2.md5\". $c" 298 | md5sum "$backupmysqlfileorig" >"$backupmysqlfileorigmd5" && echo "Done." 299 | fi 300 | fi 301 | fi 302 | 303 | # POSTGRESQL 304 | 305 | if [ "$backuppgsql" = "1" ] ; then 306 | 307 | backuppgsqlfileorig="$mountpointdir/$backuppgsqlfile" 308 | 309 | for i in "$backuppgsqlfileorig" "$backuppgsqlfileorig.bz2" 310 | do 311 | if [ -f "$i" ] ; then 312 | echo "ERROR: File $i already exist on \"$filelocation\"!" >&2 313 | return 314 | fi 315 | done 316 | 317 | echo "$n" "Dumping pgsql database to \"$backuplocationsrc/$backuppgsqlfile\". $c" 318 | $backuppgsqlcmd > "$backuppgsqlfileorig" 319 | if [ $? != 0 ] ; then 320 | backuppgsql=0 321 | else 322 | echo "Done." 323 | echo "$n" "Compressing pgsql database as \"$backuplocationsrc/$backuppgsqlfile.bz2\". $c" 324 | bzip2 "$backuppgsqlfileorig" 325 | if [ $? != 0 ] ; then 326 | backuppgsql=0 327 | else 328 | echo "Done." 329 | backuppgsqlfileorig="$backuppgsqlfileorig.bz2" 330 | backuppgsqlfileorigmd5="$backuppgsqlfileorig.md5" 331 | echo "$n" "Creating pgsql database MD5 checksum file \"$backuplocationsrc/$backuppgsqlfile.bz2.md5\". $c" 332 | md5sum "$backuppgsqlfileorig" >"$backuppgsqlfileorigmd5" && echo "Done." 333 | fi 334 | fi 335 | fi 336 | 337 | return 0 338 | 339 | } 340 | 341 | backup_copy() { 342 | 343 | echo "$n" "Copying tar backup \"$backuplocationsrc/$backupfile.bz2\" to location \"$backuplocation\". $c" 344 | cp --preserve=timestamps "$backupfileorig" "$mountpointdir" || return 1 345 | echo "Done." 346 | cp --preserve=timestamps "$backupfileorigmd5" "$mountpointdir" || return 1 347 | if [ "$backupmysql" = "1" ] ; then 348 | echo "$n" "Copying mysql backup \"$backuplocationsrc/$backupmysqlfile.bz2\" to location \"$backuplocation\". $c" 349 | cp --preserve=timestamps "$backupmysqlfileorig" "$mountpointdir" || return 1 350 | echo "Done." 351 | cp --preserve=timestamps "$backupmysqlfileorigmd5" "$mountpointdir" || return 1 352 | fi 353 | if [ "$backuppgsql" = "1" ] ; then 354 | echo "$n" "Copying pgsql backup \"$backuplocationsrc/$backuppgsqlfile.bz2\" to location \"$backuplocation\". $c" 355 | cp --preserve=timestamps "$backuppgsqlfileorig" "$mountpointdir" || return 1 356 | echo "Done." 357 | cp --preserve=timestamps "$backuppgsqlfileorigmd5" "$mountpointdir" || return 1 358 | fi 359 | 360 | return 0 361 | 362 | } 363 | 364 | backup_delete() { 365 | 366 | backups=0 367 | for i in $(ls -1 -r "$mountpointdir") 368 | do 369 | if ! echo "$i" | grep "^$backupfileprefix-.*\.tar\.bz2$" >/dev/null 2>&1 ; then 370 | continue 371 | fi 372 | if [ "$i" = "$backupfile.bz2" ] ; then 373 | continue 374 | fi 375 | backups=$(echo "$backups" + 1 | bc) 376 | if [ "$backups" -gt "$backupold2keep" ] ; then 377 | echo "$n" "Deleting old backup \"$backuplocation/$i\". $c" 378 | rm "$mountpointdir/$i" && echo "Done." 379 | rm -f "$mountpointdir/$i.md5" 380 | fi 381 | done 382 | 383 | # Delete old myql backups 384 | 385 | if [ "$backupmysql" = "1" ] ; then 386 | backups=0 387 | for i in $(ls -1 -r "$mountpointdir") 388 | do 389 | if ! echo "$i" | grep "$backupmysqlfileprefix-.*\.sql\.bz2$" >/dev/null 2>&1 ; then 390 | continue 391 | fi 392 | if [ "$i" = "$backupmysqlfile.bz2" ] ; then 393 | continue 394 | fi 395 | backups=$(echo "$backups" + 1 | bc) 396 | if [ "$backups" -gt "$backupold2keep" ] ; then 397 | echo "$n" "Deleting old mysql backup \"$backuplocation/$i\". $c" 398 | rm "$mountpointdir/$i" && echo "Done." 399 | rm -f "$mountpointdir/$i.md5" 400 | fi 401 | done 402 | fi 403 | 404 | # Delete old pgsql backups 405 | 406 | if [ "$backuppgsql" = "1" ] ; then 407 | backups=0 408 | for i in $(ls -1 -r "$mountpointdir") 409 | do 410 | if ! echo "$i" | grep "$backuppgsqlfileprefix-.*\.sql\.bz2$" >/dev/null 2>&1 ; then 411 | continue 412 | fi 413 | if [ "$i" = "$backuppgsqlfile.bz2" ] ; then 414 | continue 415 | fi 416 | backups=$(echo "$backups" + 1 | bc) 417 | if [ "$backups" -gt "$backupold2keep" ] ; then 418 | echo "$n" "Deleting old pgsql backup \"$backuplocation/$i\". $c" 419 | rm "$mountpointdir/$i" && echo "Done." 420 | rm -f "$mountpointdir/$i.md5" 421 | fi 422 | done 423 | fi 424 | 425 | } 426 | 427 | exit_cleanup() { 428 | 429 | rm -f "$backupexcludestmpfile" 430 | rm -f "$backupfilestmpfile" 431 | 432 | # Umount drives 433 | 434 | if [ "$mounted1" = "1" ] ; then 435 | echo "$mountpoint1" | grep '^\/tmp\/ssh-' >/dev/null 2>&1 436 | if [ $? -eq 0 ] ; then 437 | fusermount -u "$mountpoint1" && rmdir "$mountpoint1" 438 | else 439 | echo "$mountpoint1" | grep '^\/tmp\/smb-' >/dev/null 2>&1 440 | if [ $? -eq 0 ] ; then 441 | umount "$mountpoint1" && rmdir "$mountpoint1" 442 | else 443 | echo "$mountpoint1" | grep '^\/tmp\/ftp-' >/dev/null 2>&1 444 | if [ $? -eq 0 ] ; then 445 | umount "$mountpoint1" && rmdir "$mountpoint1" 446 | else 447 | umount "$mountpoint1" 448 | fi 449 | fi 450 | fi 451 | fi 452 | 453 | } 454 | 455 | report_success() { 456 | 457 | LANG="en_GB" 458 | mail -s "Tar backup for $(hostname) on $(date) to location $backuplocation was successful" -a "$backupfilestmpfile" "$backupemailsuccess" <"; exit 0;; 582 | v) echo "$script v$backupversion"; exit 0;; 583 | \?) echo "Usage: $script -hv -c "; exit 1;; 584 | esac 585 | done 586 | 587 | # Reset $@ 588 | shift "$(echo "$OPTIND" - 1 | bc)" 589 | 590 | # Locate configuration file 591 | 592 | if [ "$backupconfig" = "" ] ; then 593 | backupconfig="tarbackup2-conf.sh" 594 | fi 595 | 596 | if ! [ -f "$backupconfig" ]; then 597 | if [ -f "./$backupconfig" ]; then 598 | backupconfig="./$backupconfig" 599 | else 600 | if [ -f "$runpath/$backupconfig" ]; then 601 | backupconfig="$runpath/$backupconfig" 602 | fi 603 | fi 604 | fi 605 | 606 | # Check for existence of needed config file and read it 607 | 608 | if ! [ -f "$backupconfig" ]; then 609 | echo "ERROR: Backup configuration file \"$backupconfig\" does not exist!" >&2 610 | exit 1 611 | fi 612 | 613 | if ! [ -r "$backupconfig" ]; then 614 | echo "ERROR: Can't read configuration file \"$backupconfig\"." >&2 615 | exit 1; 616 | fi 617 | 618 | . $backupconfig 619 | 620 | # Check that we got the configuration needed 621 | 622 | for i in \ 623 | "backupid" \ 624 | "backupdate" \ 625 | "backupfileprefix" \ 626 | "backupfilerevision" \ 627 | "backupemailsuccess" \ 628 | "backupemailfailure" \ 629 | "backupemailfailureall" \ 630 | "backupold2keep" \ 631 | "backupmysql" \ 632 | "backupmysqlfileprefix" \ 633 | "backupmysqlfilerevision" \ 634 | "backupmysqlcmd" \ 635 | "backuppgsql" \ 636 | "backuppgsqlfileprefix" \ 637 | "backuppgsqlfilerevision" \ 638 | "backuppgsqlcmd" \ 639 | "backuplocations" \ 640 | "backupfiles" \ 641 | "backupexclude" 642 | do 643 | if [ "${!i}" = "" ] ; then 644 | echo "ERROR: Missing configuration variable \"$i\" in \"$backupconfig\"!" >&2 645 | exit 1 646 | fi 647 | done 648 | 649 | # Check that we got all the needed commands 650 | 651 | cmds="which cp scp mv rm mkdir rmdir cat tr bc cut sed grep mail tar bzip2 md5sum date hostname mount umount mountpoint fusermount" 652 | for backuplocation in $backuplocations 653 | do 654 | echo "$backuplocation" | grep -i '.*:\/\/.*\/' >/dev/null 2>&1 655 | if [ $? -eq 0 ] ; then 656 | echo "$backuplocation" | grep -i '^ftp:\/\/.*\/' >/dev/null 2>&1 657 | if [ $? -eq 0 ] ; then 658 | cmds="$cmds curlftpfs" 659 | continue 660 | fi 661 | else 662 | echo "$backuplocation" | grep '.*:\/' >/dev/null 2>&1 663 | if [ $? -eq 0 ] ; then 664 | cmds="$cmds sshfs" 665 | continue 666 | fi 667 | fi 668 | done 669 | 670 | for cmd in $cmds 671 | do 672 | which "$cmd" >/dev/null 2>&1 673 | if [ $? != 0 ] ; then 674 | echo "ERROR: Missing \"${cmd}\" command!" >&2 675 | exit_failure 676 | fi 677 | done 678 | 679 | # Set variables 680 | 681 | backupfile="$backupfileprefix-$backupfilerevision.tar" 682 | backupmysqlfile="$backupmysqlfileprefix-$backupmysqlfilerevision.sql" 683 | backuppgsqlfile="$backuppgsqlfileprefix-$backuppgsqlfilerevision.sql" 684 | 685 | backupfilestmpfile="/tmp/tarbackupfiles-$backupdate-$RANDOM.txt" 686 | backupexcludestmpfile="/tmp/tarbackupexcludes-$backupdate-$RANDOM.txt" 687 | 688 | backuplocations=$(echo "$backuplocations" | tr '\n' ' ' | sed -e 's/^ //g' | sed -e 's/ / /g') 689 | 690 | # Create backupfiles 691 | # Only include files that actually exist to avoid tar error report 692 | 693 | backupfilesX=$(echo "$backupfiles" | tr '\n' ' ' | sed -e 's/^ //g' | sed -e 's/ / /g') 694 | backupfiles= 695 | for ((i=1; ; i++)) 696 | do 697 | s=$(echo "$backupfilesX" | cut -d' ' -f"$i") 698 | if [ "$s" = "" ]; then 699 | break 700 | fi 701 | s2=$s 702 | echo "$s" | grep -i '^\/.*' >/dev/null 2>&1 703 | if ! [ $? -eq 0 ] ; then 704 | s2="/$s" 705 | fi 706 | ls "$s2" >/dev/null 2>&1 707 | if [ $? -eq 0 ] ; then 708 | backupfiles="$backupfiles $s" 709 | else 710 | echo "Excluding \"$s\" from backup, can't access!" 711 | fi 712 | done 713 | backupfiles=$(echo "$backupfiles" | sed -e 's/^ //g') 714 | 715 | if [ "$backupfiles" = "" ] ; then 716 | echo "ERROR: No backupfiles exist!" >&2 717 | exit_failure 718 | fi 719 | 720 | # Loop locations, create tar, transfer new backups and delete old backups 721 | 722 | count=0 723 | locationssuccess=0 724 | locationsfailure=0 725 | for backuplocation in $backuplocations 726 | do 727 | 728 | count=$(echo "$count" + 1 | bc) 729 | echo "Performing backup to \"$backuplocation\"" 730 | 731 | backuphost= 732 | backupdir= 733 | mounted= 734 | mountpoint= 735 | mountpointdir= 736 | ret= 737 | 738 | echo "$backuplocation" | grep -i '.*:\/\/.*\/' >/dev/null 2>&1 739 | if [ $? -eq 0 ] ; then 740 | echo "$backuplocation" | grep -i '^smb:\/\/.*\/' >/dev/null 2>&1 741 | if [ $? -eq 0 ] ; then # SMB 742 | backup_smb 743 | ret=$? 744 | fi 745 | echo "$backuplocation" | grep -i '^ftp:\/\/.*\/' >/dev/null 2>&1 746 | if [ $? -eq 0 ] ; then # FTP 747 | backup_ftp 748 | ret=$? 749 | fi 750 | else 751 | echo "$backuplocation" | grep '^\/' >/dev/null 2>&1 752 | if [ $? -eq 0 ] ; then # Local 753 | backup_local 754 | ret=$? 755 | fi 756 | echo "$backuplocation" | grep '.*:\/' >/dev/null 2>&1 757 | if [ $? -eq 0 ] ; then # SSH 758 | backup_ssh 759 | ret=$? 760 | fi 761 | fi 762 | 763 | if [ "$ret" = "" ] ; then 764 | echo "ERROR: Unknown backup location: \"$backuplocation\"" >&2 765 | if [ "$count" -lt 2 ] ; then 766 | if ! [ "$backupemailfailureall" = "0" ]; then 767 | report_failure_all 768 | fi 769 | exit_failure 770 | fi 771 | elif [ "$ret" -eq "0" ] ; then 772 | locationssuccess=$(echo "$locationssuccess" + 1 | bc) 773 | if ! [ "$backupemailsuccess" = "0" ]; then 774 | report_success 775 | fi 776 | else 777 | locationsfailure=$(echo "$locationsfailure" + 1 | bc) 778 | if ! [ "$backupemailfailure" = "0" ]; then 779 | report_failure 780 | fi 781 | fi 782 | 783 | done 784 | 785 | if [ "$locationssuccess" -gt "0" ] ; then 786 | exit_success 787 | else 788 | if ! [ "$backupemailfailureall" = "0" ]; then 789 | report_failure_all 790 | fi 791 | exit_failure 792 | fi 793 | -------------------------------------------------------------------------------- /monitoring/domainwatch.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # domainwatch.sh - Monitor Domain Watch 4 | # Copyright (C) 2017 Jonas Kvinge 5 | # 6 | # This program is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU General Public License 17 | # along with this program. If not, see . 18 | # 19 | 20 | configfile="/etc/sysconfig/domainwatch" 21 | lockfile="/tmp/domainwatch.lock" 22 | lockfilettl=3600 23 | tmpfile="/tmp/domainwatch.tmp" 24 | logfile="/tmp/domainwatch.log" 25 | #log=1 26 | debug=1 27 | inettesthosts="8.8.8.8 8.8.4.4" # Test all of these, only if 1 fails internet is reported to be down. 28 | pingtimeout=2 29 | #maxfailtime=1 30 | reportfreq=3600 31 | emailfrom="nobody" 32 | emailto="root" 33 | 34 | domains="test1.com test2.com test3.com" 35 | 36 | timestamp() { TS=$(date '+%d/%m-%Y %H:%M:%S'); } 37 | 38 | print() { 39 | timestamp 40 | if [ -t 1 ] && ! [ "$color" = "" ]; then 41 | tput bold 42 | tput setaf "$color" 43 | fi 44 | echo "[$TS] $*" 45 | if [ -t 1 ]; then 46 | tput sgr0 47 | fi 48 | color=0 49 | } 50 | log() { 51 | timestamp 52 | echo "[$TS] $*" >>"${logfile}" 53 | } 54 | statusprint() { color=8; print "$@"; } 55 | statuslog() { log "$@"; } 56 | errorprint() { 57 | timestamp 58 | if [ -t 1 ]; then 59 | tput bold 60 | tput setaf 1 61 | fi 62 | echo "[$TS] ERROR: $*" >&2 63 | if [ -t 1 ]; then 64 | tput sgr0 65 | fi 66 | color=0 67 | } 68 | errorlog() { log "ERROR: $*"; } 69 | status() { statusprint "$@"; statuslog "$@"; } 70 | error() { errorprint "$@"; errorlog "$@"; } 71 | debug() { 72 | if [ "$debug" = "1" ]; then 73 | color=3 74 | print "$@" 75 | log "$@" 76 | fi 77 | } 78 | 79 | isnum() { 80 | echo "$1" | grep -E '^\-?[0-9]+$' >/dev/null 2>&1 81 | return $? 82 | } 83 | 84 | loadconfig() { 85 | 86 | if [ -f "$configfile" ]; then 87 | . $configfile || { 88 | error "Failed to load configuration file \"$configfile\"." 89 | exit_safe 1 90 | } 91 | else 92 | status "Configuration file not found: \"$configfile\" - Using defaults." 93 | fi 94 | 95 | } 96 | 97 | main() { 98 | 99 | init "$@" 100 | 101 | cmdcheck 102 | inetstatus 103 | domainwatch 104 | 105 | if [ "$sendreport" = "1" ]; then 106 | sendreport 107 | fi 108 | 109 | scriptend=$(date +%s) 110 | scripttime=$(echo "$scriptend" - "$scriptstart" | bc) 111 | status "Script finished in $(date -u -d @${scripttime} +"%T")" 112 | 113 | exit_safe 0 114 | 115 | } 116 | 117 | init() { 118 | 119 | scriptstart=$(date +%s) 120 | havelockfile=0 121 | 122 | status "Monitor Domain Watch - Starting - $0" 123 | 124 | loadconfig 125 | 126 | if [ -f "$lockfile" ]; then 127 | error "Script is already running. If this is incorrect, remove: $lockfile." 128 | exit_safe 129 | fi 130 | which lockfile >/dev/null 2>&1 131 | if [ $? -eq 0 ] ; then 132 | lockfile -r 0 -l "$lockfilettl" "$lockfile" || { exit_safe 1; } 133 | else 134 | touch "$lockfile" || { exit_safe 1; } 135 | fi 136 | havelockfile=1 137 | 138 | touch "$tmpfile" || exit_safe 1 139 | touch "$logfile" || exit_safe 1 140 | 141 | } 142 | 143 | exit_safe() { 144 | 145 | debug "exit_safe()" 146 | 147 | if [ "$havelockfile" -eq 1 ]; then 148 | rm -f "$lockfile" 149 | fi 150 | 151 | exit $? 152 | } 153 | 154 | # Check that we got all the needed commands 155 | 156 | cmdcheck() { 157 | 158 | debug "cmdcheck()" 159 | 160 | cmds="which cat cut tr sed grep bc cp mv rm mkdir date hostname mail ssh tput ping whois mutt" 161 | for cmd in $cmds 162 | do 163 | which "$cmd" >/dev/null 2>&1 164 | if [ $? != 0 ] ; then 165 | echo "ERROR: Missing \"${cmd}\" command!" 166 | exit_safe 1 167 | fi 168 | done 169 | 170 | debug "cmdcheck() finished" 171 | 172 | } 173 | 174 | inetstatus() { 175 | 176 | debug "inetstatus()" 177 | 178 | #inetwasdown= 179 | inetwasdowntext= 180 | 181 | inetup=0 182 | for inettesthost in $inettesthosts 183 | do 184 | pingtext=$(ping -c 1 -W "${pingtimeout}" "${inettesthost}") 185 | r=$? 186 | if [ $r = 0 ] ; then 187 | inetup=1 188 | break 189 | fi 190 | done 191 | 192 | if [ "$inetup" -eq 1 ]; then 193 | status "Internet connection found to be up." 194 | entry=$(grep -i "INETDOWN Time=.*" ${tmpfile}) 195 | if ! [ "$entry" = "" ]; then 196 | #inetwasdown=1 197 | #rm -f ${tmpfile} || exit_safe 1 198 | #touch ${tmpfile} || exit_safe 1 199 | sed -i "/^INETDOWN .*$/d" ${tmpfile} 200 | timenow=$(date +%s) 201 | entrytime=$(echo "$entry" | sed -e 's/.* Time=\(.*\).*/\1/g' | cut -d' ' -f1) 202 | if isnum "$entrytime" ; then 203 | time=$(echo "$timenow" - "$entrytime" | bc) 204 | timetext=$(date -u -d @${time} +"%T") 205 | inetwasdowntext="Internet was down $timetext" 206 | status "$inetwasdowntext" 207 | else 208 | inetwasdowntext="Internet was down - Failed to calculate downtime." 209 | status "$inetwasdowntext" 210 | entrytime=0 211 | fi 212 | fi 213 | return $r 214 | else 215 | status "Internet connection was found to be down." 216 | entry=$(grep -i "INETDOWN Time=.*" ${tmpfile}) 217 | if [ "$entry" = "" ]; then 218 | timenow=$(date +%s) 219 | echo "INETDOWN Time=${timenow}" >${tmpfile} 220 | debug "Internet was found to be down - Writing to ${tmpfile}" 221 | else 222 | debug "Internet was found to be down - Existing info found in ${tmpfile}" 223 | fi 224 | exit_safe $r 225 | fi 226 | return $r 227 | 228 | debug "inetstatus() finished" 229 | 230 | } 231 | 232 | domainwatch() { 233 | 234 | debug "domainwatch()" 235 | 236 | for domain in ${domains} 237 | do 238 | readfile 239 | status= 240 | available=0 241 | result_html= 242 | output=$(whois "$domain" 2>&1) 243 | if [ $? -eq 0 ]; then 244 | IFS_DEFAULT=$IFS 245 | IFS=$'\n' 246 | for line in $output 247 | do 248 | IFS=$IFS_DEFAULT 249 | echo "$line" | grep '^% No match$' >/dev/null 2>&1 250 | if [ $? -eq 0 ]; then 251 | status="available" 252 | result_html="Available" 253 | available=1 254 | fi 255 | done 256 | if [ "$status" = "" ]; then 257 | status="taken" 258 | result_html="Taken" 259 | available=0 260 | fi 261 | elif [ $? -eq 1 ]; then 262 | status="available" 263 | result_html="Available" 264 | available=1 265 | else 266 | status="error" 267 | result_html="Error" 268 | available=0 269 | fi 270 | 271 | status "Domain: $domain Status: $status" 272 | 273 | writefile 274 | 275 | maildata="$maildata 276 | Domain: $domain - ${result_html}
" 277 | 278 | done 279 | 280 | debug "domainwatch() finished" 281 | 282 | } 283 | 284 | readfile() { 285 | 286 | debug "readfile()" 287 | 288 | #entryfail=0 289 | #failtime=0 290 | reporttime=0 291 | entrytime=0 292 | entrystatus= 293 | #entryresult= 294 | 295 | entry=$(grep -i "^${domain} .*" ${tmpfile}) 296 | if [ "$entry" = "" ]; then 297 | return 298 | fi 299 | entrystatus=$(echo "$entry" | cut -d' ' -f2) 300 | if [ "$entrystatus" = "" ]; then 301 | entry= 302 | return 303 | fi 304 | 305 | reporttime=$(echo "$entry" | cut -d' ' -f3) 306 | if [ "$reporttime" = "" ] || ! isnum "$reporttime"; then 307 | entry= 308 | entrystatus= 309 | reporttime=0 310 | return 311 | fi 312 | entrytime=$(echo "$entry" | cut -d' ' -f4) 313 | if [ "$entrytime" = "" ] || ! isnum "$entrytime"; then 314 | entry= 315 | entrystatus= 316 | reporttime=0 317 | entrytime=0 318 | return 319 | fi 320 | 321 | timenow=$(date +%s) 322 | time=$(echo "$timenow" - "$entrytime" | bc) 323 | debug "Entry with timestamp \"${entrytime}\" ($(date -u -d @${time} +"%T") ago) found for domain \"$domain\". Status: $entrystatus ReportTime: $reporttime" 324 | 325 | debug "readfile() finished" 326 | 327 | } 328 | 329 | writefile() { 330 | 331 | debug "writefile()" 332 | 333 | echo "$entry" | grep -i "^${domain} ${status} .*" >/dev/null 2>&1 334 | if [ $? -eq 0 ]; then 335 | changed=0 336 | else 337 | changed=1 338 | fi 339 | debug "domain: ${domain} - Changed=$changed" 340 | 341 | timenow=$(date +%s) 342 | reporttime_bc=$(echo "$timenow" - "$reporttime" | bc) 343 | 344 | if [ "$entry" = "" ]; then 345 | entrytime=$(date +%s) 346 | summary_add "This is the first domain watch for \"$domain\"." 347 | sendreport=1 348 | reporttime=$(date +%s) 349 | elif [ "$available" -eq 1 ]; then 350 | summary_add "Domain \"$domain\" is available." 351 | if [ "$reporttime_bc" -ge "$reportfreq" ]; then 352 | sendreport=1 353 | reporttime=$(date +%s) 354 | fi 355 | fi 356 | 357 | if [ "$entrytime" -le 0 ]; then 358 | entrytime=$(date +%s) 359 | fi 360 | 361 | sed -i "/^${domain} .*$/d" $tmpfile 362 | echo "${domain} ${status} ${reporttime} ${entrytime}" >>${tmpfile} 363 | 364 | debug "writefile() finished" 365 | 366 | } 367 | 368 | summary_add() { 369 | 370 | if [ "$summary" = "" ]; then 371 | summary="$1
" 372 | else 373 | summary="$summary 374 | $1
" 375 | fi 376 | 377 | } 378 | 379 | report_add() { 380 | 381 | if [ "$reportdata" = "" ]; then 382 | reportdata="$1" 383 | else 384 | reportdata="$reportdata 385 | $1" 386 | fi 387 | 388 | } 389 | 390 | sendreport() { 391 | 392 | debug "sendreport()" 393 | 394 | status "Sending monitor domain watch report..." 395 | 396 | scriptend=$(date +%s) 397 | scripttime=$(echo "$scriptend" - "$scriptstart" | bc) 398 | reporttime=$(date +%s) 399 | 400 | report_add "" 401 | report_add "" 402 | report_add "

Monitor domain Watch

" 403 | if ! [ "$inetwasdowntext" = "" ]; then 404 | report_add "

$inetwasdowntext

" 405 | fi 406 | if ! [ "$summary" = "" ]; then 407 | report_add "

$summary

" 408 | fi 409 | report_add "
" 410 | report_add "Status:
" 411 | report_add "$maildata" 412 | report_add "
" 413 | report_add "New report is sent every $(date -u -d @${reportfreq} +"%T")." 414 | 415 | LANG=en_GB 416 | EMAIL=$emailfrom mutt -e 'set content_type=text/html' -s "Monitor domain Watch" $emailto <. 18 | # 19 | 20 | configfile="/etc/sysconfig/portswatch" 21 | lockfile="/tmp/portswatch.lock" 22 | lockfilettl=3600 23 | tmpfile="/tmp/portswatch.tmp" 24 | logfile="/tmp/portswatch.log" 25 | #log=1 26 | debug=1 27 | inettesthosts="8.8.8.8 8.8.4.4" # Test all of these, only if 1 fails internet is reported to be down. 28 | pingtimeout=2 29 | connecttimeout=2 30 | maxfailtime=15 31 | reportfreq=120 32 | emailfrom="nobody" 33 | emailto="root" 34 | 35 | hosts=" 36 | server1 ssh/tcp domain/udp domain/tcp smtp/tcp imaps/tcp pop3s/tcp http/tcp https/tcp 37 | server2 ssh/tcp domain/udp domain/tcp smtp/tcp http/tcp https/tcp 38 | server3 ssh/tcp domain/udp domain/tcp smtp/tcp 39 | server4 ssh/tcp domain/udp domain/tcp smtp/tcp 40 | " 41 | 42 | timestamp() { TS=$(date '+%d/%m-%Y %H:%M:%S'); } 43 | 44 | print() { 45 | timestamp 46 | if [ -t 1 ] && ! [ "$color" = "" ]; then 47 | tput bold 48 | tput setaf "$color" 49 | fi 50 | echo "[$TS] $*" 51 | if [ -t 1 ]; then 52 | tput sgr0 53 | fi 54 | color=0 55 | } 56 | log() { 57 | timestamp 58 | echo "[$TS] $*" >>${logfile} 59 | } 60 | statusprint() { color=8; print "$@"; } 61 | statuslog() { log "$@"; } 62 | errorprint() { 63 | timestamp 64 | if [ -t 1 ]; then 65 | tput bold 66 | tput setaf 1 67 | fi 68 | echo "[$TS] ERROR: $*" >&2 69 | if [ -t 1 ]; then 70 | tput sgr0 71 | fi 72 | color=0 73 | } 74 | errorlog() { log "ERROR: $*"; } 75 | status() { statusprint "$@"; statuslog "$@"; } 76 | error() { errorprint "$@"; errorlog "$@"; } 77 | debug() { 78 | if [ "$debug" = "1" ]; then 79 | color=3 80 | print "$@" 81 | log "$@" 82 | fi 83 | } 84 | 85 | isnum() { 86 | 87 | echo "$1" | grep -E '^\-?[0-9]+$' >/dev/null 2>&1 88 | return $? 89 | 90 | } 91 | 92 | loadconfig() { 93 | 94 | if [ -f "$configfile" ]; then 95 | . $configfile || { 96 | error "Failed to load configuration file \"$configfile\"." 97 | exit_safe 1 98 | } 99 | else 100 | status "Configuration file not found: \"$configfile\" - Using defaults." 101 | fi 102 | 103 | } 104 | 105 | main() { 106 | 107 | init "$@" 108 | 109 | cmdcheck 110 | inetstatus 111 | portswatch 112 | 113 | if [ "$sendreport" = "1" ]; then 114 | sendreport 115 | fi 116 | 117 | scriptend=$(date +%s) 118 | scripttime=$(echo "$scriptend" - "$scriptstart" | bc) 119 | status "Script finished in $(date -u -d @${scripttime} +"%T")" 120 | 121 | exit_safe 0 122 | 123 | } 124 | 125 | init() { 126 | 127 | scriptstart=$(date +%s) 128 | havelockfile=0 129 | 130 | status "Monitor Ports Watch - Starting - $0" 131 | 132 | loadconfig 133 | 134 | if [ -f "$lockfile" ]; then 135 | error "Script is already running. If this is incorrect, remove: $lockfile." 136 | exit_safe 137 | fi 138 | which lockfile >/dev/null 2>&1 139 | if [ $? -eq 0 ] ; then 140 | lockfile -r 0 -l "$lockfilettl" "$lockfile" || { exit_safe 1; } 141 | else 142 | touch "$lockfile" || { exit_safe 1; } 143 | fi 144 | havelockfile=1 145 | 146 | touch $tmpfile || exit_safe 1 147 | touch $logfile || exit_safe 1 148 | 149 | } 150 | 151 | exit_safe() { 152 | 153 | debug "exit_safe()" 154 | 155 | if [ "$havelockfile" -eq 1 ]; then 156 | rm -f $lockfile 157 | fi 158 | 159 | exit $? 160 | 161 | } 162 | 163 | # Check that we got all the needed commands 164 | 165 | cmdcheck() { 166 | 167 | debug "cmdcheck()" 168 | 169 | cmds="which cat cut tr sed grep bc cp mv rm mkdir date hostname ssh tput ping nc mutt" 170 | for cmd in $cmds 171 | do 172 | which "$cmd" >/dev/null 2>&1 173 | if [ $? != 0 ] ; then 174 | echo "ERROR: Missing \"${cmd}\" command!" 175 | exit_safe 1 176 | fi 177 | done 178 | 179 | debug "cmdcheck() finished" 180 | 181 | } 182 | 183 | inetstatus() { 184 | 185 | debug "inetstatus()" 186 | 187 | #inetwasdown= 188 | inetwasdowntext= 189 | 190 | inetup=0 191 | for inettesthost in $inettesthosts 192 | do 193 | pingtext=$(ping -c 1 -W "${pingtimeout}" "${inettesthost}") 194 | r=$? 195 | if [ $r = 0 ] ; then 196 | inetup=1 197 | break 198 | fi 199 | done 200 | 201 | if [ "$inetup" -eq 1 ]; then 202 | status "Internet connection found to be up." 203 | entry=$(grep -i "INETDOWN Time=.*" ${tmpfile}) 204 | if ! [ "$entry" = "" ]; then 205 | #inetwasdown=1 206 | #rm -f ${tmpfile} || exit_safe 1 207 | #touch ${tmpfile} || exit_safe 1 208 | sed -i "/^INETDOWN .*$/d" ${tmpfile} 209 | timenow=$(date +%s) 210 | entrytime=$(echo "$entry" | sed -e 's/.* Time=\(.*\).*/\1/g' | cut -d' ' -f1) 211 | if isnum "$entrytime" ; then 212 | time=$(echo "$timenow" - "$entrytime" | bc) 213 | timetext=$(date -u -d @${time} +"%T") 214 | inetwasdowntext="Internet was down $timetext" 215 | status "$inetwasdowntext" 216 | else 217 | inetwasdowntext="Internet was down - Failed to calculate downtime." 218 | status "$inetwasdowntext" 219 | entrytime=0 220 | fi 221 | fi 222 | return $r 223 | else 224 | status "Internet connection was found to be down." 225 | entry=$(grep -i "INETDOWN Time=.*" ${tmpfile}) 226 | if [ "$entry" = "" ]; then 227 | timenow=$(date +%s) 228 | echo "INETDOWN Time=${timenow}" >${tmpfile} 229 | debug "Internet was found to be down - Writing to ${tmpfile}" 230 | else 231 | debug "Internet was found to be down - Existing info found in ${tmpfile}" 232 | fi 233 | exit_safe $r 234 | fi 235 | return $r 236 | 237 | debug "inetstatus() finished" 238 | 239 | } 240 | 241 | portswatch() { 242 | 243 | debug "portswatch()" 244 | 245 | #i=0 246 | IFS_DEFAULT=$IFS 247 | IFS=$'\n' 248 | for l in ${hosts} 249 | do 250 | IFS=$IFS_DEFAULT 251 | if [ "$l" = "" ]; then 252 | continue 253 | fi 254 | host=$(echo "$l" | awk '{ print $1}') 255 | ports=$(echo "$l" | cut -d ' ' -f2-) 256 | status= 257 | fail=0 258 | failtime=0 259 | ports_status= 260 | ports_status_html= 261 | debug "Host: $host - Ports: $ports" 262 | readfile 263 | for portent in $ports 264 | do 265 | port=$(echo "$portent" | cut -d '/' -f1) 266 | protocol=$(echo "$portent" | cut -d '/' -f2) 267 | status "Checking host: $host - port: $portent" 268 | if [ "$protocol" = "tcp" ]; then 269 | nc -zw ${connecttimeout} "$host" "$port" 270 | elif [ "$protocol" = "udp" ]; then 271 | nc -zuw ${connecttimeout} "$host" "$port" 272 | else 273 | continue 274 | fi 275 | if [ $? -eq 0 ]; then # Port is up 276 | status="up" 277 | status "Host: $host - Port: $portent - Status: Up" 278 | else 279 | status="down" 280 | status "Host: $host - Port: $portent - Status: Down" 281 | fail=1 282 | if [ "$failtime" -le 1 ]; then 283 | failtime=$(date +%s) 284 | fi 285 | fi 286 | portsstatus_add 287 | done 288 | 289 | portswatchdata="$portswatchdata 290 | Host: $host - ${ports_status_html}
" 291 | 292 | writefile 293 | done 294 | 295 | debug "portswatch() finished" 296 | 297 | } 298 | 299 | readfile() { 300 | 301 | debug "readfile()" 302 | 303 | entryfail=0 304 | failtime=0 305 | reporttime=0 306 | entrytime=0 307 | entry_ports_status= 308 | 309 | entry=$(grep -i "^$host .*" ${tmpfile}) 310 | if [ "$entry" = "" ]; then 311 | return 312 | fi 313 | entry_ports_status=$(echo "$entry" | cut -d' ' -f2) 314 | if [ "$entry_ports_status" = "" ]; then 315 | entry= 316 | return 317 | fi 318 | 319 | entryfail=$(echo "$entry" | cut -d' ' -f3) 320 | if [ "$entryfail" = "" ] || ! isnum "$entryfail"; then 321 | entry= 322 | entry_ports_status= 323 | entryfail=0 324 | return 325 | fi 326 | 327 | failtime=$(echo "$entry" | cut -d' ' -f4) 328 | if [ "$failtime" = "" ] || ! isnum "$failtime"; then 329 | entry= 330 | entry_ports_status= 331 | entryfail=0 332 | failtime=0 333 | return 334 | fi 335 | 336 | reporttime=$(echo "$entry" | cut -d' ' -f5) 337 | if [ "$reporttime" = "" ] || ! isnum "$reporttime"; then 338 | entry= 339 | entry_ports_status= 340 | entryfail=0 341 | failtime=0 342 | reporttime=0 343 | return 344 | fi 345 | 346 | entrytime=$(echo "$entry" | cut -d' ' -f6) 347 | if [ "$entrytime" = "" ] || ! isnum "$entrytime"; then 348 | entry= 349 | entry_ports_status= 350 | entryfail=0 351 | entrytime=0 352 | failtime=0 353 | reporttime=0 354 | return 355 | fi 356 | 357 | timenow=$(date +%s) 358 | time=$(echo "$timenow" - "$entrytime" | bc) 359 | debug "Entry with timestamp \"${entrytime}\" ($(date -u -d @${time} +"%T") ago) found for host \"$host\". Status: $entry_ports_status Fail: $entryfail FailTime: $failtime ReportTime: $reporttime" 360 | 361 | debug "readfile() finished" 362 | 363 | } 364 | 365 | portsstatus_add() { 366 | 367 | debug "portsstatus_add()" 368 | 369 | if [ "${ports_status}" = "" ]; then 370 | ports_status="$portent=$status" 371 | if [ "$status" = "up" ]; then 372 | ports_status_html="$portent=$status" 373 | else 374 | ports_status_html="$portent=$status" 375 | fi 376 | else 377 | ports_status="${ports_status},$portent=$status" 378 | if [ "$status" = "up" ]; then 379 | ports_status_html="${ports_status_html} $portent=$status" 380 | else 381 | ports_status_html="${ports_status_html} $portent=$status" 382 | fi 383 | fi 384 | 385 | debug "portsstatus_add() finished" 386 | 387 | } 388 | 389 | writefile() { 390 | 391 | debug "writefile()" 392 | 393 | debug "ports_status_____ : $ports_status" 394 | debug "entry_ports_status: $entry_ports_status" 395 | 396 | echo "$entry" | grep -i "^$host ${ports_status} .*" >/dev/null 2>&1 397 | if [ $? -eq 0 ]; then 398 | changed=0 399 | else 400 | changed=1 401 | fi 402 | 403 | timenow=$(date +%s) 404 | failtime_bc=$(echo "$timenow" - "$failtime" | bc) 405 | reporttime_bc=$(echo "$timenow" - "$reporttime" | bc) 406 | 407 | debug "Host: $host - Changed=$changed" 408 | 409 | if [ "$entry" = "" ]; then 410 | entrytime=$(date +%s) 411 | summary_add "This is the first ports watch for \"$host\"." 412 | sendreport=1 413 | reporttime=$timenow 414 | elif [ "$fail" -eq 1 ] && [ "$failtime_bc" -ge "$maxfailtime" ]; then 415 | entryfail=1 416 | summary_add "One or more ports on \"$host\" failed for more than $(date -u -d @$maxfailtime +"%T")." 417 | if [ "$reporttime_bc" -ge "$reportfreq" ]; then 418 | sendreport=1 419 | reporttime=$timenow 420 | fi 421 | elif [ "$fail" -eq 0 ] && [ "$entryfail" -eq 1 ]; then 422 | entryfail=0 423 | summary_add "\"$host\" restored from one or more failed ports." 424 | failtime=0 425 | sendreport=1 426 | reporttime=$timenow 427 | fi 428 | 429 | if [ "$fail" -eq 0 ]; then 430 | entryfail=0 431 | failtime=0 432 | fi 433 | 434 | if [ "$entrytime" -le 0 ]; then 435 | entrytime=$(date +%s) 436 | fi 437 | 438 | sed -i "/^${host} .*$/d" ${tmpfile} 439 | echo "${host} ${ports_status} ${entryfail} ${failtime} ${reporttime} ${entrytime}" >>${tmpfile} 440 | 441 | debug "writefile() finished" 442 | 443 | } 444 | 445 | summary_add() { 446 | 447 | if [ "$summary" = "" ]; then 448 | summary="$1
" 449 | else 450 | summary="$summary 451 | $1
" 452 | fi 453 | 454 | } 455 | 456 | report_add() { 457 | 458 | if [ "$reportdata" = "" ]; then 459 | reportdata="$1" 460 | else 461 | reportdata="$reportdata 462 | $1" 463 | fi 464 | 465 | } 466 | 467 | sendreport() { 468 | 469 | debug "sendreport()" 470 | 471 | status "Sending monitor ports watch report..." 472 | 473 | scriptend=$(date +%s) 474 | scripttime=$(echo "$scriptend" - "$scriptstart" | bc) 475 | reporttime=$(date +%s) 476 | 477 | report_add "" 478 | report_add "" 479 | report_add "

Monitor Ports Watch

" 480 | if ! [ "$inetwasdowntext" = "" ]; then 481 | report_add "

$inetwasdowntext

" 482 | fi 483 | if ! [ "$summary" = "" ]; then 484 | report_add "

$summary

" 485 | fi 486 | report_add "Status:
" 487 | report_add "$portswatchdata" 488 | report_add "
" 489 | report_add "New report is sent every $(date -u -d @${reportfreq} +"%T") if status is changed or there is an error." 490 | report_add "" 491 | report_add "" 492 | 493 | LANG=en_GB 494 | EMAIL=$emailfrom mutt -e 'set content_type=text/html' -s "Monitor Ports Watch" $emailto <. 18 | # 19 | 20 | configfile="/etc/sysconfig/serviceswatch" 21 | lockfile="/tmp/serviceswatch.lock" 22 | lockfilettl=3600 23 | tmpfile="/tmp/serviceswatch.tmp" 24 | logfile="/tmp/serviceswatch.log" 25 | #log=1 26 | debug=1 27 | inettesthosts="8.8.8.8 8.8.4.4" # Test all of these, only if 1 fails internet is reported to be down. 28 | pingtimeout=2 29 | connecttimeout=2 30 | maxfailtime=15 31 | reportfreq=120 32 | emailfrom="nobody" 33 | emailto="root" 34 | options="-o ConnectTimeout=${connecttimeout} -oBatchMode=yes" 35 | 36 | hosts=" 37 | server1 rsyslog sshd named sendmail saslauthd spfmilter opendkim opendmarc milter-regex spamd dovecot apache 38 | server2 rsyslog sshd named sendmail saslauthd spfmilter opendkim opendmarc apache 39 | server3 rsyslog sshd named sendmail saslauthd spfmilter opendkim opendmarc milter-regex mailscanner clamd 40 | server4 rsyslog sshd named sendmail saslauthd spfmilter opendkim opendmarc milter-regex mailscanner clamd 41 | " 42 | 43 | timestamp() { TS=$(date '+%d/%m-%Y %H:%M:%S'); } 44 | 45 | print() { 46 | timestamp 47 | if [ -t 1 ] && ! [ "$color" = "" ]; then 48 | tput bold 49 | tput setaf "$color" 50 | fi 51 | echo "[$TS] $*" 52 | if [ -t 1 ]; then 53 | tput sgr0 54 | fi 55 | color=0 56 | } 57 | log() { 58 | timestamp 59 | echo "[$TS] $*" >>${logfile} 60 | } 61 | statusprint() { color=8; print "$@"; } 62 | statuslog() { log "$@"; } 63 | errorprint() { 64 | timestamp 65 | if [ -t 1 ]; then 66 | tput bold 67 | tput setaf 1 68 | fi 69 | echo "[$TS] ERROR: $*" >&2 70 | if [ -t 1 ]; then 71 | tput sgr0 72 | fi 73 | color=0 74 | } 75 | errorlog() { log "ERROR: $*"; } 76 | status() { statusprint "$@"; statuslog "$@"; } 77 | error() { errorprint "$@"; errorlog "$@"; } 78 | debug() { 79 | if [ "$debug" = "1" ]; then 80 | color=3 81 | print "$@" 82 | log "$@" 83 | fi 84 | } 85 | 86 | isnum() { 87 | 88 | echo "$1" | grep -E '^\-?[0-9]+$' >/dev/null 2>&1 89 | return $? 90 | } 91 | 92 | loadconfig() { 93 | 94 | if [ -f "$configfile" ]; then 95 | . $configfile || { 96 | error "Failed to load configuration file \"$configfile\"." 97 | exit_safe 1 98 | } 99 | else 100 | status "Configuration file not found: \"$configfile\" - Using defaults." 101 | fi 102 | 103 | } 104 | 105 | main() { 106 | 107 | init "$@" 108 | 109 | cmdcheck 110 | inetstatus 111 | serviceswatch 112 | 113 | if [ "$sendreport" = "1" ]; then 114 | sendreport 115 | fi 116 | 117 | scriptend=$(date +%s) 118 | scripttime=$(echo "$scriptend" - "$scriptstart" | bc) 119 | status "Script finished in $(date -u -d @${scripttime} +"%T")" 120 | 121 | exit_safe 0 122 | 123 | } 124 | 125 | init() { 126 | 127 | scriptstart=$(date +%s) 128 | havelockfile=0 129 | 130 | status "Monitor Services Watch - Starting - $0" 131 | 132 | loadconfig 133 | 134 | if [ -f "$lockfile" ]; then 135 | error "Script is already running. If this is incorrect, remove: $lockfile." 136 | exit_safe 137 | fi 138 | which lockfile >/dev/null 2>&1 139 | if [ $? -eq 0 ] ; then 140 | lockfile -r 0 -l $lockfilettl $lockfile || { exit_safe 1; } 141 | else 142 | touch "$lockfile" || { exit_safe 1; } 143 | fi 144 | havelockfile=1 145 | 146 | touch "$tmpfile" || exit_safe 1 147 | touch "$logfile" || exit_safe 1 148 | 149 | } 150 | 151 | exit_safe() { 152 | 153 | debug "exit_safe()" 154 | 155 | if [ "$havelockfile" -eq 1 ]; then 156 | rm -f "$lockfile" 157 | fi 158 | 159 | exit $? 160 | } 161 | 162 | # Check that we got all the needed commands 163 | 164 | cmdcheck() { 165 | 166 | debug "cmdcheck()" 167 | 168 | cmds="which cat cut tr sed grep bc cp mv rm mkdir date hostname mail ssh tput ping mutt" 169 | for cmd in $cmds 170 | do 171 | which "$cmd" >/dev/null 2>&1 172 | if [ $? != 0 ] ; then 173 | echo "ERROR: Missing \"${cmd}\" command!" 174 | exit_safe 1 175 | fi 176 | done 177 | 178 | debug "cmdcheck() finished" 179 | 180 | } 181 | 182 | inetstatus() { 183 | 184 | debug "inetstatus()" 185 | 186 | #inetwasdown= 187 | inetwasdowntext= 188 | 189 | inetup=0 190 | for inettesthost in $inettesthosts 191 | do 192 | pingtext=$(ping -c 1 -W "${pingtimeout}" "${inettesthost}") 193 | r=$? 194 | if [ $r = 0 ] ; then 195 | inetup=1 196 | break 197 | fi 198 | done 199 | 200 | if [ "$inetup" -eq 1 ]; then 201 | status "Internet connection found to be up." 202 | entry=$(grep -i "INETDOWN Time=.*" ${tmpfile}) 203 | if ! [ "$entry" = "" ]; then 204 | #inetwasdown=1 205 | #rm -f ${tmpfile} || exit_safe 1 206 | #touch ${tmpfile} || exit_safe 1 207 | sed -i "/^INETDOWN .*$/d" ${tmpfile} 208 | timenow=$(date +%s) 209 | entrytime=$(echo "$entry" | sed -e 's/.* Time=\(.*\).*/\1/g' | cut -d' ' -f1) 210 | if isnum "$entrytime" ; then 211 | time=$(echo "$timenow" - "$entrytime" | bc) 212 | timetext=$(date -u -d @${time} +"%T") 213 | inetwasdowntext="Internet was down $timetext" 214 | status "$inetwasdowntext" 215 | else 216 | inetwasdowntext="Internet was down - Failed to calculate downtime." 217 | status "$inetwasdowntext" 218 | entrytime=0 219 | fi 220 | fi 221 | return $r 222 | else 223 | status "Internet connection was found to be down." 224 | entry=$(grep -i "INETDOWN Time=.*" ${tmpfile}) 225 | if [ "$entry" = "" ]; then 226 | timenow=$(date +%s) 227 | echo "INETDOWN Time=${timenow}" >${tmpfile} 228 | debug "Internet was found to be down - Writing to ${tmpfile}" 229 | else 230 | debug "Internet was found to be down - Existing info found in ${tmpfile}" 231 | fi 232 | exit_safe $r 233 | fi 234 | return $r 235 | 236 | debug "inetstatus() finished" 237 | 238 | } 239 | 240 | serviceswatch() { 241 | 242 | debug "serviceswatch()" 243 | 244 | #i=0 245 | IFS_DEFAULT=$IFS 246 | IFS=$'\n' 247 | for l in ${hosts} 248 | do 249 | IFS=$IFS_DEFAULT 250 | if [ "$l" = "" ]; then 251 | continue 252 | fi 253 | 254 | uhp=$(echo "$l" | awk '{ print $1}') 255 | services=$(echo "$l" | cut -d ' ' -f2-) 256 | 257 | echo "$uhp" | grep '.:.' >/dev/null 2>&1 258 | if [ $? -eq 0 ]; then 259 | userhost=$(echo "$uhp" | cut -d':' -f1) 260 | port=$(echo "$uhp" | cut -d':' -f2) 261 | hostoptions="$options -p $port" 262 | else 263 | userhost=$uhp 264 | port=22 265 | hostoptions=$options 266 | fi 267 | 268 | echo "$userhost" | grep '.@.' >/dev/null 2>&1 269 | if [ $? -eq 0 ]; then 270 | user=$(echo "$userhost" | cut -d'@' -f1) 271 | host=$(echo "$userhost" | cut -d'@' -f2) 272 | else 273 | user=root 274 | host=$userhost 275 | fi 276 | 277 | status "Host: $host User: $user - Port: $port UserHost: $userhost - Services: $services" 278 | 279 | status= 280 | fail=0 281 | failtime=0 282 | services_status= 283 | services_status_html= 284 | 285 | readfile 286 | 287 | for service in $services 288 | do 289 | ssh "$hostoptions" "$userhost" systemctl status "$service" >/dev/null 2>&1 290 | if [ $? -eq 0 ]; then # Service is up 291 | status="up" 292 | elif [ $? = 3 ]; then # Service is down 293 | status="down" 294 | fail=1 295 | if [ "$failtime" -le 1 ]; then 296 | failtime=$(date +%s) 297 | fi 298 | elif [ $? = 255 ]; then # Host is down 299 | status="down" 300 | fail=1 301 | if [ "$failtime" -le 1 ]; then 302 | failtime=$(date +%s) 303 | fi 304 | else 305 | status="down" 306 | fail=1 307 | if [ "$failtime" -le 1 ]; then 308 | failtime=$(date +%s) 309 | fi 310 | fi 311 | status "Host: $host - Service: $service - Status: $status" 312 | servicesstatus_add 313 | done 314 | 315 | servicesdata="$servicesdata 316 | Host: $host - ${services_status_html}
" 317 | 318 | writefile 319 | 320 | done 321 | 322 | debug "serviceswatch() finished" 323 | 324 | } 325 | 326 | readfile() { 327 | 328 | debug "readfile()" 329 | 330 | entryfail=0 331 | failtime=0 332 | reporttime=0 333 | entrytime=0 334 | entry_services_status= 335 | 336 | entry=$(grep -i "^${host} .*" ${tmpfile}) 337 | if [ "$entry" = "" ]; then 338 | return 339 | fi 340 | entry_services_status=$(echo "$entry" | cut -d' ' -f2) 341 | if [ "$entry_services_status" = "" ]; then 342 | entry= 343 | return 344 | fi 345 | 346 | entryfail=$(echo "$entry" | cut -d' ' -f3) 347 | if [ "$entryfail" = "" ] || ! isnum "$entryfail"; then 348 | entry= 349 | entry_services_status= 350 | entryfail=0 351 | return 352 | fi 353 | 354 | failtime=$(echo "$entry" | cut -d' ' -f4) 355 | if [ "$failtime" = "" ] || ! isnum "$failtime"; then 356 | entry= 357 | entry_services_status= 358 | entryfail=0 359 | failtime=0 360 | return 361 | fi 362 | 363 | reporttime=$(echo "$entry" | cut -d' ' -f5) 364 | if [ "$reporttime" = "" ] || ! isnum "$reporttime"; then 365 | entry= 366 | entry_services_status= 367 | entryfail=0 368 | failtime=0 369 | reporttime=0 370 | return 371 | fi 372 | 373 | entrytime=$(echo "$entry" | cut -d' ' -f6) 374 | if [ "$entrytime" = "" ] || ! isnum "$entrytime"; then 375 | entry= 376 | entry_services_status= 377 | entryfail=0 378 | entrytime=0 379 | failtime=0 380 | reporttime=0 381 | return 382 | fi 383 | 384 | timenow=$(date +%s) 385 | time=$(echo "$timenow" - "$entrytime" | bc) 386 | debug "Entry with timestamp \"${entrytime}\" ($(date -u -d @${time} +"%T") ago) found for host \"$host\". Status: $entry_services_status Fail: $entryfail FailTime: $failtime ReportTime: $reporttime" 387 | 388 | debug "readfile() finished" 389 | 390 | } 391 | 392 | servicesstatus_add() { 393 | 394 | debug "servicesstatus_add()" 395 | 396 | if [ "${services_status}" = "" ]; then 397 | services_status="$service=$status" 398 | if [ "$status" = "up" ]; then 399 | services_status_html="$service=$status" 400 | else 401 | services_status_html="$service=$status" 402 | fi 403 | else 404 | services_status="${services_status},$service=$status" 405 | if [ "$status" = "up" ]; then 406 | services_status_html="${services_status_html} $service=$status" 407 | else 408 | services_status_html="${services_status_html} $service=$status" 409 | fi 410 | fi 411 | 412 | debug "servicesstatus_add() finished" 413 | 414 | } 415 | 416 | writefile() { 417 | 418 | debug "writefile()" 419 | 420 | echo "$entry" | grep -i "^${host} ${services_status} .*" >/dev/null 2>&1 421 | if [ $? -eq 0 ]; then 422 | changed=0 423 | else 424 | changed=1 425 | fi 426 | 427 | timenow=$(date +%s) 428 | failtime_bc=$(echo "$timenow" - "$failtime" | bc) 429 | reporttime_bc=$(echo "$timenow" - "$reporttime" | bc) 430 | 431 | debug "Host: ${host} - Changed=$changed" 432 | 433 | if [ "$entry" = "" ]; then 434 | entrytime=$(date +%s) 435 | summary_add "This is the first services watch for \"$host\"." 436 | sendreport=1 437 | reporttime=$(date +%s) 438 | elif [ "$fail" -eq 1 ] && [ "$failtime_bc" -ge "$maxfailtime" ]; then 439 | entryfail=1 440 | summary_add "One or more services on \"$host\" failed for more than $(date -u -d @$maxfailtime +"%T")." 441 | if [ "$reporttime_bc" -ge "$reportfreq" ]; then 442 | sendreport=1 443 | reporttime=$(date +%s) 444 | fi 445 | elif [ "$fail" -eq 0 ] && [ "$entryfail" -eq 1 ]; then 446 | entryfail=0 447 | summary_add "\"$host\" restored from one or more failed services." 448 | failtime=0 449 | sendreport=1 450 | reporttime=$(date +%s) 451 | fi 452 | 453 | if [ "$fail" -eq 0 ]; then 454 | entryfail=0 455 | failtime=0 456 | fi 457 | 458 | if [ "$entrytime" -le 0 ]; then 459 | entrytime=$(date +%s) 460 | fi 461 | 462 | sed -i "/^${host} .*$/d" ${tmpfile} 463 | echo "${host} ${services_status} ${entryfail} ${failtime} ${reporttime} ${entrytime}" >>${tmpfile} 464 | 465 | debug "writefile() finished" 466 | 467 | } 468 | 469 | summary_add() { 470 | 471 | if [ "$summary" = "" ]; then 472 | summary="$1
" 473 | else 474 | summary="$summary 475 | $1
" 476 | fi 477 | 478 | } 479 | 480 | report_add() { 481 | 482 | if [ "$reportdata" = "" ]; then 483 | reportdata="$1" 484 | else 485 | reportdata="$reportdata 486 | $1" 487 | fi 488 | 489 | } 490 | 491 | sendreport() { 492 | 493 | debug "sendreport()" 494 | 495 | status "Sending monitor services watch report..." 496 | 497 | scriptend=$(date +%s) 498 | scripttime=$(echo "$scriptend" - "$scriptstart" | bc) 499 | reporttime=$(date +%s) 500 | 501 | report_add "" 502 | report_add "" 503 | report_add "

Monitor Services Watch

" 504 | if ! [ "$inetwasdowntext" = "" ]; then 505 | report_add "

$inetwasdowntext

" 506 | fi 507 | if ! [ "$summary" = "" ]; then 508 | report_add "

$summary

" 509 | fi 510 | report_add "Status:
" 511 | report_add "$servicesdata" 512 | report_add "
" 513 | report_add "New report is sent every $(date -u -d @${reportfreq} +"%T") if status is changed or there is an error.
" 514 | 515 | report_add "" 516 | report_add "" 517 | 518 | LANG=en_GB 519 | EMAIL=$emailfrom mutt -e 'set content_type=text/html' -s "Monitor Services Watch" $emailto <. 18 | # 19 | 20 | configfile="/etc/sysconfig/syswatch" 21 | #hostname=$(hostname -s) 22 | lockfile="/tmp/syswatch.lock" 23 | lockfilettl=3600 24 | tmpfile="/tmp/syswatch.tmp" 25 | logfile="/tmp/syswatch.log" 26 | #log=1 27 | debug=1 28 | reportfreq=180 29 | pingtimeout=2 30 | connecttimeout=2 31 | options="-o ConnectTimeout=${connecttimeout} -oBatchMode=yes" 32 | inettesthosts="8.8.8.8 8.8.4.4" 33 | emailfrom="nobody" 34 | emailto="root" 35 | 36 | maxcpu=70.00 37 | maxhdd=90 38 | maxmem=90 39 | 40 | memfailreporttime=60 41 | cpufailreporttime=60 42 | hddfailreporttime=60 43 | 44 | hosts="server1 server2 server3 server4" 45 | filesystems="/boot / /usr /opt /var /home /usr/local /tmp /srv /share/MD0_DATA /mnt/HDA_ROOT /mnt/ext" 46 | 47 | timestamp() { TS=$(date '+%d/%m-%Y %H:%M:%S'); } 48 | 49 | print() { 50 | timestamp 51 | if [ -t 1 ] && ! [ "$color" = "" ]; then 52 | tput bold 53 | tput setaf "$color" 54 | fi 55 | echo "[$TS] $*" 56 | if [ -t 1 ]; then 57 | tput sgr0 58 | fi 59 | color=0 60 | } 61 | log() { 62 | timestamp 63 | echo "[$TS] $*" >>${logfile} 64 | } 65 | statusprint() { color=8; print "$@"; } 66 | statuslog() { log "$@"; } 67 | errorprint() { 68 | timestamp 69 | if [ -t 1 ]; then 70 | tput bold 71 | tput setaf 1 72 | fi 73 | echo "[$TS] ERROR: $*" >&2 74 | if [ -t 1 ]; then 75 | tput sgr0 76 | fi 77 | color=0 78 | } 79 | errorlog() { log "ERROR: $*"; } 80 | status() { statusprint "$@"; statuslog "$@"; } 81 | error() { errorprint "$@"; errorlog "$@"; } 82 | debug() { 83 | if [ "$debug" = "1" ]; then 84 | color=3 85 | print "$@" 86 | log "$@" 87 | fi 88 | } 89 | 90 | isnum() { 91 | 92 | echo "$1" | grep -E '^\-?[0-9]+$' >/dev/null 2>&1 93 | return $? 94 | } 95 | 96 | human_print() { 97 | 98 | while read B dummy; do 99 | [ "$B" -lt 1024 ] && echo "${B}" && break 100 | KB=$(((B+512)/1024)) 101 | [ "$KB" -lt 1024 ] && echo "${KB}K" && break 102 | MB=$(((KB+512)/1024)) 103 | [ "$MB" -lt 1024 ] && echo "${MB}M" && break 104 | GB=$(((MB+512)/1024)) 105 | [ "$GB" -lt 1024 ] && echo "${GB}G" && break 106 | TB=$(((GB+512)/1024)) 107 | echo "${TB}T" 108 | break 109 | done 110 | 111 | } 112 | 113 | loadconfig() { 114 | 115 | if [ -f "$configfile" ]; then 116 | . $configfile || { 117 | error "Failed to load configuration file \"$configfile\"." 118 | exit_safe 1 119 | } 120 | else 121 | status "Configuration file not found: \"$configfile\" - Using defaults." 122 | fi 123 | 124 | } 125 | 126 | init() { 127 | 128 | scriptstart=$(date +%s) 129 | havelockfile=0 130 | 131 | status "Monitor System Watch - Starting - $0" 132 | 133 | loadconfig 134 | 135 | if [ -f "$lockfile" ]; then 136 | error "Script is already running. If this is incorrect, remove: $lockfile." 137 | exit_safe 138 | fi 139 | which lockfile >/dev/null 2>&1 140 | if [ $? -eq 0 ] ; then 141 | lockfile -r 0 -l $lockfilettl $lockfile || { exit_safe 1; } 142 | else 143 | touch $lockfile || { exit_safe 1; } 144 | fi 145 | havelockfile=1 146 | 147 | touch $tmpfile || exit_safe 1 148 | touch $logfile || exit_safe 1 149 | 150 | } 151 | 152 | main() { 153 | 154 | init "$@" 155 | 156 | cmdcheck 157 | inetstatus 158 | syswatch 159 | 160 | if [ "$sendreport" = "1" ]; then 161 | sendreport 162 | fi 163 | 164 | scriptend=$(date +%s) 165 | scripttime=$(echo "$scriptend" - "$scriptstart" | bc) 166 | status "Script finished in $(date -u -d @${scripttime} +"%T")" 167 | 168 | exit_safe 0 169 | 170 | } 171 | 172 | exit_safe() { 173 | 174 | debug "exit_safe()" 175 | 176 | if [ "$havelockfile" -eq 1 ]; then 177 | rm -f $lockfile 178 | fi 179 | 180 | exit $? 181 | 182 | } 183 | 184 | # Check that we got all the needed commands 185 | 186 | cmdcheck() { 187 | 188 | debug "cmdcheck()" 189 | 190 | cmds="test which cat cut tr sed grep bc cp mv rm mkdir date hostname ssh tput curl mutt ps head sort sar touch ping" 191 | for cmd in $cmds 192 | do 193 | which "$cmd" >/dev/null 2>&1 194 | if [ $? != 0 ] ; then 195 | error "Missing \"${cmd}\" command!" 196 | exit_safe 1 197 | fi 198 | done 199 | 200 | debug "cmdcheck() finished" 201 | 202 | } 203 | 204 | inetstatus() { 205 | 206 | debug "inetstatus()" 207 | 208 | #inetwasdown= 209 | inetwasdowntext= 210 | 211 | inetup=0 212 | for inettesthost in $inettesthosts 213 | do 214 | pingtext=$(ping -c 1 -W "${pingtimeout}" "${inettesthost}") 215 | r=$? 216 | if [ $r = 0 ] ; then 217 | inetup=1 218 | break 219 | fi 220 | done 221 | 222 | if [ "$inetup" -eq 1 ]; then 223 | status "Internet connection found to be up." 224 | entry=$(grep -i "INETDOWN Time=.*" ${tmpfile}) 225 | if ! [ "$entry" = "" ]; then 226 | #inetwasdown=1 227 | #rm -f ${tmpfile} || exit_safe 1 228 | #touch ${tmpfile} || exit_safe 1 229 | sed -i "/^INETDOWN .*$/d" ${tmpfile} 230 | timenow=$(date +%s) 231 | entrytime=$(echo "$entry" | sed -e 's/.* Time=\(.*\).*/\1/g' | cut -d' ' -f1) 232 | if isnum "$entrytime" ; then 233 | time=$(echo "$timenow" - "$entrytime" | bc) 234 | timetext=$(date -u -d @${time} +"%T") 235 | inetwasdowntext="Internet was down $timetext" 236 | status "$inetwasdowntext" 237 | else 238 | inetwasdowntext="Internet was down - Failed to calculate downtime." 239 | status "$inetwasdowntext" 240 | entrytime=0 241 | fi 242 | fi 243 | return $r 244 | else 245 | status "Internet connection was found to be down." 246 | entry=$(grep -i "INETDOWN Time=.*" ${tmpfile}) 247 | if [ "$entry" = "" ]; then 248 | timenow=$(date +%s) 249 | echo "INETDOWN Time=${timenow}" >${tmpfile} 250 | debug "Internet was found to be down - Writing to ${tmpfile}" 251 | else 252 | debug "Internet was found to be down - Existing info found in ${tmpfile}" 253 | fi 254 | exit_safe $r 255 | fi 256 | return $r 257 | 258 | debug "inetstatus() finished" 259 | 260 | } 261 | 262 | syswatch() { 263 | 264 | debug "syswatch()" 265 | 266 | cpuerrors=0 267 | memerrors=0 268 | hddhosterrors=0 269 | hdderrors=0 270 | 271 | i=0 272 | #IFS_DEFAULT=$IFS 273 | #IFS=$'\n' 274 | for l in ${hosts} 275 | do 276 | #IFS=$IFS_DEFAULT 277 | i=$(echo "$i" + 1 | bc) 278 | if [ "$l" = "" ]; then 279 | continue 280 | fi 281 | uhp=$(echo "$l" | awk '{ print $1}') 282 | 283 | echo "$uhp" | grep '.@.' >/dev/null 2>&1 284 | if [ $? -eq 0 ]; then 285 | user=$(echo "$uhp" | cut -d'@' -f1) 286 | hostport=$(echo "$uhp" | cut -d'@' -f2) 287 | else 288 | user= 289 | hostport=$uhp 290 | fi 291 | 292 | echo "$hostport" | grep '.:.' >/dev/null 2>&1 293 | if [ $? -eq 0 ]; then 294 | host=$(echo "$hostport" | cut -d':' -f1) 295 | port=$(echo "$hostport" | cut -d':' -f2) 296 | hostoptions="$options -p $port" 297 | else 298 | host=$hostport 299 | port=22 300 | hostoptions=$options 301 | fi 302 | if [ "$user" = "" ]; then 303 | hostuh="${host}" 304 | else 305 | hostuh="${user}@${host}" 306 | fi 307 | 308 | debug "Starting syswatch for \"$host\"." 309 | 310 | hostfail=0 311 | hostsummary=0 312 | havefree=0 313 | havesar=0 314 | havedf=0 315 | sshfail=0 316 | sshresult= 317 | sshtext= 318 | report=0 319 | #uname= 320 | 321 | hostchkcmds 322 | 323 | sshresult=$(ssh "$hostoptions" "$hostuh" uname -a 2>&1) 324 | if ! [ $? -eq 0 ]; then 325 | sshfail 326 | fi 327 | 328 | if [ "$sshfail" = "1" ]; then 329 | debug "SSH command failure for \"$host\": ${sshtext}" 330 | fi 331 | 332 | readfile 333 | cpuload 334 | memusage 335 | hddusage 336 | writefile 337 | 338 | if [ "$report" -eq 1 ]; then 339 | sendreport=1 340 | fi 341 | 342 | status "Status: $host - CPU: ${cpuload}% Mem: ${memusage}% HDD: $hddusage" 343 | 344 | done 345 | 346 | debug "syswatch() finished" 347 | 348 | } 349 | 350 | sshfail() { 351 | 352 | sshfail=1 353 | 354 | if [ "$sshresult" = "" ]; then 355 | sshresult="SSH connection failure." 356 | fi 357 | if [ "$sshtext" = "" ]; then 358 | sshtext=$sshresult 359 | fi 360 | 361 | } 362 | 363 | hostchkcmds() { 364 | 365 | debug "hostchkcmds()" 366 | 367 | havefree=0 368 | havedf=0 369 | havesar=0 370 | 371 | sshresult=$(ssh "$hostoptions" "$hostuh" echo '$PATH' | sed 's/:/ /g') 372 | if [ $? -eq 0 ] ; then 373 | paths="$sshresult /opt/bin" 374 | else 375 | sshfail 376 | return 1 377 | fi 378 | 379 | for cmd in sar free df 380 | do 381 | 382 | CMD=$(echo "$cmd" | tr /a-z/ /A-Z/) 383 | found=0 384 | 385 | for path in $paths 386 | do 387 | cmdpath=$path/$cmd 388 | 389 | ssh "$hostoptions" "$hostuh" test -x "$cmdpath" >/dev/null 2>&1 390 | if ! [ $? -eq 0 ] ; then 391 | continue 392 | fi 393 | 394 | if [ "$cmd" = "free" ] && ! [ "$havefree" -eq 1 ]; then 395 | sshresult=$(ssh "$hostoptions" "$hostuh" "$cmdpath" --version 2>&1 | head -n1) 396 | if [ $? -eq 0 ] ; then 397 | echo "$sshresult" | grep "^free from procps.*$" >/dev/null 2>&1 398 | if [ $? -eq 0 ] ; then 399 | eval "${CMD}"="$cmdpath" 400 | found=1 401 | havefree=1 402 | break 403 | fi 404 | fi 405 | elif [ "$cmd" = "df" ] && ! [ "$havedf" -eq 1 ]; then 406 | #sshresult=$(ssh "$hostoptions" "$hostuh" "$cmdpath" --version 2>&1 | head -n1) 407 | #if [ $? -eq 0 ] ; then 408 | #echo "$sshresult" | grep "^df (GNU coreutils) .*$" >/dev/null 2>&1 409 | #if [ $? -eq 0 ] ; then 410 | found=1 411 | eval "${CMD}"="$cmdpath" 412 | havedf=1 413 | break 414 | #fi 415 | #fi 416 | elif [ "$cmd" = "sar" ] && ! [ "$havesar" -eq 1 ]; then 417 | found=1 418 | eval "${CMD}"="$cmdpath" 419 | havesar=1 420 | break 421 | fi 422 | done 423 | if ! [ "$found" -eq 1 ]; then 424 | error "Host \"$host\" Missing correct version of \"${cmd}\" command!" 425 | fi 426 | 427 | done 428 | 429 | debug "hostchkcmds() finished" 430 | 431 | } 432 | 433 | # CPU LOAD 434 | 435 | cpuload() { 436 | 437 | debug "cpuload()" 438 | 439 | #cpufail=0 440 | cpustatus= 441 | cpustatus_check= 442 | cpuload_html= 443 | 444 | if [ "$sshfail" = "1" ]; then 445 | text="SSH command failure for \"$host\": ${sshtext}" 446 | summary_add "$text" 447 | status "$text" 448 | cpuload_fail "FAIL" 449 | cpuload_html="CPU: ${sshtext}" 450 | return 451 | fi 452 | if ! [ "$havesar" -eq 1 ]; then 453 | text="Missing sar command for \"$host\"." 454 | summary_add "$text" 455 | status "$text" 456 | cpuload_fail "FAIL" 457 | cpuload_html="CPU: Missing SAR command!" 458 | return 459 | fi 460 | 461 | sshresult=$(ssh "$hostoptions" "$hostuh" "$SAR" -P ALL 1 2 2>&1) 462 | if ! [ $? -eq 0 ]; then 463 | text="SSH command failure for \"$host\": ${sshresult}" 464 | summary_add "$text" 465 | status "$text" 466 | cpuload_fail "FAIL" 467 | cpuload_html="CPU: ${sshresult}" 468 | return 469 | fi 470 | cpuload=$(echo "$sshresult" | grep 'Average.*all' | awk -F" " '{print 100.0 -$NF}') 471 | if [ 1 -eq "$(echo "${cpuload} >= ${maxcpu}" | bc)" ]; then 472 | text="CPU threshold (${maxcpu}) exceeded for host \"$host\", CPU load is ${cpuload}." 473 | summary_add "$text" 474 | status "$text" 475 | cpuload_fail "ERROR" 476 | cpuload_html="CPU: ${cpuload}%" 477 | else 478 | cpufail=0 479 | cpustatus="OK" 480 | cpustatus_check="OK" 481 | cpufailtime=0 482 | cpuload_html="CPU: ${cpuload}%" 483 | debug "CPU load for host \"$host\" is within threshold." 484 | if [ "$cpuprevstatus" = "ERROR" ]; then 485 | text="Host \"$host\" restored from high CPU usage." 486 | debug "$text" 487 | summary_add "$text" 488 | report=1 489 | elif [ "$cpuprevstatus" = "FAIL" ]; then 490 | text="Host \"$host\" restored from CPU status failure." 491 | debug "$text" 492 | summary_add "$text" 493 | report=1 494 | fi 495 | fi 496 | 497 | debug "cpuload() finished" 498 | 499 | } 500 | 501 | cpuload_fail() { 502 | 503 | #cpufail=1 504 | if [ "$1" = "" ]; then 505 | cpustatus="ERROR" 506 | else 507 | cpustatus=$1 508 | fi 509 | cpustatus_check="OK" 510 | 511 | if [ "$cpufailtime" -le 0 ]; then 512 | timenow=$(date +%s) 513 | cpufailtime=$timenow 514 | debug "Zero CPU failure time for host \"$host\", setting CPU failure time to NOW ($timenow)." 515 | fi 516 | timenow=$(date +%s) 517 | time=$(echo "$timenow" - "$cpufailtime" | bc) 518 | if [ "$time" -ge "$cpufailreporttime" ]; then 519 | hostfail=1 520 | cpustatus_check=$cpustatus 521 | debug "CPU failure for host \"$host\" longer than CPU fail report time $(date -u -d @$cpufailreporttime +"%T"), CPU fail time is $(date -u -d @$time +"%T")." 522 | fi 523 | 524 | cpuerrors=$(echo "$cpuerrors" + 1 | bc) 525 | 526 | } 527 | 528 | # MEM USAGE 529 | 530 | memusage() { 531 | 532 | debug "memusage()" 533 | 534 | memstatus= 535 | memstatus_check= 536 | memusage=0 537 | memtotal=0 538 | #memfail=0 539 | used=0 540 | free=0 541 | total=0 542 | 543 | if [ "$sshfail" -eq 1 ]; then 544 | text="SSH command failure for \"$host\": ${sshtext}" 545 | summary_add "$text" 546 | status "$text" 547 | memusage_fail "FAIL" 548 | memusage_html="Mem: ${sshtext}" 549 | return 550 | fi 551 | if ! [ "$havefree" -eq 1 ]; then 552 | text="Missing correct free command for \"$host\"." 553 | summary_add "$text" 554 | status "$text" 555 | memusage_fail "FAIL" 556 | memusage_html="Mem: Missing FREE command!" 557 | return 558 | fi 559 | 560 | sshresult=$(ssh "$hostoptions" "$hostuh" "$FREE" -b 2>&1) 561 | if ! [ $? -eq 0 ]; then 562 | text="SSH command failure for \"$host\": ${sshresult}" 563 | summary_add "$text" 564 | status "$text" 565 | memusage_fail "FAIL" 566 | memusage_html="Mem: ${sshresult}" 567 | return 568 | fi 569 | #echo "$sshresult" >/tmp/debug-df-$host 570 | 571 | count=0 572 | IFS_DEFAULT=$IFS 573 | IFS=$'\n' 574 | for l in ${sshresult} 575 | do 576 | count=$(echo "$count" +1 | bc) 577 | IFS=$IFS_DEFAULT 578 | if [ "$l" = "" ]; then 579 | continue 580 | fi 581 | if [ "$count" -eq 1 ]; then 582 | column1=$(echo "$l" | awk '{print $1}') 583 | #column2=$(echo "$l" | awk '{print $2}') 584 | #column3=$(echo "$l" | awk '{print $3}') 585 | #column4=$(echo "$l" | awk '{print $4}') 586 | #column5=$(echo "$l" | awk '{print $5}') 587 | column6=$(echo "$l" | awk '{print $6}') 588 | continue 589 | fi 590 | 591 | row=$(echo "$l" | awk '{print $1}') 592 | param1=$(echo "$l" | awk '{print $2}') 593 | param2=$(echo "$l" | awk '{print $3}') 594 | param3=$(echo "$l" | awk '{print $4}') 595 | #param4=$(echo "$l" | awk '{print $5}') 596 | #param5=$(echo "$l" | awk '{print $6}') 597 | param6=$(echo "$l" | awk '{print $7}') 598 | 599 | if [ "$row" = "Mem:" ]; then 600 | if [ "$column1" = "total" ]; then 601 | total=$param1 602 | fi 603 | if [ "$column6" = "available" ]; then 604 | free=$param6 605 | if isnum "$total" && isnum "$free"; then 606 | used=$(echo "$total" - "$free" | bc) 607 | fi 608 | fi 609 | elif [ "$row" = "-/+" ] && [ "$param1" = "buffers/cache:" ] ; then 610 | used=$param2 611 | free=$param3 612 | else 613 | continue 614 | fi 615 | done 616 | 617 | if ! isnum "$used" || ! isnum "$free" || ! isnum "$total"; then 618 | text="Failed to calculate memory usage on \"$host\"." 619 | summary_add "$text" 620 | status "$text" 621 | memusage_fail "FAIL" 622 | memusage_html="Mem: Failed to calculate memory usage." 623 | return 624 | fi 625 | 626 | memusage=$(echo "${used}" * 100 / "${total}" | bc) 627 | memtotal=$(echo "$total" | human_print) 628 | if [ "$memusage" -ge "$maxmem" ]; then 629 | text="Memory usage threshold (${maxmem}) exceeded for host \"$host\", memory usage is ${memusage}." 630 | summary_add "$text" 631 | status "$text" 632 | memusage_fail "ERROR" 633 | memusage_html="Mem: ${memusage}% / ${memtotal}" 634 | else 635 | memstatus="OK" 636 | memstatus_check="OK" 637 | memfailtime=0 638 | memusage_html="Mem: ${memusage}% / ${memtotal}" 639 | debug "Memory usage for host \"$host\" is within threshold." 640 | if [ "$memprevstatus" = "FAIL" ]; then 641 | text="Host \"$host\" restored from memory status failure." 642 | debug "$text" 643 | summary_add "$text" 644 | report=1 645 | elif [ "$memprevstatus" = "ERROR" ]; then 646 | text="Host \"$host\" restored from high memory usage." 647 | debug "$text" 648 | summary_add "$text" 649 | report=1 650 | fi 651 | fi 652 | 653 | debug "memusage() finished" 654 | 655 | } 656 | 657 | memusage_fail() { 658 | 659 | #memfail=1 660 | if [ "$1" = "" ]; then 661 | memstatus="ERROR" 662 | else 663 | memstatus=$1 664 | fi 665 | memstatus_check="OK" 666 | 667 | if [ "$memfailtime" -le 0 ]; then 668 | timenow=$(date +%s) 669 | memfailtime=$timenow 670 | debug "Zero memory failure time for host \"$host\", setting memory failure time to NOW ($timenow)." 671 | fi 672 | timenow=$(date +%s) 673 | time=$(echo "$timenow" - "$memfailtime" | bc) 674 | if [ "$time" -ge "$memfailreporttime" ]; then 675 | hostfail=1 676 | memstatus_check=$memstatus 677 | debug "Memory failure for host \"$host\" longer than memory failure report time $(date -u -d @$memfailreporttime +"%T"), memory failure time is $(date -u -d @$time +"%T")." 678 | fi 679 | 680 | memerrors=$(echo "$memerrors" + 1 | bc) 681 | 682 | } 683 | 684 | hddusage() { 685 | 686 | debug "hddusage()" 687 | 688 | hddfail=0 689 | hddstatus="OK" 690 | hddstatus_check="OK" 691 | hddstatus_line= 692 | hddusage_html= 693 | hddusage= 694 | hddcount=0 695 | 696 | if [ "$sshfail" -eq 1 ]; then 697 | text="SSH command failure for \"$host\": ${sshtext}" 698 | summary_add "$text" 699 | status "$text" 700 | hddusage_fail "FAIL" 701 | hddusage_html="HDD: ${sshtext}
" 702 | return 703 | fi 704 | if ! [ "$havedf" -eq 1 ]; then 705 | text="Missing correct df command for \"$host\"." 706 | summary_add "$text" 707 | status "$text" 708 | hddusage_fail "FAIL" 709 | hddusage_html="HDD: Missing DF command!
" 710 | return 711 | fi 712 | 713 | sshresult=$(ssh "$hostoptions" "$hostuh" "$DF" -h 2>&1) 714 | if ! [ $? -eq 0 ]; then 715 | # Ignore DF command error, sometimes it returns error messages even if there are valid results. 716 | text="DF command failure for \"$host\": ${sshresult}" 717 | #summary_add "$text" 718 | #status "$text" 719 | #hddusage_fail "FAIL" 720 | #hddusage_html="HDD: ${sshresult}
" 721 | #return 722 | fi 723 | 724 | IFS_DEFAULT=$IFS 725 | IFS=$'\n' 726 | for l in ${sshresult} 727 | do 728 | IFS=$IFS_DEFAULT 729 | hddcount=$(echo "$hddcount" +1 | bc) 730 | #if [ "$hddcount" -eq 1 ]; then 731 | #continue 732 | #fi 733 | if [ "$l" = "" ]; then 734 | continue 735 | fi 736 | #device=$(echo "$l" | awk '{print $1}') 737 | total=$(echo "$l" | awk '{print $2}') 738 | used=$(echo "$l" | awk '{print $3}') 739 | #avail=$(echo "$l" | awk '{print $4}') 740 | use=$(echo "$l" | awk '{print $5}' | sed 's/%//g') 741 | # | tr -d % 742 | filesystem=$(echo "$l" | awk '{print $6}') 743 | 744 | if ! isnum "$use"; then 745 | continue 746 | fi 747 | 748 | found=0 749 | for x in ${filesystems} 750 | do 751 | if [ "${x}" = "${filesystem}" ]; then 752 | found=1 753 | break 754 | fi 755 | done 756 | if [ "$found" -eq 0 ]; then 757 | continue 758 | fi 759 | 760 | if [ "$use" -ge "$maxhdd" ]; then 761 | text="HDD usage threshold (${maxhdd}) exceeded for host \"$host\" on partition \"${filesystem}\", HDD usage is ${use}%." 762 | summary_add "$text" 763 | status "$text" 764 | hddusage_fail "ERROR" 765 | else 766 | debug "HDD usage for host \"$host\" partition \"${filesystem}\" is within threshold." 767 | fi 768 | 769 | if [ "$hddusage" = "" ]; then 770 | hddusage="$filesystem=$use% ($used / $total)" 771 | if [ "$use" -ge "$maxhdd" ]; then 772 | hddstatus_line="$filesystem=ERROR" 773 | hddusage_html="HDD:
$filesystem $use% ($used / $total)
" 774 | else 775 | hddstatus_line="$filesystem=OK" 776 | hddusage_html="HDD:
$filesystem $use% ($used / $total)
" 777 | fi 778 | else 779 | hddusage="$hddusage $filesystem=$use% ($used / $total)" 780 | if [ "$use" -ge "$maxhdd" ]; then 781 | hddstatus_line="$hddstatus_line,$filesystem=ERROR" 782 | hddusage_html="$hddusage_html $filesystem $use% ($used / $total)
" 783 | else 784 | hddstatus_line="$hddstatus_line,$filesystem=OK" 785 | hddusage_html="$hddusage_html $filesystem $use% ($used / $total)
" 786 | fi 787 | fi 788 | 789 | done 790 | 791 | if [ "$hddfail" -ne 1 ]; then 792 | hddfailtime=0 793 | debug "HDD usage for all partitions on host \"$host\" are within threshold." 794 | if [ "$hddprevstatus" = "FAIL" ]; then 795 | text="Host \"$host\" restored from HDD status failure." 796 | summary_add "$text" 797 | status "$text" 798 | report=1 799 | elif [ "$hddprevstatus" = "ERROR" ]; then 800 | text="Host \"$host\" restored from high HDD usage." 801 | summary_add "$text" 802 | status "$text" 803 | report=1 804 | fi 805 | fi 806 | 807 | debug "hddusage() finished" 808 | 809 | } 810 | 811 | hddusage_fail() { 812 | 813 | hddfail=1 814 | 815 | if [ "$1" = "" ]; then 816 | hddstatus="ERROR" 817 | else 818 | hddstatus=$1 819 | fi 820 | hddstatus_check="OK" 821 | 822 | if [ "$hddfailtime" -le 0 ]; then 823 | timenow=$(date +%s) 824 | hddfailtime=$timenow 825 | debug "Zero HDD failure time for host \"$host\", setting HDD failure time to NOW ($timenow)." 826 | fi 827 | timenow=$(date +%s) 828 | time=$(echo "$timenow" - "$hddfailtime" | bc) 829 | if [ "$time" -ge "$hddfailreporttime" ]; then 830 | debug "HDD failure for host \"$host\" partition \"${filesystem}\" longer than HDD failure report time $(date -u -d @$hddfailreporttime +"%T"), HDD failure time is $(date -u -d @$time +"%T")." 831 | hddstatus_check=$hddstatus 832 | hostfail=1 833 | fi 834 | 835 | hdderrors=$(echo "$hdderrors" + 1 | bc) 836 | if [ "$hddfail" -ne 1 ]; then 837 | hddhosterrors=$(echo $hddhosterrors + 1 | bc) 838 | fi 839 | 840 | } 841 | 842 | readfile() { 843 | 844 | debug "readfile()" 845 | 846 | cpuprevstatus="Unknown" 847 | memprevstatus="Unknown" 848 | hddprevstatus="Unknown" 849 | cpufailtime=0 850 | memfailtime=0 851 | hddfailtime=0 852 | reporttime=0 853 | entrytime=0 854 | #changed=0 855 | 856 | entry=$(grep -i "^${host} .*" ${tmpfile}) 857 | if [ "$entry" = "" ]; then 858 | debug "No entry found for \"$host\"." 859 | summary_add "This is the first system watch for \"$host\"." 860 | report=1 861 | return 862 | fi 863 | 864 | for token in $entry 865 | do 866 | 867 | if [ "$token" = "$host" ]; then 868 | continue 869 | fi 870 | 871 | var=$(echo "$token" | cut -d'=' -f1) 872 | data=$(echo "$token" | cut -d'=' -f2) 873 | 874 | if [ "$var" = "" ] || [ "$data" = "" ]; then 875 | debug "Invalid entry \"$token\" in entry file." 876 | continue 877 | fi 878 | 879 | if [ "$var" = "CPU" ]; then 880 | cpuprevstatus=$data 881 | continue 882 | elif [ "$var" = "Mem" ]; then 883 | memprevstatus=$data 884 | continue 885 | elif [ "$var" = "HDD" ]; then 886 | hddprevstatus=$data 887 | continue 888 | elif [ "$var" = "CPUFailTime" ] && isnum "$data"; then 889 | cpufailtime=$data 890 | continue 891 | elif [ "$var" = "MemFailTime" ] && isnum "$data"; then 892 | memfailtime=$data 893 | continue 894 | elif [ "$var" = "HDDFailTime" ] && isnum "$data"; then 895 | hddfailtime=$data 896 | continue 897 | elif [ "$var" = "ReportTime" ] && isnum "$data"; then 898 | reporttime=$data 899 | continue 900 | elif [ "$var" = "Time" ] && isnum "$data"; then 901 | entrytime=$data 902 | continue 903 | fi 904 | 905 | done 906 | 907 | timenow=$(date +%s) 908 | time=$(echo "$timenow" - "$entrytime" | bc) 909 | debug "Entry with timestamp \"${entrytime}\" ($(date -u -d @${time} +"%T") ago) found for host \"$host\". CPUPrevStatus: $cpuprevstatus CPUFailTime: $cpufailtime MemPrevStatus: $memprevstatus MemFailTime: $memfailtime HDDPrevStatus: $hddprevstatus HDDFailTime: $hddfailtime ReportTime: $reporttime." 910 | 911 | debug "readfile() finished" 912 | 913 | } 914 | 915 | writefile() { 916 | 917 | debug "writefile()" 918 | 919 | # The changed state is not in use anymore, keep it here for now. 920 | echo "$entry" | grep -i "^${host} CPU=${cpustatus_check} CPUFailTime=.* Mem=${memstatus_check} MemFailTime=.* HDD=${hddstatus_check} .* HDDFailTime=.* ReportTime=.* Time=.*" >/dev/null 2>&1 921 | #if [ $? -eq 0 ]; then 922 | # changed=0 923 | #else 924 | # changed=1 925 | #fi 926 | 927 | timenow=$(date +%s) 928 | reporttime_bc=$(echo "$timenow" - "$reporttime" | bc) 929 | 930 | if [ "$entry" = "" ]; then 931 | entrytime=$(date +%s) 932 | report=1 933 | sendreport=1 934 | reporttime=$(date +%s) 935 | elif [ "$hostfail" -eq 1 ] && [ "$reporttime_bc" -ge "$reportfreq" ]; then 936 | report=1 937 | sendreport=1 938 | reporttime=$(date +%s) 939 | fi 940 | 941 | if [ "$entrytime" -le 0 ]; then 942 | entrytime=$(date +%s) 943 | fi 944 | 945 | sed -i "/^${host} .*$/d" ${tmpfile} 946 | timenow=$(date +%s) 947 | echo "${host} CPU=${cpustatus_check} CPUFailTime=${cpufailtime} Mem=${memstatus_check} MemFailTime=${memfailtime} HDD=${hddstatus_check} ${hddstatus_line} HDDFailTime=${hddfailtime} ReportTime=${reporttime} Time=${entrytime}" >>${tmpfile} 948 | 949 | syswatch="$syswatch 950 | Host: ${host}
951 | ${cpuload_html}
952 | ${memusage_html}
953 | ${hddusage_html}
954 | " 955 | 956 | debug "writefile() finished" 957 | 958 | } 959 | 960 | summary_add() { 961 | 962 | if [ "$hostsummary" -eq 1 ]; then 963 | return 1 964 | fi 965 | hostsummary=1 966 | 967 | if [ "$summary" = "" ]; then 968 | summary="$1
" 969 | else 970 | summary="$summary 971 | $1
" 972 | fi 973 | 974 | } 975 | 976 | report_add() { 977 | 978 | if [ "$reportdata" = "" ]; then 979 | reportdata="$1" 980 | else 981 | reportdata="$reportdata 982 | $1" 983 | fi 984 | 985 | } 986 | 987 | sendreport() { 988 | 989 | debug "sendreport()" 990 | 991 | status "Sending monitor system watch report..." 992 | 993 | scriptend=$(date +%s) 994 | scripttime=$(echo "$scriptend" - "$scriptstart" | bc) 995 | reporttime=$(date +%s) 996 | 997 | report_add "" 998 | report_add "" 999 | report_add "

Monitor System Watch

" 1000 | if ! [ "$inetwasdowntext" = "" ]; then 1001 | report_add "

$inetwasdowntext

" 1002 | fi 1003 | if ! [ "$summary" = "" ]; then 1004 | report_add "

" 1005 | report_add "

Summary:

" 1006 | report_add "$summary" 1007 | report_add "

" 1008 | fi 1009 | 1010 | report_add "

" 1011 | report_add "

Status:

" 1012 | report_add "$syswatch" 1013 | report_add "

" 1014 | report_add "

" 1015 | report_add "Script finished in $(date -u -d @${scripttime} +"%T")
" 1016 | report_add "New report is sent if status has changed or every $(date -u -d @${reportfreq} +"%T") if there is an error.
" 1017 | report_add "

" 1018 | report_add "" 1019 | report_add "" 1020 | 1021 | LANG=en_GB 1022 | EMAIL=$emailfrom mutt -e 'set content_type=text/html' -s "Monitor System Watch" $emailto <. 18 | # 19 | 20 | configfile="/etc/sysconfig/webwatch" 21 | lockfile="/tmp/webwatch.lock" 22 | lockfilettl=3600 23 | tmpfile="/tmp/webwatch.tmp" 24 | logfile="/tmp/webwatch.log" 25 | #log=1 26 | debug=1 27 | inettesthosts="8.8.8.8 8.8.4.4" # Test all of these, only if 1 fails internet is reported to be down. 28 | pingtimeout=2 29 | maxfailtime=1 30 | reportfreq=120 31 | emailfrom="nobody" 32 | emailto="root" 33 | 34 | websites=" 35 | http://www.jkvinge.net/
Apache Server at www.jkvinge.net Port 80
36 | https://secure.jkvinge.net/ secure.jkvinge.net 37 | https://mail.jkvinge.net/src/login.php
38 | http://teamviewer.jkvinge.net/

TeamViewer Remote Assistance

39 | http://files.jkvinge.net/
Apache Server at files.jkvinge.net Port 80
40 | " 41 | 42 | timestamp() { TS=$(date '+%d/%m-%Y %H:%M:%S'); } 43 | 44 | print() { 45 | timestamp 46 | if [ -t 1 ] && ! [ "$color" = "" ]; then 47 | tput bold 48 | tput setaf "$color" 49 | fi 50 | echo "[$TS] $*" 51 | if [ -t 1 ]; then 52 | tput sgr0 53 | fi 54 | color=0 55 | } 56 | log() { 57 | timestamp 58 | echo "[$TS] $*" >>${logfile} 59 | } 60 | statusprint() { color=8; print "$@"; } 61 | statuslog() { log "$@"; } 62 | errorprint() { 63 | timestamp 64 | if [ -t 1 ]; then 65 | tput bold 66 | tput setaf 1 67 | fi 68 | echo "[$TS] ERROR: $*" >&2 69 | if [ -t 1 ]; then 70 | tput sgr0 71 | fi 72 | color=0 73 | } 74 | errorlog() { log "ERROR: $*"; } 75 | status() { statusprint "$@"; statuslog "$@"; } 76 | error() { errorprint "$@"; errorlog "$@"; } 77 | debug() { 78 | if [ "$debug" = "1" ]; then 79 | color=3 80 | print "$@" 81 | log "$@" 82 | fi 83 | } 84 | 85 | isnum() { 86 | echo "$1" | grep -E '^\-?[0-9]+$' >/dev/null 2>&1 87 | return $? 88 | } 89 | 90 | loadconfig() { 91 | 92 | if [ -f "$configfile" ]; then 93 | . $configfile || { 94 | error "Failed to load configuration file \"$configfile\"." 95 | exit_safe 1 96 | } 97 | else 98 | status "Configuration file not found: \"$configfile\" - Using defaults." 99 | fi 100 | 101 | } 102 | 103 | main() { 104 | 105 | init "$@" 106 | 107 | cmdcheck 108 | inetstatus 109 | webwatch 110 | 111 | if [ "$sendreport" = "1" ]; then 112 | sendreport 113 | fi 114 | 115 | scriptend=$(date +%s) 116 | scripttime=$(echo "$scriptend" - "$scriptstart" | bc) 117 | status "Script finished in $(date -u -d @${scripttime} +"%T")" 118 | 119 | exit_safe 0 120 | 121 | } 122 | 123 | init() { 124 | 125 | scriptstart=$(date +%s) 126 | havelockfile=0 127 | 128 | status "Monitor Web Watch - Starting - $0" 129 | 130 | loadconfig 131 | 132 | if [ -f "$lockfile" ]; then 133 | error "Script is already running. If this is incorrect, remove: $lockfile." 134 | exit_safe 135 | fi 136 | which lockfile >/dev/null 2>&1 137 | if [ $? -eq 0 ] ; then 138 | lockfile -r 0 -l $lockfilettl $lockfile || { exit_safe 1; } 139 | else 140 | touch $lockfile || { exit_safe 1; } 141 | fi 142 | havelockfile=1 143 | 144 | touch $tmpfile || exit_safe 1 145 | touch $logfile || exit_safe 1 146 | 147 | } 148 | 149 | exit_safe() { 150 | 151 | debug "exit_safe()" 152 | 153 | if [ "$havelockfile" -eq 1 ]; then 154 | rm -f $lockfile 155 | fi 156 | 157 | exit $? 158 | } 159 | 160 | # Check that we got all the needed commands 161 | 162 | cmdcheck() { 163 | 164 | debug "cmdcheck()" 165 | 166 | cmds="which cat cut tr sed grep bc cp mv rm mkdir date hostname mail ssh tput ping curl mutt" 167 | for cmd in $cmds 168 | do 169 | which "$cmd" >/dev/null 2>&1 170 | if [ $? != 0 ] ; then 171 | echo "ERROR: Missing \"${cmd}\" command!" 172 | exit_safe 1 173 | fi 174 | done 175 | 176 | debug "cmdcheck() finished" 177 | 178 | } 179 | 180 | inetstatus() { 181 | 182 | debug "inetstatus()" 183 | 184 | #inetwasdown= 185 | inetwasdowntext= 186 | 187 | inetup=0 188 | for inettesthost in $inettesthosts 189 | do 190 | pingtext=$(ping -c 1 -W ${pingtimeout} ${inettesthost}) 191 | r=$? 192 | if [ $r = 0 ] ; then 193 | inetup=1 194 | break 195 | fi 196 | done 197 | 198 | if [ "$inetup" -eq 1 ]; then 199 | status "Internet connection found to be up." 200 | entry=$(grep -i "INETDOWN Time=.*" ${tmpfile}) 201 | if ! [ "$entry" = "" ]; then 202 | #inetwasdown=1 203 | #rm -f ${tmpfile} || exit_safe 1 204 | #touch ${tmpfile} || exit_safe 1 205 | sed -i "/^INETDOWN .*$/d" ${tmpfile} 206 | timenow=$(date +%s) 207 | entrytime=$(echo "$entry" | sed -e 's/.* Time=\(.*\).*/\1/g' | cut -d' ' -f1) 208 | if isnum "$entrytime" ; then 209 | time=$(echo "$timenow" - "$entrytime" | bc) 210 | timetext=$(date -u -d @${time} +"%T") 211 | inetwasdowntext="Internet was down $timetext" 212 | status "$inetwasdowntext" 213 | else 214 | inetwasdowntext="Internet was down - Failed to calculate downtime." 215 | status "$inetwasdowntext" 216 | entrytime=0 217 | fi 218 | fi 219 | return $r 220 | else 221 | status "Internet connection was found to be down." 222 | entry=$(grep -i "INETDOWN Time=.*" ${tmpfile}) 223 | if [ "$entry" = "" ]; then 224 | timenow=$(date +%s) 225 | echo "INETDOWN Time=${timenow}" >${tmpfile} 226 | debug "Internet was found to be down - Writing to ${tmpfile}" 227 | else 228 | debug "Internet was found to be down - Existing info found in ${tmpfile}" 229 | fi 230 | exit_safe $r 231 | fi 232 | return $r 233 | 234 | debug "inetstatus() finished" 235 | 236 | } 237 | 238 | webwatch() { 239 | 240 | debug "webwatch()" 241 | 242 | IFS_DEFAULT=$IFS 243 | IFS=$'\n' 244 | for l in ${websites} 245 | do 246 | IFS=$IFS_DEFAULT 247 | if [ "$l" = "" ]; then 248 | continue 249 | fi 250 | url=$(echo "$l" | awk '{print $1}') 251 | if [ "$url" = "" ]; then 252 | continue 253 | fi 254 | url2=$(echo "$url" | sed 's/\//\\\//g') 255 | match=$(echo "$l" | cut -d ' ' -f2-) 256 | if [ "$match" = "" ]; then 257 | continue 258 | fi 259 | readfile 260 | status= 261 | result= 262 | fail=0 263 | output=$(curl -sSf --insecure "$url" 2>&1) 264 | if ! [ $? -eq 0 ]; then 265 | exitstatus=$? 266 | status="down" 267 | result=$(echo "$output" | sed 's/curl: (.*) //g') 268 | result_html="${result}
${exitstatus}
" 269 | fail=1 270 | if [ "$failtime" -le 1 ]; then 271 | failtime=$(date +%s) 272 | fi 273 | else 274 | curl -sSf --insecure "$url" | grep -i "$match" >/dev/null 2>&1 275 | if [ $? -eq 0 ]; then 276 | status="up" 277 | result="Up and running" 278 | result_html="Up and running" 279 | fail=0 280 | else 281 | status="down" 282 | result="Wrong content" 283 | result_html="Wrong content" 284 | fail=1 285 | if [ "$failtime" -le 1 ]; then 286 | failtime=$(date +%s) 287 | fi 288 | fi 289 | fi 290 | 291 | status "Website: $url Status: $status Result: $result" 292 | 293 | writefile 294 | 295 | maildata="$maildata 296 | Website: $url - ${result_html}
" 297 | 298 | done 299 | 300 | debug "webwatch() finished" 301 | 302 | } 303 | 304 | readfile() { 305 | 306 | debug "readfile()" 307 | 308 | entryfail=0 309 | failtime=0 310 | reporttime=0 311 | entrytime=0 312 | entrystatus= 313 | entryresult= 314 | 315 | entry=$(grep -i "^${url} .*" ${tmpfile}) 316 | if [ "$entry" = "" ]; then 317 | return 318 | fi 319 | entrystatus=$(echo "$entry" | cut -d' ' -f2) 320 | if [ "$entrystatus" = "" ]; then 321 | entry= 322 | return 323 | fi 324 | 325 | entryresult=$(echo "$entry" | cut -d' ' -f3) 326 | if [ "$entryresult" = "" ]; then 327 | entry= 328 | entrystatus= 329 | return 330 | fi 331 | 332 | entryfail=$(echo "$entry" | cut -d' ' -f4) 333 | if [ "$entryfail" = "" ] || ! isnum "$entryfail"; then 334 | entry= 335 | entrystatus= 336 | entryresult= 337 | entryfail=0 338 | return 339 | fi 340 | 341 | failtime=$(echo "$entry" | cut -d' ' -f5) 342 | if [ "$failtime" = "" ] || ! isnum "$failtime"; then 343 | entry= 344 | entrystatus= 345 | entryresult= 346 | entryfail=0 347 | failtime=0 348 | return 349 | fi 350 | 351 | reporttime=$(echo "$entry" | cut -d' ' -f6) 352 | if [ "$reporttime" = "" ] || ! isnum "$reporttime"; then 353 | entry= 354 | entrystatus= 355 | entryresult= 356 | entryfail=0 357 | failtime=0 358 | reporttime=0 359 | return 360 | fi 361 | 362 | entrytime=$(echo "$entry" | cut -d' ' -f7) 363 | if [ "$entrytime" = "" ] || ! isnum "$entrytime"; then 364 | entry= 365 | entrystatus= 366 | entryresult= 367 | entryfail=0 368 | entrytime=0 369 | failtime=0 370 | reporttime=0 371 | return 372 | fi 373 | 374 | timenow=$(date +%s) 375 | time=$(echo "$timenow" - "$entrytime" | bc) 376 | debug "Entry with timestamp \"${entrytime}\" ($(date -u -d @${time} +"%T") ago) found for URL \"$url\". Status: $entrystatus Result: $entryresult Fail: $entryfail FailTime: $failtime ReportTime: $reporttime" 377 | 378 | debug "readfile() finished" 379 | 380 | } 381 | 382 | writefile() { 383 | 384 | debug "writefile()" 385 | 386 | result_nospace=$(echo "$result" | sed 's/ /_/g') 387 | echo "$entry" | grep -i "^${url} ${status} .*" >/dev/null 2>&1 388 | if [ $? -eq 0 ]; then 389 | changed=0 390 | else 391 | changed=1 392 | fi 393 | debug "URL: ${url} - Changed=$changed" 394 | 395 | timenow=$(date +%s) 396 | failtime_bc=$(echo "$timenow" - "$failtime" | bc) 397 | reporttime_bc=$(echo "$timenow" - "$reporttime" | bc) 398 | 399 | if [ "$entry" = "" ]; then 400 | entrytime=$(date +%s) 401 | summary_add "This is the first web watch for \"$url\"." 402 | sendreport=1 403 | reporttime=$(date +%s) 404 | elif [ "$fail" -eq 1 ] && [ "$failtime_bc" -ge "$maxfailtime" ]; then 405 | entryfail=1 406 | summary_add "URL \"$url\" failed for more than $(date -u -d @$maxfailtime +"%T")." 407 | if [ "$reporttime_bc" -ge "$reportfreq" ]; then 408 | sendreport=1 409 | reporttime=$(date +%s) 410 | fi 411 | elif [ "$fail" -eq 0 ] && [ "$entryfail" -eq 1 ]; then 412 | entryfail=0 413 | summary_add "URL \"$url\" restored from failed state." 414 | failtime=0 415 | sendreport=1 416 | reporttime=$(date +%s) 417 | fi 418 | 419 | if [ "$fail" -eq 0 ]; then 420 | entryfail=0 421 | failtime=0 422 | fi 423 | 424 | if [ "$entrytime" -le 0 ]; then 425 | entrytime=$(date +%s) 426 | fi 427 | 428 | sed -i "/^${url2} .*$/d" $tmpfile 429 | echo "${url} ${status} ${result_nospace} ${entryfail} ${failtime} ${reporttime} ${entrytime}" >>${tmpfile} 430 | 431 | debug "writefile() finished" 432 | 433 | } 434 | 435 | summary_add() { 436 | 437 | if [ "$summary" = "" ]; then 438 | summary="$1
" 439 | else 440 | summary="$summary 441 | $1
" 442 | fi 443 | 444 | } 445 | 446 | report_add() { 447 | 448 | if [ "$reportdata" = "" ]; then 449 | reportdata="$1" 450 | else 451 | reportdata="$reportdata 452 | $1" 453 | fi 454 | 455 | } 456 | 457 | sendreport() { 458 | 459 | debug "sendreport()" 460 | 461 | status "Sending monitor web watch report..." 462 | 463 | scriptend=$(date +%s) 464 | scripttime=$(echo "$scriptend" - "$scriptstart" | bc) 465 | reporttime=$(date +%s) 466 | 467 | report_add "" 468 | report_add "" 469 | report_add "

Monitor Web Watch

" 470 | if ! [ "$inetwasdowntext" = "" ]; then 471 | report_add "

$inetwasdowntext

" 472 | fi 473 | if ! [ "$summary" = "" ]; then 474 | report_add "

$summary

" 475 | fi 476 | report_add "
" 477 | report_add "Status:
" 478 | report_add "$maildata" 479 | report_add "
" 480 | report_add "New report is sent every $(date -u -d @${reportfreq} +"%T") if status is changed or there is an error." 481 | 482 | LANG=en_GB 483 | EMAIL=$emailfrom mutt -e 'set content_type=text/html' -s "Monitor Web Watch" $emailto <. 18 | # 19 | # Redistributions of this script must retain the above copyright notice. 20 | # 21 | # ABOUT THIS SCRIPT: 22 | # 23 | # This script imports photos directly from your camera or memory card 24 | # into your specified photo directory, it copies and renames the photo 25 | # files after photo creation date in the format: 26 | # img-yyyymmdd-hhmmss-nnn-widthxheight.ext 27 | # 28 | 29 | version="0.2.5" 30 | 31 | # Basic settings 32 | 33 | logging=1 # Log all the changes to a file so you can see what has been done. 34 | logfile="pimport.log" # Filename of the logfile. 35 | createmd5sum=0 # Create a MD5 checksum file in each directory. 36 | autorotate=1 # Rotate photos that are taken portrait. 37 | 38 | devices="/dev/sdd1 /dev/sde1 /dev/sdf1 /dev/sdg1 /dev/sdh1 /dev/sdi1" 39 | folders="DCIM" 40 | photodir="$HOME/Photos/New" 41 | tempdir="/tmp" 42 | tempdiresc=$(echo "$tempdir" | sed 's/ /\\\ /g') 43 | 44 | 45 | ##################### DONT CHANGE ANYTHING BELOW HERE ##################### 46 | 47 | 48 | # Find out how to echo without newline 49 | 50 | c='' 51 | n='' 52 | if [ "$(eval echo -n 'a')" = "-n a" ] ; then 53 | c='\c' 54 | else 55 | n='-n' 56 | fi 57 | 58 | # Set dirs 59 | 60 | destdir=$photodir 61 | if ! [ "$1" = "" ] ; then 62 | case "$1" in 63 | /dev/* ) devices=$1;; 64 | * ) sourcedir=$1;; 65 | esac 66 | if ! [ "$2" = "" ] ; then 67 | destdir=$2 68 | else 69 | destdir=$photodir 70 | fi 71 | fi 72 | 73 | # See if the system has the needed commands to continue. 74 | 75 | for cmd in "which" "ls" "mkdir" "mv" "cp" "rm" "sed" "awk" "bc" "tr" "date" "identify" "convert" "mogrify" "exiv2" "ufraw-batch" "md5sum" "udisksctl" "mount" 76 | do 77 | which $cmd >/dev/null 2>&1 78 | if [ $? != 0 ] ; then 79 | echo "ERROR: Missing the \"${cmd}\" command!" 80 | exit 1 81 | fi 82 | done 83 | 84 | tput bold 85 | tput setaf 7 86 | echo " _ 87 | /.) ╔═╗┌─┐┌┬┐┌─┐┬─┐┌─┐ ╔═╗┬ ┬┌─┐┌┬┐┌─┐ ╦┌┬┐┌─┐┌─┐┬─┐┌┬┐┌─┐┬─┐ 88 | /)\| ║ ├─┤│││├┤ ├┬┘├─┤ ╠═╝├─┤│ │ │ │ │ ║│││├─┘│ │├┬┘ │ ├┤ ├┬┘ 89 | // / ╚═╝┴ ┴┴ ┴└─┘┴└─┴ ┴ ╩ ┴ ┴└─┘ ┴ └─┘ ╩┴ ┴┴ └─┘┴└─ ┴ └─┘┴└─ 90 | /'\" \" v${version} by Jonas Kvinge 91 | " 92 | tput sgr0 93 | 94 | # Function to log to file and screen 95 | 96 | logfile () { 97 | 98 | if [ "$2" = 2 ] ; then 99 | echo "$1" 100 | fi 101 | 102 | if [ "$logging" = "1" ] && ! [ "$destdir" = "" ] ; then 103 | echo "$(date) *** $1" >>"$destdir/$logfile" || fail 104 | fi 105 | 106 | } 107 | 108 | fail () { 109 | 110 | sleep 90d 111 | 112 | exit 1 113 | 114 | } 115 | 116 | # Mount device 117 | 118 | devices_total=0 119 | devices_valid=0 120 | devices_mount=0 121 | devices_photo=0 122 | 123 | if [ "$sourcedir" = "" ]; then 124 | if ! [ "$device" = "" ]; then 125 | devices=$device 126 | fi 127 | echo "Scanning devices \"$devices\"..." 128 | for device in $devices 129 | do 130 | #echo "Device: $device" 131 | devices_total=$(echo $devices_total + 1 | bc) 132 | ls "$device" >/dev/null 2>&1 133 | if ! [ $? -eq 0 ]; then 134 | continue 135 | fi 136 | echo "Found device \"$device\"." 137 | devices_valid=$(echo $devices_valid + 1 | bc) 138 | mountpoint= 139 | automounted= 140 | mount=$(mount | grep "^${device}") 141 | if [ "$mount" = "" ]; then 142 | echo $n "Mounting \"$device\". $c" 143 | udisksctl mount -b "$device" || continue 144 | # udisks may return 0 even when it's not mounted. 145 | mount=$(mount | grep "^${device}.*") 146 | if [ "$mount" = "" ]; then 147 | echo "Failed." 148 | continue 149 | fi 150 | mountpoint=$(echo "$mount" | awk '{print $3}') 151 | automounted=1 152 | if [ "$mountpoint" = "" ]; then 153 | echo "Failed to find mountpoint." 154 | logfile "ERROR: Unable to find mountpoint for \"$device\"." 1 155 | udisksctl unmount -b "$device" 156 | continue 157 | fi 158 | #echo "$mountpoint." 159 | else 160 | echo $n "Probing \"$device\". $c" 161 | mountpoint=$(echo "$mount" | awk '{print $3}') 162 | if [ "$mountpoint" = "" ]; then 163 | echo "Failed to find mountpoint." 164 | logfile "ERROR: Unable to find mountpoint for \"$device\"." 1 165 | continue 166 | fi 167 | echo "$mountpoint." 168 | fi 169 | devices_mount=$(echo "$devices_mount" + 1 | bc) 170 | logfile "Device \"$device\" mounted on \"$mountpoint\"." 1 171 | found=0 172 | for folder in $folders 173 | do 174 | for i in "$mountpoint"/* 175 | do 176 | i=$(echo "$i" | awk -F/ '{print $NF}') 177 | if [ "$i" = "$folder" ]; then 178 | found=1 179 | break 180 | fi 181 | done 182 | done 183 | if [ $found = "0" ]; then 184 | logfile "Device \"$device\" missing folders $folders, skipping." 1 185 | if [ "$automounted" = "1" ]; then 186 | udisksctl unmount -b "$device" 187 | fi 188 | mountpoint= 189 | automounted= 190 | continue 191 | fi 192 | devices_photo=$(echo "$devices_photo" + 1 | bc) 193 | tput bold 194 | echo $n "Contents: $c" 195 | tput setaf 2 196 | ls "$mountpoint" 197 | tput sgr0 198 | while true; do 199 | read -e -n 1 -p "Use $device (Y/N) " answer 200 | case $answer in 201 | [Yy]* ) answer=1;break;; 202 | [Nn]* ) answer=0;break;; 203 | * ) echo "Invalid answer. Press Y or N.";; 204 | esac 205 | done 206 | if [ "$answer" = "0" ]; then 207 | if [ "$automounted" = "1" ]; then 208 | udisksctl unmount -b "$device" 209 | fi 210 | mountpoint= 211 | automounted= 212 | continue 213 | fi 214 | break 215 | done 216 | sourcedir=$mountpoint 217 | fi 218 | 219 | if [ "$sourcedir" = "" ]; then 220 | if [ "$devices_total" -eq 0 ]; then 221 | logfile "No devices found." 2 222 | else 223 | if [ "$devices_valid" -eq 0 ]; then 224 | logfile "No valid devices found." 2 225 | else 226 | if [ "$devices_mount" -eq 0 ]; then 227 | logfile "Unable to mount any devices." 2 228 | else 229 | if [ "$devices_photo" -eq 0 ]; then 230 | logfile "Unable to find photos on any devices." 2 231 | else 232 | logfile "No more devices with photos." 2 233 | fi 234 | fi 235 | fi 236 | fi 237 | read -e -n 1 -p "" answer 238 | exit 1 239 | fi 240 | 241 | # Confirmation 242 | 243 | while true; do 244 | read -e -n 1 -p "Import photos from $sourcedir to $destdir (Y/N) " answer 245 | case $answer in 246 | [Yy]* ) break;; 247 | [Nn]* ) exit 0;; 248 | * ) echo "Invalid answer. Press Y or N.";; 249 | esac 250 | done 251 | 252 | # Log start 253 | 254 | logfile "Script started with: $0 $*" 2 255 | if [ "$logging" = "1" ] ; then 256 | echo "Log file stored as $destdir/$logfile" 257 | fi 258 | 259 | # Create photo directory 260 | 261 | mkdir -p "$destdir" || fail 262 | 263 | if [ ! -d "$destdir" ] ; then 264 | echo "ERROR: \"$destdir\" is not a directory!" 265 | read -e -n 1 -p "" answer 266 | exit 1 267 | fi 268 | 269 | # Parse the camera directory. 270 | 271 | #Replace space with colon to avoid splitting up one image name. 272 | #for imagename in $cdir/* 273 | # | sed 's/ /\:/g') 274 | IFS_DEFAULT=$IFS 275 | IFS=$'\n' 276 | for fullfile in $(find "$sourcedir") 277 | do 278 | 279 | IFS=$IFS_DEFAULT 280 | 281 | if [ -d "$fullfile" ] ; then 282 | continue 283 | fi 284 | 285 | #fullfile=$(echo $fullfile | sed 's/:/\ /g') 286 | cdir=$(dirname "$fullfile") 287 | imagename=$(echo "$fullfile" | awk -F/ '{print $NF}') 288 | 289 | #cdiresc=$(echo "$cdir" | sed 's/ /\\\ /g') 290 | imagenameesc=$(echo "$imagename" | sed 's/ /\\\ /g') 291 | imagenamefake=$(echo "$imagename" | sed 's/ /\:/g') 292 | 293 | # Check if the file is a known image file. 294 | 295 | image=0 296 | imagenamelow=$(echo "$imagename" | tr '[:upper:]' '[:lower:]') 297 | case "$imagenamelow" in 298 | *.jpg ) image=1;; 299 | *.jpeg ) image=1;; 300 | *.cr2 ) image=1;; 301 | * ) image=0;; 302 | esac 303 | if [ "$image" = 1 ] ; then 304 | if [ "$imagelist" = "" ] ; then 305 | imagelist="$imagenamefake" 306 | else 307 | imagelist="$imagelist $imagenamefake" 308 | fi 309 | else 310 | logfile "Skipping file \"$cdir/$imagename\", unknown image format." 2 311 | continue 312 | fi 313 | 314 | # Copy image to a temporary location 315 | 316 | echo $n "Copying \"$cdir/$imagename\" to tempdir \"$tempdir/$imagename\". $c" 317 | cp --preserve=timestamps "$cdir/$imagename" "$tempdir/$imagename" || fail 318 | echo "Done." 319 | logfile "Copied \"$cdir/$imagename\" to tempdir \"$tempdir/$imagename\"." 1 320 | 321 | # Determine orientation 322 | 323 | #exif=$(exiftool -Orientation -n "$tempdir/$imagename") || fail 324 | #if [ "$exif" = "" ] ; then 325 | # logfile "ERROR: Cannot determine orientation for image: \"$tempdir/$imagename\"." 2 326 | # read -e -n 1 -p "" answer 327 | # exit 1 328 | #fi 329 | #orientation=$(echo "$exif" | awk '{print $3}') || fail 330 | orientation=$(identify -format '%[exif:orientation]' "$tempdir/$imagename") 331 | if [ "$orientation" = "" ] ; then 332 | logfile "ERROR: Cannot determine orientation for image: \"$tempdir/$imagename\"." 2 333 | read -e -n 1 -p "" answer 334 | exit 1 335 | fi 336 | 337 | # Rotate portrait photos, we need to do this BEFORE numbering the image, since checksum will change on the file. 338 | 339 | if [ "$autorotate" = "1" ]; then 340 | if [ "$orientation" = "6" ] || [ "$orientation" = "8" ]; then 341 | echo $n "Rotating \"$tempdir/$imagename\". $c" 342 | convert -auto-orient "$tempdir/$imagename" "$tempdir/$imagename" 343 | echo "Done." 344 | orientation=$(identify -format '%[exif:orientation]' "$tempdir/$imagename") 345 | if [ "$orientation" = "" ] ; then 346 | logfile "ERROR: Cannot determine orientation for image: \"$tempdir/$imagename\"." 2 347 | read -e -n 1 -p "" answer 348 | exit 1 349 | fi 350 | fi 351 | fi 352 | 353 | #if [ "$autorotate" = "1" ] && [ "$width" -gt "$height" ]; then 354 | # if [ "$orientation" = "6" ]; then 355 | # echo $n "Rotating \"$tempdir/$imagename\" 90 degrees. $c" 356 | # convert -rotate 90 "$tempdir/$imagename" "$tempdir/$imagename" 357 | # exiftool -Orientation=1 -overwrite_original -n "$tempdir/$imagename" 358 | #echo "Done." 359 | # logfile "Rotated \"$tempdir/$imagename\" 90 degrees." 1 360 | # newwidth=$height 361 | # newheight=$width 362 | # elif [ "$orientation" = "8" ]; then 363 | # echo $n "Rotating \"$tempdir/$imagename\" 270 degrees. $c" 364 | # convert -rotate 270 "$tempdir/$imagename" "$tempdir/$imagename" 365 | # exiftool -Orientation=1 -overwrite_original -n "$tempdir/$imagename" 366 | # logfile "Rotated \"$tempdir/$imagename\" 270 degrees." 1 367 | # newwidth=$height 368 | # newheight=$width 369 | # fi 370 | #fi 371 | 372 | # Determine date 373 | 374 | #echo $n "Dermining date for image \"$tempdir/$imagename\". $c" 375 | 376 | exif=$(exiv2 "$tempdir/$imagename" | grep -a "^Image timestamp :") 377 | if [ "$exif" = "" ] ; then 378 | logfile "ERROR: Cannot determine date for image: \"$tempdir/$imagename\"." 2 379 | read -e -n 1 -p "" answer 380 | exit 1 381 | fi 382 | #echo "exif: $exif" 383 | 384 | date=$(echo "$exif" | awk '{print $4}') || fail 385 | if [ "$date" = "" ] ; then 386 | logfile "ERROR: Cannot determine date for image: \"$tempdir/$imagename\"." 2 387 | read -e -n 1 -p "" answer 388 | exit 1 389 | fi 390 | #echo "Date: $date" 391 | 392 | time=$(echo "$exif" | awk '{print $5}') || fail 393 | if [ "$date" = "" ] ; then 394 | logfile "ERROR: Cannot determine time for image: \"$tempdir/$imagename\"." 2 395 | read -e -n 1 -p "" answer 396 | exit 1 397 | fi 398 | 399 | date=$(echo "$date" | sed 's/://g') || fail 400 | if [ "$date" = "" ] ; then 401 | logfile "ERROR: Cannot determine date for image: \"$tempdir/$imagename\"." 2 402 | read -e -n 1 -p "" answer 403 | exit 1 404 | fi 405 | time=$(echo "$time" | sed 's/://g') || fail 406 | if [ "$date" = "" ] ; then 407 | logfile "ERROR: Cannot determine date for image: \"$tempdir/$imagename\"." 2 408 | read -e -n 1 -p "" answer 409 | exit 1 410 | fi 411 | 412 | dir=$date 413 | 414 | # Determine the image format and geometry for use in the filename. 415 | 416 | echo $n "Dermining format and geometry for image \"$tempdir/$imagename\". $c" 417 | 418 | identify=$(identify "$tempdiresc/$imagenameesc") || fail 419 | #identifyfake=$(echo "$identify" | sed 's/\//:/g') 420 | #cdirfake=$(echo "$cdir" | sed 's/\//:/g') 421 | #identify=$(echo "$identifyfake" | sed "s/${cdirfake}:${imagename} //g") 422 | 423 | if [ "$identify" = "" ] ; then 424 | logfile "ERROR: Cannot determine format and geometry for image: \"$tempdir/$imagename\"." 2 425 | read -e -n 1 -p "" answer 426 | exit 1 427 | fi 428 | format=$(echo "$identify" | awk '{print $2}') || fail 429 | if [ "$format" = "" ] ; then 430 | logfile "ERROR: Cannot determine format for image: \"$tempdir/$imagename\"." 2 431 | read -e -n 1 -p "" answer 432 | exit 1 433 | fi 434 | extension=$(echo "$format" | tr '[:upper:]' '[:lower:]') || fail 435 | if [ "$extension" = "" ] ; then 436 | logfile "ERROR: Cannot determine extension for image: \"$tempdir/$imagename\"." 2 437 | read -e -n 1 -p "" answer 438 | exit 1 439 | fi 440 | case "$extension" in 441 | jpeg ) extension=jpg;; 442 | esac 443 | geometry=$(echo "$identify" | awk '{print $3}') || fail 444 | if [ "$geometry" = "" ] ; then 445 | logfile "ERROR: Cannot determine geometry for image: \"$tempdir/$imagename\"." 2 446 | read -e -n 1 -p "" answer 447 | exit 1 448 | fi 449 | 450 | # Geometry can be 563x144+0+0 or 75x98 451 | # we need to get rid of the plus (+) and the x characters: 452 | width=$(echo "$geometry" | sed 's/[^0-9]/ /g' | awk '{print $1}') || fail 453 | if [ "$width" = "" ] ; then 454 | logfile "ERROR: Cannot determine width for image: \"$tempdir/$imagename\"." 2 455 | read -e -n 1 -p "" answer 456 | exit 1 457 | fi 458 | height=$(echo "$geometry" | sed 's/[^0-9]/ /g' | awk '{print $2}') || fail 459 | if [ "$height" = "" ] ; then 460 | logfile "ERROR: Cannot determine height for image: \"$tempdir/$imagename\"." 2 461 | read -e -n 1 -p "" answer 462 | exit 1 463 | fi 464 | newheight=$height 465 | newwidth=$width 466 | 467 | echo "$format ${width}x${height}" 468 | 469 | # Calculate the image number for use in the filename. 470 | skip=0 471 | for (( count = 1 ;; count++ )) 472 | do 473 | if [ "$count" -gt 999 ] ; then 474 | logfile "ERROR: Directory \"$dir\" has more then 999 images. Skipping image \"$tempdir/$imagename\"."; 2 475 | skip=1 476 | break 477 | fi 478 | imagenumber=$(echo "$count" | awk '{printf("%.3d", $1)}') || fail 479 | if [ "$imagenumber" = "" ] ; then 480 | logfile "ERROR: Cannot format number for image: \"$tempdir/$imagename\"."; 2 481 | read -e -n 1 -p "" answer 482 | exit 1 483 | fi 484 | newname="img-${date}-${time}-${imagenumber}-${newwidth}x${newheight}.${extension}" 485 | if [ -f "$destdir/$dir/$newname" ] ; then 486 | # Make sure we dont overwrite another image. 487 | md5sum1=$(md5sum "$destdir/$dir/$newname" | awk '{print $1}') 488 | if [ "$md5sum1" = "" ]; then 489 | logfile "ERROR: Cannot determine md5sum for image: \"$destdir/$dir/$newname\"." 2 490 | rm -f "$tempdir/$imagename" 491 | read -e -n 1 -p "" answer 492 | exit 1 493 | fi 494 | md5sum2=$(md5sum "$tempdir/$imagename" | awk '{print $1}') 495 | if [ "$md5sum2" = "" ]; then 496 | logfile "ERROR: Cannot determine md5sum for image: \"$tempdir/$imagename\"." 2 497 | rm -f "$tempdir/$imagename" 498 | read -e -n 1 -p "" answer 499 | exit 1 500 | fi 501 | if [ "$md5sum1" = "$md5sum2" ]; then 502 | # The same image already exist with right filename. 503 | logfile "Image \"$tempdir/$imagename\" already exist as \"$destdir/$dir/$newname\"." 2 504 | skip=1 505 | break 506 | else 507 | continue 508 | fi 509 | else 510 | # Found free filename. 511 | skip=0 512 | break 513 | fi 514 | done 515 | 516 | if [ "$skip" = "1" ] ; then 517 | rm -f "$tempdir/$imagename" 518 | continue 519 | fi 520 | 521 | # Create directory 522 | 523 | if [ ! -d "$destdir/$dir" ] ; then 524 | echo $n "Creating directory \"$destdir/$dir\". $c" 525 | mkdir "$destdir/$dir" || { 526 | rm -f "$tempdir/$imagename"; 527 | read -e -n 1 -p "" answer; 528 | exit 1; 529 | } 530 | echo "Done." 531 | fi 532 | 533 | # Copy image 534 | 535 | echo $n "Copying \"$tempdir/$imagename\" to \"$destdir/$dir/$newname\". $c" 536 | mv "$tempdir/$imagename" "$destdir/$dir/$newname" || { 537 | rm -f "$tempdir/$imagename"; 538 | read -e -n 1 -p "" answer; 539 | exit 1 540 | } 541 | echo "Done." 542 | logfile "Copied \"$tempdir/$imagename\" to \"$destdir/$dir/$newname\"." 1 543 | 544 | if [ "$createmd5sum" = "1" ] ; then 545 | md5sum "$destdir/$dir/$newname" >>"$destdir/$dir/$dir.md5" 546 | fi 547 | 548 | done 549 | 550 | if [ "$automounted" = "1" ]; then 551 | udisksctl unmount -b "$device" 552 | fi 553 | 554 | echo "Done." 555 | 556 | sleep 90d 557 | 558 | exit 0 559 | -------------------------------------------------------------------------------- /photo/prename.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ########################################################################### 3 | # prename.sh - Camera Photo File Renamer 4 | # Copyright (C) 2006-2010 Jonas Kvinge 5 | ########################################################################### 6 | # Redistributions of this source code must retain the above 7 | # copyright notice. 8 | ########################################################################### 9 | # This program is free software; you can redistribute it and/or modify 10 | # it under the terms of the GNU General Public License as published by 11 | # the Free Software Foundation; either version 2 of the License, or 12 | # (at your option) any later version. 13 | # 14 | # This program is distributed in the hope that it will be useful, 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | # GNU General Public License for more details. 18 | # 19 | # You should have received a copy of the GNU General Public License along 20 | # with this program; if not, write to the Free Software Foundation, Inc., 21 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 22 | # 23 | ########################################################################### 24 | # 25 | # ABOUT THIS SCRIPT: 26 | # 27 | # This script renames photos in a all the subdirectories inside the 28 | # specified directory. 29 | # 30 | # The format is: img-subdir-xxx-widthxheight.ext 31 | # 32 | ########################################################################### 33 | 34 | version="1.3.2" 35 | 36 | # Basic settings 37 | 38 | logging=0 # Log all the changes to a file so you can see what has been done. 39 | logfile="prename.log" # Filename of the logfile. 40 | renamedirs=1 # Remove '_' and '-' from dirs. 41 | 42 | 43 | ########################################################################### 44 | ##################### DONT CHANGE ANYTHING BELOW HERE ##################### 45 | ########################################################################### 46 | 47 | # Find out how to echo without newline 48 | 49 | c='' 50 | n='' 51 | if [ "$(eval echo -n 'a')" = "-n a" ] ; then 52 | c='\c' 53 | else 54 | n='-n' 55 | fi 56 | 57 | # Exit if no directory is specified. 58 | 59 | if [ "$1" = "" ] ; then 60 | echo "Usage: $0 " 61 | exit 1 62 | fi 63 | 64 | pdir="$1" 65 | 66 | # Check if the root directory is valid. 67 | 68 | if [ ! -d "$pdir" ] ; then 69 | echo "ERROR: \"$pdir\" is not a directory!" 70 | exit 1 71 | fi 72 | 73 | # See if the system has the needed commands to continue. 74 | 75 | for cmd in "which" "ls" "mkdir" "mv" "cp" "rm" "sed" "awk" "bc" "tr" "date" "identify" "convert" "mogrify" 76 | do 77 | which $cmd >/dev/null 2>&1 78 | if [ $? != 0 ] ; then 79 | echo "ERROR: Missing the \"${cmd}\" command!" 80 | exit 1 81 | fi 82 | done 83 | 84 | echo "--------------------------------------------------------" 85 | echo "Camera Photo File Renamer v${version}" 86 | echo "Copyright (C) 2006-2010 Jonas Kvinge" 87 | echo "--------------------------------------------------------" 88 | 89 | # Function to log to file and screen 90 | 91 | logfile () { 92 | 93 | if [ "$2" = 2 ] ; then 94 | echo "$1" 95 | fi 96 | 97 | if [ "$logging" = "1" ] ; then 98 | echo "$(date) *** $1" >>"$pdir/$logfile" || exit 1 99 | fi 100 | 101 | } 102 | 103 | logfile "Script started with: $0 $*" 2 104 | if [ "$logging" = "1" ] ; then 105 | echo "Log file stored as $pdir/$logfile" 106 | fi 107 | 108 | # Parse the photo directory. 109 | 110 | for ddir in "$pdir"/* 111 | do 112 | 113 | ddir=$(echo "$ddir" | awk -F/ '{print $NF}') 114 | 115 | # If this is not a directory, then skip it. 116 | if [ ! -d "${pdir}/${ddir}" ] ; then 117 | continue 118 | fi 119 | 120 | logfile "Entering directory \"$pdir/$ddir\"." 2 121 | 122 | if [ "$renamedirs" = "1" ] ; then 123 | ddirnew=$(echo "$ddir" | sed -e 's/_//g' -e 's/-//g') 124 | if [ ! "$ddirnew" = "$ddir" ] ; then 125 | echo $n "Renaming \"$pdir/$ddir\" to \"$pdir/$ddirnew\". $c" 126 | mv "${pdir}/${ddir}" "${pdir}/${ddirnew}" || exit 1 127 | echo "Done." 128 | logfile "Renamed directory \"$pdir/$ddir\" to \"$pdir/$ddirnew\"." 1 129 | fi 130 | ddir="$ddirnew" 131 | fi 132 | 133 | image=0 134 | skip=0 135 | imagelist="" 136 | 137 | # Parse the files in the album. 138 | 139 | for imagename in "${pdir}"/"${ddir}"/* 140 | do 141 | 142 | imagename=$(echo "$imagename" | awk -F/ '{print $NF}') 143 | imagenamefake=$(echo "$imagename" | sed 's/ /\:/g') 144 | 145 | # Create the imagelist and ignore all files except for known images. 146 | image=0 147 | imagenamelow=$(echo "$imagename" | tr '[:upper:]' '[:lower:]') 148 | case "$imagenamelow" in 149 | *.bmp ) image=1;; 150 | *.jpg ) image=1;; 151 | *.jpeg ) image=1;; 152 | *.gif ) image=1;; 153 | *.png ) image=1;; 154 | *.cr2 ) image=1;; 155 | *.raw ) image=1;; 156 | * ) image=0;; 157 | esac 158 | if [ "$image" = 1 ] ; then 159 | if [ "$imagelist" = "" ] ; then 160 | imagelist="$imagenamefake" 161 | else 162 | imagelist="$imagelist $imagenamefake" 163 | fi 164 | else 165 | logfile "Skipping file \"$pdir/$ddir/$imagename\", unknown image format." 2 166 | fi 167 | done 168 | # If Skip is set to 1 then this album is processed, continue to the next directory. 169 | if [ "$skip" = "1" ] ; then 170 | continue 171 | fi 172 | if [ "$imagelist" = "" ] ; then 173 | logfile "Skipping directory \"$pdir/$ddir\", no images in directory." 2 174 | continue 175 | fi 176 | 177 | # Take image by image and rename 178 | 179 | #if [ "$createmd5sum" = "1" ] ; then 180 | # rm "$pdir/$ddir/$ddir.md5" 181 | #fi 182 | 183 | count=0 184 | for imagenamefake in $imagelist 185 | do 186 | 187 | # Set the real filename 188 | 189 | imagename=$(echo "$imagenamefake" | sed 's/:/\ /g') 190 | 191 | # Determine the image format and geometry for use in the filename. 192 | 193 | #echo $n "Dermining format and geometry for image \"$pdir/$ddir/$imagename\". $c" 194 | identify=$(identify "$pdir/$ddir/$imagename" | sed "s/${pdir}\/${ddir}\/${imagename} //g") || exit 1 195 | if [ "$identify" = "" ] ; then 196 | logfile "ERROR: Cannot determine format and geometry for image: \"$pdir/$ddir/$imagename\"." 2 197 | continue 198 | fi 199 | format=$(echo "$identify" | awk '{print $1}') || exit 1 200 | if [ "$format" = "" ] ; then 201 | logfile "ERROR: Cannot determine format for image: \"$pdir/$ddir/$imagename\"." 2 202 | continue 203 | fi 204 | extension=$(echo "$format" | tr '[:upper:]' '[:lower:]') || exit 1 205 | if [ "$extension" = "" ] ; then 206 | logfile "ERROR: Cannot determine extension for image: \"$pdir/$ddir/$imagename\"." 2 207 | continue 208 | fi 209 | case "$extension" in 210 | jpeg ) extension=jpg;; 211 | esac 212 | geometry=$(echo "$identify" | awk '{print $2}') || exit 1 213 | if [ "$geometry" = "" ] ; then 214 | logfile "ERROR: Cannot determine geometry for image: \"$pdir/$ddir/$imagename\"." 2 215 | continue 216 | fi 217 | 218 | # Geometry can be 563x144+0+0 or 75x98 219 | # we need to get rid of the plus (+) and the x characters: 220 | width=$(echo "$geometry" | sed 's/[^0-9]/ /g' | awk '{print $1}') || exit 1 221 | if [ "$width" = "" ] ; then 222 | logfile "ERROR: Cannot determine width for image: \"$pdir/$ddir/$imagename\"." 2 223 | continue 224 | fi 225 | height=$(echo "$geometry" | sed 's/[^0-9]/ /g' | awk '{print $2}') || exit 1 226 | if [ "$height" = "" ] ; then 227 | logfile "ERROR: Cannot determine height for image: \"$pdir/$ddir/$imagename\"." 2 228 | continue 229 | fi 230 | #echo "$format $geometry" 231 | 232 | # Determine image date for use in filename 233 | 234 | #echo $n "Dermining date for image \"$pdir/$ddir/$imagename\". $c" 235 | date=$(identify -format "%[exif:DateTimeOriginal]" "$pdir/$ddir/$imagename" | sed 's/\://g' | sed 's/ /-/g') || exit 1 236 | if [ "$date" = "" ] ; then 237 | logfile "ERROR: Cannot determine date for image: \"$pdir/$ddir/$imagename\"." 2 238 | continue 239 | fi 240 | #echo "$date" 241 | 242 | # Calculate the image number for use in the filename. 243 | 244 | count=$(echo "$count" + 1 | bc) 245 | if [ "$count" -gt 999 ] ; then 246 | logfile "ERROR: Directory \"$pdir/$ddir\" has more then 999 images. Skipping the rest."; 2 247 | break 248 | fi 249 | imagenumber=$(echo "$count" | awk '{printf("%.3d", $1)}') || exit 1 250 | if [ "$imagenumber" = "" ] ; then 251 | logfile "ERROR: Cannot format number for image: \"$pdir/$ddir/$imagename\"."; 2 252 | exit 1 253 | fi 254 | 255 | # Rename the filename. 256 | 257 | newname="img-${date}-${imagenumber}-${width}x${height}.${extension}" 258 | count2=0 259 | while : 260 | do 261 | if [ "$imagename" = "$newname" ] ; then 262 | logfile "No need to rename file \"$pdir/$ddir/$imagename\"." 2 263 | break 264 | fi 265 | if [ -f "$pdir/$ddir/$newname" ] ; then 266 | count2=$(echo "$count2" + 1 | bc) 267 | imagenumber2=$(echo "$count2" | awk '{printf("%.3d", $1)}') || exit 1 268 | logfile "ERROR: File \"$pdir/$ddir/$newname\" already exist, adding extra number $count2 at the end of the filename." 2 269 | newname="img-${date}-${imagenumber}-${width}x${height}-${imagenumber2}.${extension}" 270 | continue 271 | fi 272 | #echo $n "Renaming \"$pdir/$ddir/$imagename\" to \"$pdir/$ddir/$newname\". $c" 273 | mv -vn "$pdir/$ddir/$imagename" "$pdir/$ddir/$newname" || exit 1 274 | #echo "Done." 275 | logfile "Renamed \"$pdir/$ddir/$imagename\" to \"$pdir/$ddir/$newname\"." 1 276 | imagename="$newname" 277 | break 278 | done 279 | 280 | #if [ "$createmd5sum" = "1" ] ; then 281 | # md5sum "$pdir/$ddir/$imagename" >>"$pdir/$ddir/$ddir.md5" 282 | #fi 283 | 284 | done 285 | logfile "Leaving directory \"$pdir/$ddir\"." 2 286 | 287 | done 288 | 289 | exit 0 290 | -------------------------------------------------------------------------------- /photo/presize.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # presize.sh 4 | # Copyright (C) 2019 Jonas Kvinge 5 | # 6 | # This script is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # This script is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU General Public License 17 | # along with this script. If not, see . 18 | # 19 | # Redistributions of this script must retain the above copyright notice. 20 | # 21 | 22 | # Resize all JPEG's in the specific directory. 23 | # Always convert to progressive when progressive is set to 1. 24 | 25 | sourcedir="/run/media/jonas/MUSIC/" 26 | maxsize=1000 27 | progressive=1 28 | #92666.220695 29 | 30 | which sed awk convert identify >/dev/null || exit 1 31 | 32 | IFS_DEFAULT=$IFS 33 | IFS=$'\n' 34 | for fullfile in $(find $sourcedir) 35 | do 36 | 37 | IFS=$IFS_DEFAULT 38 | 39 | if [ -d "$fullfile" ] ; then 40 | continue 41 | fi 42 | 43 | dir=$(dirname "$fullfile") 44 | #diresc=$(echo "$dir" | sed 's/ /\\\ /g') 45 | imagename=$(echo "$fullfile" | awk -F/ '{print $NF}') 46 | #imagenameesc=$(echo "$imagename" | sed 's/ /\\\ /g') 47 | 48 | image=0 49 | imagenamelow=$(echo "$imagename" | tr '[:upper:]' '[:lower:]') 50 | case "$imagenamelow" in 51 | *.jpg ) image=1;; 52 | *.jpeg ) image=1;; 53 | * ) image=0;; 54 | esac 55 | 56 | if ! [ "$image" = 1 ] ; then 57 | continue 58 | fi 59 | 60 | geometry=$(identify "${dir}/${imagename}" | sed 's/.*JPEG //g' | awk '{print $1}') || exit 1 61 | if [ "$geometry" = "" ] ; then 62 | echo "ERROR: Cannot determine format and geometry for image: \"$imagename\"." 63 | exit 1 64 | fi 65 | 66 | # Geometry can be 563x144+0+0 or 75x98 67 | # we need to get rid of the plus (+) and the x characters: 68 | width=$(echo "$geometry" | sed 's/[^0-9]/ /g' | awk '{print $1}') || exit 1 69 | if [ "$width" = "" ] ; then 70 | echo "ERROR: Cannot determine width for image: \"$imagename\"." 71 | exit 1 72 | fi 73 | height=$(echo "$geometry" | sed 's/[^0-9]/ /g' | awk '{print $2}') || exit 1 74 | if [ "$height" = "" ] ; then 75 | echo "ERROR: Cannot determine height for image: \"$imagename\"." 76 | exit 1 77 | fi 78 | #echo "$imagename ${width}x${height}" 79 | if [ "$height" -gt "$maxsize" ] || [ "$width" -gt "$maxsize" ]; then 80 | echo "Resizing $imagename ${width}x${height}" 81 | convert "$fullfile" -resize ${maxsize}x${maxsize} "$fullfile" || exit 1 82 | fi 83 | 84 | file "$fullfile" | grep 'progressive' >/dev/null 2>&1 85 | if ! [ $? -eq 0 ] && [ "$progressive" -eq 1 ]; then 86 | echo "Converting $imagename to progressive." 87 | convert "$fullfile" -interlace plane "$fullfile" || exit 1 88 | fi 89 | 90 | done 91 | --------------------------------------------------------------------------------