Skip to content

脚本备份小文件到telegram

把一些重要的配置文件定时保存在Telegram上,以防服务器出现硬件故障导致文件丢失。

#!/bin/bash

# Backup important files to telegram
# 2019/6/5

BACKUPPASS='XXXXXX'
api_key='bot1577714372:BotKeyHereZtWBcXa0Fj7qHzLY8hTbqfzo'
chat_id='-10013chat_id50'

fList=$(tr '\n' ' ' < /data/_run/telegram-backup-data.backuplist)
fName=/tmp/tgbaklp1tt_$(hostname)_$(date +"%FT%H%M%S").tar.gz
fNameEnc=$fName.enc

tar -czvPf  $fName $fList
openssl enc -aes256 -in $fName -out $fNameEnc -pass pass:"${BACKUPPASS}" -md sha1
# decrypt use -
# openssl enc -aes256 -in [encrypted backup] -out decrypted_backup.tar.gz -pass pass:[backup password] -d -md sha1
curl -F chat_id=$chat_id -F document=@"${fNameEnc}" https://api.telegram.org/$api_key/sendDocument  > /dev/null
rm -rf $fName $fNameEnc

exit 0

上面的脚本配合添加一个备份列表文件/opt/scripts/backuplist,将需要备份的文件/文件夹逐行添加

cat > /data/_run/telegram-baskup-data.backuplist <<EOF
/etc/sysctl.conf
/etc/sysctl.d
/etc/sysconfig/iptables
/etc/sysconfig/ip6tables
/etc/systemd/system/*.service
/etc/ssh/sshd_config
$HOME/.vimrc
$HOME/.bashrc
$HOME/.gitconfig
$HOME/.ssh
EOF
chmod +x /data/_run/telegram-baskup-data.sh
crontab -e # 添加

# run every wed 0110am
10 1 * * 3  /data/_run/telegram-baskup-data.sh

Leave a Reply

Your email address will not be published.