Skip to content

Instantly share code, notes, and snippets.

@jarulsamy
Last active February 22, 2024 18:21
Show Gist options
  • Save jarulsamy/164fff300fe0facb9c2ed4bb1f124473 to your computer and use it in GitHub Desktop.
Save jarulsamy/164fff300fe0facb9c2ed4bb1f124473 to your computer and use it in GitHub Desktop.
Linux backup home directory daily.
#!/usr/bin/env bash
#
# Backup home directory
#
#
set -o errexit
set -o nounset
set -o pipefail
readonly SOURCE_DIR="/home/joshua"
readonly BACKUP_DIR="/mnt/sega0/backups/home"
readonly DATETIME="$(date '+%Y-%m-%d_%H-%M-%S')"
readonly BACKUP_PATH="${BACKUP_DIR}/${DATETIME}"
readonly LATEST_LINK="${BACKUP_DIR}/latest"
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
mkdir -p "${BACKUP_DIR}"
rsync -ahv --progress --delete \
"${SOURCE_DIR}/" \
--link-dest "${LATEST_LINK}" \
--exclude=".cache" \
--exclude=".cpan" \
--exclude=".npm" \
--exclude=".vim" \
--exclude=".vscode-server" \
"${BACKUP_PATH}"
rm -rf "${LATEST_LINK}"
ln -s "${BACKUP_PATH}" "${LATEST_LINK}"
# /etc/systemd/system/home-backup.service
[Unit]
Description=Backup entire user home directory to other disk.
After=network-online.target
[Service]
Type=oneshot
WorkingDirectory=/home/joshua/auto_backup
ExecStart=/bin/bash /home/joshua/auto_backup/backup.sh
RemainAfterExit=False
StandardOutput=journal
[Install]
WantedBy=multi-user.target
# /etc/systemd/system/home-backup.timer
[Unit]
Description=Backup home directory daily.
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment