Back to Home

Cobian Backup and sending messages in Telegram

powershell · cobian backup · cmd

Cobian Backup and sending messages in Telegram

I think there are very few system administrators who do not back up this or that data. This note will be useful (I hope) for those who use such a software product as Cobian Backup . And especially to those who have backed up in more than 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 telegram.


For those who are wondering why Telegram, because Cobian Backup can send messages to e-mails, 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 sooooo bad Internet - everything works. Moreover, sending via email in the same conditions did not work.


Script to send messages to Telegram


I built the following logic for myself. When completing a task in Cobian Backup, in the penultimate line of the log file, we have approximately the following text: "2017-05-31 12:11 Copying 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 is below.


We will use PowerShell to send messages. I borrowed the script here , but slightly modified it.


$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)

Configure Cobian Backup


For those who use Cobian Backup as a backup utility, I think it’s not worth describing 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 affordable. True, there is one BUT - Cobian Backup is not able to execute PowerShell scripts. Well, it doesn’t matter - but he knows what BAT is. To start, 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 we pop out the menu item "Additional actions" and add our BAT file to the final actions.


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


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

Read Next