-
-
Save simplerethink/850836a37b75c55da09fe061cc2340ef to your computer and use it in GitHub Desktop.
Linux backup home directory daily.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /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