Backup files to Telegram

 Sun, 21 Apr 2024 19:31 UTC

Backup files to Telegram
Image: CC BY 4.0 by cybrkyd


Telegram, the instant messaging platform, can be used as a cloud storage service. The platform allows users on its free tier to upload an unlimited number of files provided that each file is less than 2GB in size. The file size limit is doubled to 4GB per file for paying premium subscribers.

With all this wonderful free cloud storage available, let us do something with it like use it for remote backups.

Telegram as a cloud storage service

To enable background uploads to Telegram, I use telegram-upload. To install telegram-upload, run this command in your terminal:

$ sudo pip3 install -U telegram-upload

Follow the setup instructions which need to be performed in order to connect to your Telegram account.

By default, telegram-upload will send all uploads to the Saved Messages folder on Telegram. It is quite possible to create and use an alternative folder. For my script, I created a new, private Group called Cybrkyd Backup. To obtain the group chat ‘address’, do the following:

  1. Whilst in the group, choose View group info from the menu options (three vertical dots).
  2. Next, choose Manage group then Invite links.
  3. Locate Primary link and make a note of it.

Telegram group primary link

Note: the group should be set to private. If you are prompted to add at least one member, add a bot like Telegram Raw Bot or any other bot then click on Create. Once the group is created, the bots should immediately leave the group; if not, remove them manually.

It should now be possible to run the following command to upload a file to the Group:

$ telegram-upload --to https://t.me/YOUR_PRIMARY_LINK file.zip

Backing up files to Telegram

My backup routine is (1) backup locally and then (2) make a copy of that backup to a remote location.

To use Telegram as my remote backup location, I’ve integrated the following script into my workflow. It is important to note that all the files I will be uploading are 2GB or less in size.

Define the location of the backup files:

# Path to the directory containing backup files
BACKUP_DIR="/path/to/backup-local"

Keep a rolling log of which files have been sent. This is important as we do not want to re-send the same file to Telegram twice:

# File to keep track of sent file names
SENT_FILE="/path/to/backup-local/sent_log.txt"

Next, we will loop through the BACKUP_DIR and check which files have not been sent by checking against the logs. All my backup files are encrypted, so I am only looking for files with .gpg extension:

# Loop through files in the backup directory
for file in "$BACKUP_DIR"/*.gpg; do
file_name=$(basename "$file")
# Check if the file has been sent before
if grep -Fxq "$file_name" "$SENT_FILE"; then
    :

Now that we know which files to send, let’s do that and update the logs:

# Use telegram-upload to send the file
# If using cron, include telegram-upload's explicit location
/home/USER/.local/bin/telegram-upload --to https://t.me/YOUR_PRIMARY_LINK "$file"
echo "File $file_name sent successfully"
# Record the sent file in the log
echo "$file_name" >> "$SENT_FILE"

Here is the full script. This can be set to run periodically using cron to ensure that local backups are saved to a remote location.

#!/bin/bash

# Path to the directory containing backup files
BACKUP_DIR="/path/to/backup-local"

# File to keep track of sent file names
SENT_FILE="/path/to/backup-local/sent_log.txt"

# Loop through files in the backup directory
for file in "$BACKUP_DIR"/*.gpg; do
  file_name=$(basename "$file")
  # Check if the file has been sent before
  if grep -Fxq "$file_name" "$SENT_FILE"; then
    :
  else
  # Use telegram-upload to send the file.
  # If using cron, include telegram-upload's explicit location
  /home/USER/.local/bin/telegram-upload --to https://t.me/YOUR_PRIMARY_LINK "$file"
  echo "File $file_name sent successfully"
  # Record the sent file in the log
  echo "$file_name" >> "$SENT_FILE"
fi
done