-
-
Save mcxiaoke/bca0e5ed7b4fa83a5ebc329deba68abf to your computer and use it in GitHub Desktop.
Linux backup automation
This file contains hidden or 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
# Systemd environment file for backup.sh daily backup service | |
# Place in /etc/backup.env | |
# Destination for rsync file system clone | |
BACKUP_DEST=/media/large1/backups/raw | |
# Destination for restic repository | |
RESTIC_REPOSITORY=/media/large1/backups/restic | |
# Restic password file | |
RESTIC_PASSWORD_FILE=/root/.restic.pass | |
# Exclude file for rsync --exclude-from | |
EXCLUDE_FILE=/etc/backup.exclude | |
# Directories to back up | |
BACKUP_DIRS="/home;/etc" | |
This file contains hidden or 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
.cargo/registry | |
.toolchain | |
target | |
go/pkg | |
go/src | |
.cache | |
.mozilla | |
This file contains hidden or 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
# Systemd unit for backup.sh service | |
# Place in /etc/systemd/system/backup.service | |
[Unit] | |
Description=Daily backup service | |
[Service] | |
Type=oneshot | |
User=root | |
EnvironmentFile=/etc/backup.env | |
ExecStart=/usr/bin/nice -n 19 /usr/bin/ionice -c2 -n7 /usr/sbin/backup.sh | |
RemainAfterExit=true | |
[Install] | |
WantedBy=multi-user.target | |
This file contains hidden or 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
#!/bin/bash | |
# Local backup script, adapted from https://webworxshop.com/centralised-backups-with-restic-and-rsync/ | |
# Place in /usr/sbin/backup.sh | |
# Enable with `systemctl enable backup.timer` | |
# Check status with `systemctl status backup.service` | |
set -e | |
# Check variables | |
if [ -z "$BACKUP_DEST" ]; then | |
echo "BACKUP_DEST must be set" | |
exit 1 | |
fi | |
if [ -z "$RESTIC_REPOSITORY" ]; then | |
echo "RESTIC_REPOSITORY must be set" | |
exit 1 | |
fi | |
if [ -z "$RESTIC_PASSWORD_FILE" ]; then | |
echo "RESTIC_PASSWORD_FILE must be set" | |
exit 1 | |
fi | |
if [ -Z "$BACKUP_DIRS" ]; then | |
echo "BACKUP_DIRS must be set" | |
exit 1 | |
fi | |
if [! -Z "$EXCLUDE_FILE" ]; then | |
EXCLUDES="--exclude-from=$EXCLUDE_FILE" | |
fi | |
# Split dirs using semicolons | |
DIRS=$(echo $BACKUP_DIRS | tr ";" "\n") | |
function do_rsync() { | |
# Write start info to log file | |
echo "Starting rsync backup job $1 to $BACKUP_DEST$1 at $(date '+%Y-%m-%d %H:%M:%S')..." | |
# Ensure backup directory exists | |
mkdir -p $BACKUP_DEST$1 | |
# Execute rsync | |
/usr/bin/rsync -aP --delete $EXCLUDES $1 $BACKUP_DEST | |
# Write end-info to log file | |
echo "Rsync job finished at $(date '+%Y-%m-%d %H:%M:%S')." | |
echo "Done" | |
} | |
echo "Starting rsync clone" | |
for D in $DIRS; do | |
do_rsync $D | |
done | |
# copy apt list | |
dpkg --get-selections > $BACKUP_DEST/packages.list | |
echo "Starting restic backup" | |
# Check database | |
restic check | |
# Execute backups | |
for D in $DIRS; do | |
restic backup $BACKUP_DEST/$D | |
done | |
# Clean up old snapshots | |
restic forget -d 7 -w 4 -m 6 -y 2 | |
# Re-check the database | |
restic check | |
This file contains hidden or 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
# Systemd timer for backup.sh service | |
# Place in /etc/systemd/system/backup.timer | |
[Unit] | |
Description=Daily backup service | |
[Timer] | |
OnCalendar=*-*-* 4:00:00 | |
[Install] | |
WantedBy=timers.target |
This file contains hidden or 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
#!/bin/bash | |
# Helper script to install the backup service | |
# Copy tool | |
cp backup.sh /usr/sbin/ | |
# Copy configuration | |
cp backup.env backup.exclude /etc/ | |
# Copy units | |
cp backup.service backup.timer /etc/systemd/system | |
# Install backup service | |
systemctl enable backup.service | |
systemctl enable backup.timer | |
# Start timer | |
systemctl start backup.timer | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment