Skip to content

Instantly share code, notes, and snippets.

@MParvin
Last active November 6, 2022 09:40
Show Gist options
  • Save MParvin/1978517c362fe747024ce3684ba2c252 to your computer and use it in GitHub Desktop.
Save MParvin/1978517c362fe747024ce3684ba2c252 to your computer and use it in GitHub Desktop.
MySQL dump with telegram failure notification
#!/bin/bash -x
# database credentials
db_user=""
db_pass=""
db_name=""
db_host="" # e.g 127.0.0.1 or localhost
# FTP credentials
ftp_url="" e.g ftp://ftp.example.com
ftp_user=""
ftp_pass=""
# Telegram config
telegram_token=""
chat_id="" # Admin or group chat id
# Backup credential and path
backup_password=""
backup_path="" # e.g /var/backup/
# Backup name config
backup_suffix=`date +%F`
backup_prefix="your_site_"
### Backup Section ####
# Backup name generator
backup_name="$backup_prefix$backup_suffix"
# Create sql backup
mysqldump --lock-tables=false -u"$db_user" -p"$db_pass" -h"$db_host" $db_name > "$backup_path/$backup_name.sql"
# Zip and encrypt backup
7za a "$backup_path/$backup_name.zip" -tzip -p"$backup_password" -mem=AES256 "$backup_path/$backup_name"
# Delete sql file
rm -rf $backup_path/*.sql
# Send backup to FTP server via curl
(curl -T "$backup_path$backup_name.zip" $ftp_url --user $ftp_user:$ftp_pass && \
rm -rf $backup_path/*.zip ) || \
( echo "error in bakckup $backup_name" && curl -sS -X POST https://api.telegram.org/bot$telegram_token/sendMessage -d"text=backup_error%20$backup_name&chat_id=$chat_id" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment