📜 ⬆️ ⬇️

Cobian Backup and sending messages to Telegram

I think there are very few system administrators who do not backup any data. This note will be useful (I hope) for those who use such software as Cobian Backup . And especially for those who do not have backup in one place, or even in different cities.


As you already understood from the title, I want to share with you a small script that allows you to send something (in my case, a piece of the log file) to the telegram.


For those who are wondering why it is Telegram, because Cobian Backup can send messages to the post office - I will explain my point of view. Telegram is a popular developing messenger that uses a huge number of people. In my subjective opinion, this is more convenient than an email client. Also, this method was tested on ooooochen bad internet - everything works. And sending by email in the same conditions did not work.


Script for sending messages to Telegram


Logic for myself, I built the following. When performing a task in Cobian Backup, in the last but one line of the log file we have about the following text: "2017-05-31 12:11 Copy completed. Errors: 0, processed files: 3893, copied files: 3893, total size: 2.43 GB ". So I somehow need to pull this penultimate line from the log file. Well, actually what I did - below.


To send messages we will use PowerShell. I borrowed the script myself here , but modified it a little bit.


$chat_id = 'chat_id' #  id ,     .     ,   . $date=get-date -uformat "%Y-%m-%d" #      $text = get-content -Path ('c:\Program Files (x86)\Cobian Backup 11\Logs\log '+$date+'.txt') -Encoding UTF8 # ,    cobian backup $token = 'token' # ,      [string]$text=$text[$text.count-2] #   #     $payload = @{ "chat_id" = $chat_id; "text" = "$text"; "parse_mode" = 'HTML'; } Invoke-WebRequest ` -Uri ("https://api.telegram.org/bot{0}/sendMessage" -f $token) ` -Method Post ` -ContentType "application/json;charset=utf-8" ` -Body (ConvertTo-Json -Compress -InputObject $payload) 

Cobian Backup Setup


For those who use Cobian Backup as a backup utility, I think you shouldn’t describe how tasks are created. For those who are just going to - I think after installation you don’t even need to look for something on the Internet - everything is very simple and accessible. True, there is one BUT - Cobian Backup does not know how to execute PowerShell scripts. Well, it does not matter - but he knows what BAT is. To run use the following BATnik


 TIMEOUT /T 5 /NOBREAK %SystemRoot%\System32\WindowsPowerShell\v1.0\PowerShell.exe -ExecutionPolicy ByPass -command "C:\ToTelegram.ps1" 

ToTelegram.ps1 is a script that we previously created in PowerShell. After creating the bat file, you can already add its execution to the task. In the task parameters, pop up the menu item "Additional actions" and add our BAT file to the final actions.


That's basically all, as you can see nothing complicated. I hope for someone it will be useful.


PS This script satisfies my needs, but I will be very happy if someone makes their suggestions and comments.


')

Source: https://habr.com/ru/post/331870/


All Articles