Created
March 3, 2011 00:12
-
-
Save pyther/852065 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# Backup Script - Matthew Gyurgyik ([email protected]) | |
# | |
# Backup a System using rsync and --link-dest (hard links) | |
# Backups are stored in a folder called $Year$Month$Day ex. 2010Oct18 | |
# | |
# Exclude file format: | |
# /backup/exclude.txt | |
# + /etc | |
# + /home | |
# + /srv | |
# - /* | |
# | |
# Due to the nature of hard links, only one copy of the file is stored on the | |
# backup drive. | |
# Edit These | |
backupdir="/backup" | |
mountpoint="/backup" | |
srcdir="/" | |
dstdir=$backupdir/$(date +%Y%h%d) | |
excludeFile="$backupdir/exclude.txt" | |
lastbackup="$backupdir/last.txt" | |
rsync_opts=(--archive --delete-excluded --numeric-ids --human-readable "--exclude-from=$excludeFile") | |
# Root check | |
if (( $UID != 0 )); then | |
echo "This script must be run as root" 1>&2 | |
exit 1 | |
fi | |
# Try to mount the drive | |
# If unsuccessful quit | |
if mountpoint -q "$mountpoint"; then | |
echo "$mountpoint is mounted" | |
else | |
echo "$mountpoint is not mounted" | |
if ! mount "$mountpoint"; then | |
echo "Error mounting drive... BAILING" | |
exit 1 | |
fi | |
fi | |
# Sanity Checks | |
# if directory exists we don't want to overwrite it | |
if [[ -d $dstdir ]]; then | |
echo "Destination Directory Already Exists!" | |
echo "Terminating!" | |
exit 1 | |
elif [[ ! -d $srcdir ]]; then | |
echo "No source directory" | |
exit 1 | |
fi | |
if [[ -f $lastbackup ]]; then | |
read linkdir </backup/last.txt | |
rsync_opts+=("--link-dest=$linkdir") | |
else | |
echo "No previous backups found." | |
fi | |
echo "Rsync Options: ${rsync_opts[@]}" | |
echo "Destination Directory: $dstdir" | |
echo "Linking Directory: $linkdir" | |
# Make Dest Dir | |
mkdir -p "$dstdir" | |
# Lets run the command | |
# If successful store dest dir into lastbackup (file) | |
# Else tell the user and exit 1 | |
if /usr/bin/rsync "${rsync_opts[@]}" "$srcdir" "$dstdir"; then | |
echo "$dstdir" > "$lastbackup" | |
else | |
echo "Rsync exited with status: $?" | |
echo "Backup is considered incomplete." | |
exit 1 | |
fi | |
echo "$(df -h $mountpoint)" | |
echo "Unmounting Drive" | |
umount "$mountpoint" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
...
else
printf "%s\n" "$mountpoint is not mounted" "trying to mount.."
mount $mountpoint || { printf "%s\n" "Error mounting drive... BAILING"; exit 1; }
fi
...
if /usr/bin/rsync $rsync_opts $srcdir $dstdir; then
[[ $1 == "full" ]] && echo "$dstdir" > "$lastfull"
echo "$dstdir" > "$lastinc";
else
printf "%s\n" "Rysnc exited with status: $?" "Backup is considered incomplete."
fi
I would also quote all dirs (mountpoint, destdir, etc)
and things like
rsync_opts+=" --link-dest="$linkdir
could be
rsync_opts+=" --link-dest=$linkdir"
also ls can sort files according to ctime with -c option, so you could get away with having to write a file with the lastinc and/or lastfull , but alternatively do something like
head -1 < <(ls -1tc)
I may use this :D