[Hampshire] Re: Automating rsync to run as root

Top Page

Reply to this message
Author: Russell Gadd
Date:  
To: hampshire lug
Subject: [Hampshire] Re: Automating rsync to run as root
Hi again,

Stuart wrote:

>For the sake of completeness (and because I am sat around with little
>else to do today) something like this:
>mount -t vfat /dev/hda7 /mnt/backup -o uid=500 (and even gid=500)
>
>although the -t vfat may not be necessary
>this means that user 500 (or whatever you choose) owns all the files on
>that filesystem - you no longer need root privs to write to it.

.......
>ps despite the length, it would have been nice to see the script anyway.

.......

I changed the mount options as you suggested (I did this in fstab) and I can
now use rsync without resorting to root privileges. Thanks - this is the
best solution to my original problem.


Philip wrote:

>If the script is too long for mail message, you could post it to a
>website, and link to it here. Alternatively, it sounds like you doing
>something a bit different to how others may do it. I think that
>makes it a good case to add to the HantsLUG Wiki. I know you
>describe yourself as a 'green horn' but I for one am interested in
>how you are managing your backups.

.........

Ok I'll copy the script below (not sure what the HantsLUG Wiki is).

A few comments first. This is my first (currently also my latest!) script.
You will see I used sed to strip commas from my filespecs - this was part of
an attempt to try to deal with filespecs with spaces in, but my rudimentary
bash skills failed me - I couldn't get tar to accept what I gave it when
there were spaces in. I have resolved not to use spaces in names from now
on. The script might need a bit more tweaking for robustness, but it does
work ok.

Note "my" and XPdataD are partitions on HD1 and XPdataE is a partition on
HD0 (where all the OS partitions are). All my OS's can see XPdataD and
XPdataE and so can write backups to these. I've also got a couple of other
partitions (one on each drive) with the OS images on. These are also backed
up to DVD. This means if one disc fails the data for all the systems is
always on the other one (this has happened to me - a Maxtor drive got too
hot). The USB stick and some weekly backup CDR's contain backups of my
personal files only. If the PC screws up both drives the USB stick may work
(or may not) - I used to use CDRWs but found these unreliable. If the house
goes up in flames I have my CDR's and the insurance to buy another PC and
start with a new install of the OS's using text files with all my setup
notes on.

At present my goal is to be able to boot into either Windows or Linux and do
useful work and back up whichever system I use before I shut down. These
backups have to be simple - i.e. hit a desktop icon, so I don't get lazy and
not do it and also I can expect my wife to do it when she has used the PC.
Taking system images to back up non-personal files is however an ad-hoc
thing I do when I think I have changed the system a fair bit. In Windows I
have a desktop link to a "system changes" document to record changes made so
I can redo these if I need to. I still need to organise documentation for
Linux.

I'm happy to write this scheme up in a more structured way if anyone thinks
it may be useful.

In running this script rsync actually takes a while at first, but if run
subsequent times is very quick. At first I thought this was a little
worrying - what I mean by that is that I might restart the PC and run
Windows and do some changes to the content of the vfat directories - will
rsync catch these or assume it knows the latest state of these directories?
I think it should be ok as it works across networks so presumably can't make
too many assumptions. This is borne out by rebooting and immediately going
back into Debian without running Windows: rsync then again takes a while to
run, so it seems well behaved.

======================================================
Here's the script:

#! /bin/bash
# dailybak
# creates compressed archive of my folder
# copies to /home/russell/XPdataD/backup/daily/Debian
# then clones /home/russell/XPdataD/backup/daily to USB stick
# then clones /home/russell/XPDataD to /home/russell/XPDataE/Dclone

shopt -s -o nounset

declare LOGDIR=/home/russell/my/backups/logs
declare BAKDIR=/home/russell/my/backups

#======================================================================================
#separate the file or dir specs below by commas - can't have spaces in
filenames!
declare BACKUPDIRS="/home/russell/my"
declare EXCLUDEDIRS="/home/russell/my/.Trash-russell,$BAKDIR"
declare EXCLUDEFILES="*.tmp"
#======================================================================================
declare BAKDIRS
declare EXDIRS
declare EXFILS
declare OK

declare DATESTAMP=`date +"%Y%m%d_%H%M%S"`
declare LOGFILE=$LOGDIR/${DATESTAMP}_backup.log
declare BAKFILE=$BAKDIR/Debian_backup.tar.gz

if [ ! -z "$BACKUPDIRS" ]
then
    BAKDIRS=`echo $BACKUPDIRS| sed 's/,[ ]*/ /g'`
else
    echo "ERROR:Nothing specified to backup!"
    exit 0
fi


#======================================================================================
# remove commas and add exclude command
# (may as well have just used spaces instead of commas in the spec!)
if [ ! -z "$EXCLUDEDIRS" ]
then
    EXDIRS=`echo '--exclude='$EXCLUDEDIRS | sed 's/,[ ]*/ --exclude=/g'`
fi


if [ ! -z "$EXCLUDEFILES" ]
then
    EXFILS=`echo '--exclude='$EXCLUDEFILES | sed 's/,[ ]*/ --exclude=/g'`
fi
#======================================================================================


#echo "BAKDIRS=<"$BAKDIRS">"
#echo "EXDIRS=<"$EXDIRS">"
#echo "EXFILS=<"$EXFILS">"
#echo "BAKFILE=<"$BAKFILE">"

#set -x #shows commands being executed on the terminal for debugging
purposes

printf "\nBacking up Debian 'my' directory...\n\n"

declare TAROPTIONS="${EXDIRS} ${EXFILS} --dereference --ignore-case
--ignore-failed-read --no-anchor --preserve-permissions --verbose --totals"

tar -cz $TAROPTIONS --file=$BAKFILE $BAKDIRS >$LOGFILE

printf "\nCopying backup to XPdataD...\n"
cp $BAKFILE /home/russell/XPdataD/backup/daily/Debian

printf "\nCloning backup to USB stick...\n"
rsync -rltD --delete --modify-window=3601
/home/russell/XPdataD/backup/daily/ /media/usb0

printf "\nCloning XPdataD to XPdataE...\n"
rsync -a --delete --modify-window=3601 /home/russell/XPdataD/
/home/russell/XPdataE/Dclone


#set +x

printf "\n==============================="
printf "\nDONE - PLEASE CLOSE THIS WINDOW"

exit 0