Hi All
It's been a long time since I've done any scripting. I'd value your opinions on this (very simple) backup script. I've tested it, and it does what I want which is:
- Backup files in (currently 2) directories on my raid partition.
- Not run if the USB storage is not attached (so I don't fill up /)
- Overwrite changed files without preserving earlier copies.
- Keep backups of deleted files
- Log activities to a file.
Would appreciate your comments before I commit it to the root crontab. In particular have I done anything here that could give rise to a fault that could crash the server if something doesn't work as expected?Code:#!/bin/bash # # test file ROOT_UID=0 # Only users with $UID 0 have root privileges. E_NOTROOT=67 # Non-root exit error. # List folders to be backed up here srcdirs="/raid/users /raid/media" if [ "$UID" -ne "$ROOT_UID" ] then echo "Must be root to run this script." exit $E_NOTROOT fi starttime="$(date -u)" echo $starttime Backup started >> backup.log mount -t ext4 -L backup01 /mnt/backup/ if [ $? -ne 0 ]; then echo "$(date -u)" Failed to mount backup disk. Backup FAILED >> backup.log exit else echo "$(date -u)" backup disk mounted >> backup.log fi for srcdir in $srcdirs do stats="$(rsync -ah --info=stats2 $srcdir /mnt/backup/ | grep transferred)" files="$(echo $stats | awk '{ print $6 }')" bytes="$(echo $stats | awk '{ print $11 }')" echo "$(date -u)" Backup of $srcdir: $files files, $bytes bytes copied >> backup.log done diskfree="$(df -h --sync /mnt/backup/ | grep backup | awk '{ print $4 }')" echo "$(date -u) All backups completed succesfully. $diskfree free on backup01" >> backup.log sync umount /mnt/backup echo "$(date -u) Backup completed, drive umounted" >> backup.log
Also am thinking of writing the backup.log to a folder on my web server so I can access it without logging in to the server directly. Is this a bad idea?
Bookmarks