Skip to content

Instantly share code, notes, and snippets.

@Eyad-Bereh
Created January 18, 2024 21:09
Show Gist options
  • Save Eyad-Bereh/3392afb77728133b3d199b928ded28bf to your computer and use it in GitHub Desktop.
Save Eyad-Bereh/3392afb77728133b3d199b928ded28bf to your computer and use it in GitHub Desktop.
A simple Bash Script To Backup Your Laravel Storage Folder And Send It To Telegram
#!/bin/bash
TELEGRAM_BOT_TOKEN="YOUR_TELEGRAM_BOT_TOKEN"
TELEGRAM_CHAT_ID="TELEGRAM_CHAT_ID"
TELEGRAM_API_URL="https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendDocument"
STORAGE_PATH="/path/to/storage/folder"
PATH="/tmp/storage-backup" # temporary path to store backups
DATETIME=$(/usr/bin/date "+%F-%H-%M-%S") # get datetime in the format YYYY-MM-DD HH:MM:SS
FILENAME=$(printf "storage-backup-%s" "${DATETIME}") # form file name using the format
ZIP=$(printf "%s.zip" "${FILENAME}") # append ".zip" extension to resultant name
/usr/bin/zip -r -q -s 20m "${PATH}/${ZIP}" "${STORAGE_PATH}" # split the storage folder to several zip files sized 20MB each
shopt -s nullglob
for file in "/tmp/storage-backup/${FILENAME}.z"*; do # loop over each zip chunk and upload it using Telegram API
/usr/bin/curl -s -F "chat_id=${TELEGRAM_CHAT_ID}" -F document=@${file} ${TELEGRAM_API_URL}
done;
shopt -u nullglob
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment