This project contains a Bash script that extracts daily logs from a specified systemd service and automatically backs them up to this GitHub repository. It is designed for system administrators, developers, and DevOps teams who want a simple, auditable way to preserve logs over time.
- ✅ Extracts logs from any systemd unit using
journalctl - ✅ Saves logs to timestamped
.logfiles - ✅ Skips empty logs automatically
- ✅ Commits new logs to Git
- ✅ Pushes changes to the
mainbranch of a GitHub repository
Copy and paste this into a file using nano save_logs.sh:
#!/bin/bash
# === Configuration ===
UNIT_NAME="your_service_name" # ← Replace with your systemd service name
REPO_DIR="$HOME/github-log-backups" # ← Local path to your cloned GitHub repo
LOG_DIR="$REPO_DIR/logs"
# Ensure the log directory exists
mkdir -p "$LOG_DIR"
cd "$REPO_DIR" || exit 1
# === Timestamps ===
NOW_HUMAN=$(date "+%Y-%m-%d 00:00:00")
YESTERDAY_HUMAN=$(date -d "1 day ago" "+%Y-%m-%d 00:00:00")
NOW_FILENAME=$(date "+%Y-%m-%d")
YESTERDAY_FILENAME=$(date -d "1 day ago" "+%Y-%m-%d")
# === Log file path ===
LOG_FILE="$LOG_DIR/${UNIT_NAME}_logs_${YESTERDAY_FILENAME}_to_${NOW_FILENAME}.log"
echo "📄 Saving logs from $YESTERDAY_HUMAN to $NOW_HUMAN into $LOG_FILE"
# === Collect logs ===
sudo journalctl -u "$UNIT_NAME" --since "$YESTERDAY_HUMAN" --until "$NOW_HUMAN" --no-pager -o short-iso > "$LOG_FILE"
# === Skip if log file is empty ===
if [ ! -s "$LOG_FILE" ]; then
echo "⚠️ Log file is empty. Removing it."
rm -f "$LOG_FILE"
exit 0
fi
# === Git: Commit and Push ===
echo "✅ Logs saved. Committing to GitHub."
# Set Git identity if needed (optional for automation)
git config pull.rebase true
git config user.name "log-bot"
git config user.email "[email protected]"
git add "$LOG_FILE"
git commit -m "Add ${UNIT_NAME} logs: ${YESTERDAY_FILENAME} to ${NOW_FILENAME}"
git pull --rebase origin main
git push origin mainCreate a private GitHub repository named for example: log-backups. Do not initialize it with a README.
⚠️ Make sure your GitHub account has an SSH key added, and you're using the[email protected]:...SSH format.
git clone [email protected]:yourusername/log-backups.git ~/github-log-backups
cd ~/github-log-backupschmod +x save_logs.shUpdate these variables at the top of the script:
UNIT_NAME="your_service_name" # e.g. nginx, docker, your-custom-service
REPO_DIR="$HOME/github-log-backups" # path to this repoeval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsaEnsure your SSH public key is added to GitHub.
- The script uses
journalctlto pull logs from the past 24 hours. - It saves them to a file named like:
logs/your_service_name_logs_2025-06-08_to_2025-06-09.log - If the log is not empty, it commits the file to Git and pushes it to GitHub.
To run the script every day at midnight:
- Open crontab:
crontab -e- Add this line:
0 0 * * * /bin/bash $HOME/github-log-backups/save_logs.sh >> /var/log/save_logs.log 2>&1This will run the script daily and write any output to /var/log/save_logs.log.
If you want to run the script without sudo:
sudo usermod -aG systemd-journal $USERThen log out and log back in.
logs/
├── nginx_logs_2025-06-08_to_2025-06-09.log
├── nginx_logs_2025-06-09_to_2025-06-10.log