Skip to content

Instantly share code, notes, and snippets.

@danielbmeireles
Created May 21, 2025 09:48
Show Gist options
  • Save danielbmeireles/654c5e59142393400f6519b379febd0c to your computer and use it in GitHub Desktop.
Save danielbmeireles/654c5e59142393400f6519b379febd0c to your computer and use it in GitHub Desktop.
Backup script
#!/bin/bash
# Timestamp
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
# Configuration
BACKUP_DIR="/run/media/daniel/Backup"
HOME_DIR="$HOME"
LOG_FILE="$BACKUP_DIR/backup.$TIMESTAMP.log"
# Check if backup drive is mounted
if [ ! -d "$BACKUP_DIR" ]; then
echo "Backup drive not mounted at $BACKUP_DIR. Exiting."
exit 1
fi
echo "Backup started at $TIMESTAMP" | tee -a "$LOG_FILE"
# Backup home directory (excluding large/unnecessary folders)
echo "Backing up home directory..." | tee -a "$LOG_FILE"
rsync -aAXvz \
--exclude='.cache' \
--exclude=".local/share/Trash" \
--exclude="Dropbox" \
--exclude="OneDrive" \
--exclude="maestral-venv" \
--exclude="git-credential-manager" \
"$HOME_DIR/" "$BACKUP_DIR/home/" | tee -a "$LOG_FILE"
# Backup important system configurations
echo "Backing up system configurations..." | tee -a "$LOG_FILE"
rsync -aAXvz /etc/ "$BACKUP_DIR/etc/" | tee -a "$LOG_FILE"
# Backup list of installed packages
echo "Backing up package lists..." | tee -a "$LOG_FILE"
mkdir -p "$BACKUP_DIR/packages"
if command -v apt &> /dev/null; then
# Debian/Ubuntu
dpkg --get-selections > "$BACKUP_DIR/packages/dpkg_selections.txt"
elif command -v dnf &> /dev/null; then
# Fedora/RHEL
dnf list --installed > "$BACKUP_DIR/packages/dnf_installed.txt"
dnf repoquery --userinstalled > "$BACKUP_DIR/packages/dnf_userinstalled.txt"
dnf group list --installed > "$BACKUP_DIR/packages/dnf_group_installed.txt"
fi
# Backup Flatpak applications
if command -v flatpak &> /dev/null; then
echo "Backing up Flatpak list..." | tee -a "$LOG_FILE"
mkdir -p "$BACKUP_DIR/flatpak"
flatpak list --app --columns=application > "$BACKUP_DIR/flatpak/flatpak_apps.txt"
fi
# Backup Brew applications
if command -v brew &> /dev/null; then
echo "Backing up Brewfile..." | tee -a "$LOG_FILE"
mkdir -p "$BACKUP_DIR/brew"
brew bundle dump --file "$BACKUP_DIR/brew/Brewfile" -f
fi
echo "Backup completed at $(date +"%Y-%m-%d_%H-%M-%S")" | tee -a "$LOG_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment