3 ms·
I use this little bash script (as a daily cron job) to selectively mirror directories on two hard drives in case I lose one (very poor man's backup solution). I
by Xichekolas 17y ago
I use this little bash script (as a daily cron job) to selectively mirror directories on two hard drives in case I lose one (very poor man's backup solution). I imagine it could be used for deploying code (modified for remote machine of course):
#!/bin/sh
RSYNC="/usr/bin/rsync" # Verify with 'which rsync'
DIRS="/home/xich /etc" # Directories to be backed up.
TARGET="/mnt/secondhd/backup" # Directory into which all backups are placed.
# For instance, if TARGET is /mnt/secondhd/backup and DIRS is "/home/user1 /home/user2"
# then after running, there will exist a /mnt/secondhd/backup/user1 and
# /mnt/secondhd/backup/user2. DO NOT use trailing slashes for any of these paths,
# as that will change the behavior of rsync.
LOGFILE="/var/log/mirror_hds.log" # For errors only.
for dir in $DIRS; do
INEX=""
if [[ -e "$dir/.mirror_include" ]]; then
INEX="--include-from $dir/.mirror_include"
fi
if [[ -e "$dir/.mirror_exclude" ]]; then
INEX="$INEX --exclude-from $dir/.mirror_exclude"
fi
$RSYNC -av --delete $INEX $dir $TARGET &> $LOGFILE
done
The .mirror_include and .mirror_exclude files are just newline-delimited lists of file masks (they do what you would expect). I did it like this so each user can modify his own exclusion/inclusion lists (the file belongs to the user), and doesn't have to mess with the cron script (which belongs to root). Inclusion takes precedence. As an example, my exclude file:
.* # any file that starts with a period
tv # my tv shows directory
movies
And my include file:
.mirror_include # since this would be filtered out above
.mirror_exclude
.conkyrc