Backup script with zenity notifications
Kaip ir žadėjau, rašyti pradėsiu anglų kalba
Introduction
As an efficient programmer, I have set up a backup script that I trigger using keyboard shortcuts (e.g., Ctrl+Shift+B). This script runs in the background and notifies me of its progress and completion using zenity
notifications. This automation ensures that my important directories and files are backed up consistently without manual intervention.
In this script, you can find references to other scripts (older versions) that I've discussed in detail on my blog. [1] [2] [3]
Šis atsarginės kopijos (backup) scenarijus skirtas automatizuoti svarbių katalogų ir failų atsarginį kopijavimą. Kiekvienas žingsnis yra registruojamas log faile, o pasibaigus veiksmams, naudotojas informuojamas per zenity
programą apie operacijos statusą ir detales.
Purpose
This backup script is designed to automate the process of backing up important directories and files. It logs each step to a log file and, upon completion, uses zenity
to inform the user about the operation's status and details.
How It Works
- Configuration File:
- All variables, such as paths and directories to be backed up, are stored in a separate configuration file (
backup_config.sh
). This allows the script to be flexible and easily updatable.
- All variables, such as paths and directories to be backed up, are stored in a separate configuration file (
- Logging:
- The script logs each step to both a main log file (
backup.log
) and a temporary session log file. The session log file is used to track actions performed during the current session.
- The script logs each step to both a main log file (
- Initial Scripts:
- The script runs initial scripts (
binbash.sh
andbotulis.sh
for several directories) based on parameters specified in the configuration file.
- The script runs initial scripts (
- Audiobook Synchronization:
- The
sync_audio_books
function checks the existence of the audiobook directory, counts the number of books, calculates the total size, and writes this information to a markdown file.
- The
- Notification:
- After the backup process completes, a
zenity
notification is shown, summarizing the actions taken during the current session, clearly informing the user.
- After the backup process completes, a
- Temporary Log File Cleanup:
- The session log file is deleted after the notification is shown to keep the system clean.
Usage
- Ensure
zenity
is installed on your system. It is available in most Linux distribution package repositories. - Prepare the configuration file (
backup_config.sh
) with the necessary variables:
# Configuration file
AUDIOBOOKS_DIR="/run/media/vaidotak/1f3e2462-ef6f-47a1-a3f8-9300d4bec261/audiobooks/"
OUTPUT_FILE="$HOME/Obsidian/notes/audio_knygos_sync_su_telefonu.md"
LOG_FILE="$HOME/bin/logs/backup.log"
BIN_PATH="$HOME/bin"
BACKUP_TARGETS=("$HOME/bin" "$HOME/Obsidian" "$HOME/Keepass")
./backup.sh /path/to/your/backup_config.sh
#!/bin/bash
# Load the configuration file
CONFIG_FILE="${1:-$HOME/bin/backup_config.sh}"
if [ ! -f "$CONFIG_FILE" ]; then
echo "Configuration file not found: $CONFIG_FILE"
exit 1
fi
source "$CONFIG_FILE"
session_log_file="/tmp/backup_$(date +%Y-%m-%d_%H-%M-%S).log"
# Function to log messages to both the main log file and the session log file
log() {
echo "$(date +%Y-%m-%d\ %H:%M:%S) - $1" | tee -a "$LOG_FILE" >> "$session_log_file"
}
log "Backup process started."
# Run initial scripts
initial_scripts=("binbash.sh" "botulis.sh")
for script in "${initial_scripts[@]}"; do
"$BIN_PATH/$script"
log "Ran $script."
done
# Run botulis.sh for each target directory
for target in "${BACKUP_TARGETS[@]}"; do
"$BIN_PATH/botulis.sh" "$target"
log "Ran botulis.sh for $target."
done
sync_audio_books
log "sync_audio_books function executed."
# Function to synchronize audiobooks
sync_audio_books() {
log "Starting audiobook synchronization."
# Check if the audiobook directory exists
if [ ! -d "$AUDIOBOOKS_DIR" ]; then
echo "Directory $AUDIOBOOKS_DIR does not exist!"
log "Directory $AUDIOBOOKS_DIR does not exist!"
return 1
fi
# Count the number of books
num_books=$(find "$AUDIOBOOKS_DIR" -mindepth 1 -maxdepth 1 -type d | wc -l)
log "Number of books: $num_books."
# Calculate the total size of the directory in gigabytes
total_size_gb=$(du -sh "$AUDIOBOOKS_DIR" | cut -f1)
log "Total directory size: $total_size_gb."
# Create a file and write initial information
echo "Found $num_books books ($total_size_gb):" > "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
# Get the list of directories with their sizes
index=1
for dir in "$AUDIOBOOKS_DIR"/*; do
if [ -d "$dir" ]; then
dir_size=$(du -sh "$dir" | cut -f1)
dir_name=$(basename "$dir")
echo "$index. $dir_name - $dir_size" >> "$OUTPUT_FILE"
log "Directory: $dir_name, Size: $dir_size."
((index++))
fi
done
echo "List saved to $OUTPUT_FILE"
log "List saved to $OUTPUT_FILE."
}
# Function to show a notification
show_notification() {
zenity --info --title="Backup Status" --text="$1"
}
# Show a notification with the current session log entries
log_content=$(cat "$session_log_file")
show_notification "Backup process completed. Actions taken:\n$log_content"
# Delete the temporary log file
rm "$session_log_file"
Advantages
- Automation: Automates repetitive backup tasks, ensuring consistency and saving time.
- Logging: Provides detailed records of each step of the backup process.
- User-Friendly Notifications: Uses
zenity
for notifications, clearly and concisely informing the user about the backup process. - Flexibility: Uses a configuration file for easy updating and customization of settings.
Disadvantages
- Dependency on
zenity
: Requireszenity
to be installed for notifications to work. - Single Session Logging: Uses a temporary log file for session logs, meaning logs are not retained beyond the current session.
Improvements
- Email Notifications: Add functionality to send email notifications with log summaries.
- Incremental Backups: Implement incremental backups to save time and disk space.
- Error Handling: Improve error handling to better manage failures and retries.
- Remote Backups: Add support for backing up data to a remote server or cloud storage.
Conclusion
This script is a powerful tool for automating backups and providing user-friendly notifications. By separating configuration into a dedicated file and logging each step, it ensures both flexibility and transparency. While there are areas for improvement, such as enhanced error handling and support for remote backups, this script provides a solid foundation for efficiently automating and managing backups.
Komentarai
Rašyti komentarą