rsync backup script

Forum rules
Share your brain ;)
User avatar
Sector11
Sperminator
Posts: 193
Joined: Mon Aug 19, 2013 4:15 am

rsync backup script

Unread post by Sector11 » Mon Jan 26, 2015 9:13 pm

Not sure where else to put this so here goes.

I don't like "compressed" backups, I've had a few fail. This little script has never failed me.

It does a backup of my home partition with come exclusions and my personal data partition - /dev/sda5 -> /media/5 to an External USB HDD

It has more comment lines than actual lines as it developed from something simpler to what it is with the help of a few over at #!, namely luc´ for one.

I call it "s11bk" and there are Debian SID versions and Debian Stable versions as they read my external HDD differently

s11bk

Code: Select all

#!/bin/bash
## 12 Jan 2012
## 23 Feb changed to 'rsync' from 'cp'
## --exclude-from '/home/sector11/bin/rsync_home_exclude.txt'
## --exclude-from '/home/sector11/bin/rsync_m5_exclude.txt'
## --exclude-from '/home/sector11/bin/rsync_m6_exclude.txt' ### not used anymore
## --exclude-from '/home/sector11/bin/rsync_m8_exclude.txt' ### not used anymore

# -a, --archive		This is equivalent to −rlptgoD. It is a quick way of saying
#							you want recursion and want to preserve almost everything
#							(with −H being a notable omission). The only exception to
#                    the above equivalence is when −−files−from is specified, in
#                    which case −r is not implied. Note that −a does not
#							preserve hardlinks, because finding multiply−linked files
#							is expensive. You must separately specify −H.
# -r, --recursive:	recurse into directories
# −n, --dry−run:		perform a trial run with no changes made
# −t, --times:			preserve modification times
# −p, --perms:			This option causes the receiving rsync
#							to set the destination permissions
# −o, --owner:			preserve owner (super−user only)
# -g, --group:			preserve group
# -v, --verbose:		increase verbosity
# -h,                    format sizes to be more readable
# --progress:			show progress during transfer
# --delete:				delete extraneous files from dest dirs
# -l, --links			copy symlinks as symlinks
# -H, --hard−links	preserve hard links
# -s, --protect−args
# -z, --compress              compress file data during the transfer
#     --compress-level=NUM    explicitly set compression level

# 24 Feb 2012
# rsync "cannot delete non-empty directory:"
# http://hardforum.com/showthread.php?t=1526242
# Answer: Adding --delete-excluded fixes this issue

MON=%b
###### make sure ~/.s11bk-logs exists #####
mkdir -p /home/sector11/.s11bk-logs

##################################################### Debian Stable TARGET ###
TARGET="/media/disk/S11-$(date +"$MON")"
######################################################## Debian SID TARGET ###
#TARGET="/media/sector11/disk/S11-$(date +"$MON")"
##############################################################################

echo "Syncing /home     to $TARGET/"
  rsync -avh --progress --delete --delete-excluded --exclude-from '/home/sector11/bin/rsync_home_exclude.txt' /home/sector11/ > "/home/sector11/.s11bk-logs/Home_$(date +%Y·%m·%d·%H·%M·%S).txt" "$TARGET"
#  rsync -avh --progress --delete --delete-excluded --exclude-from '/home/sector11/bin/rsync_home_exclude.txt' "$TARGET"
#  rsync -avh --delete --delete-excluded --exclude-from '/home/sector11/bin/rsync_home_exclude.txt' "$TARGET"
#echo ""

echo "Syncing /media/5/ to /media/disk/M5/"
########################################################### SID
# rsync -avh --progress --delete --delete-excluded --exclude-from '/home/sector11/bin/rsync_m5_exclude.txt' /media/5/ /media/sector11/disk/M5 > "/home/sector11/.s11bk-logs/M5_$(date +%Y·%m·%d·%H·%M·%S).txt"
# rsync -avh --progress --delete --delete-excluded --exclude-from '/home/sector11/bin/rsync_m5_exclude.txt' /media/5/ /media/sector11/disk/M5 > "/home/sector11/.s11bk-logs/M5_$(date +%Y·%m·%d·%H·%M·%S).txt"
########################################################### Stable
rsync -avh --progress --delete --delete-excluded --exclude-from '/home/sector11/bin/rsync_m5_exclude.txt' /media/5/ /media/disk/M5 > "/home/sector11/.s11bk-logs/M5_$(date +%Y·%m·%d·%H·%M·%S).txt"
#rsync -avh --delete --delete-excluded --exclude-from '/home/sector11/bin/rsync_m5_exclude.txt' /media/5/
#rsync -avh --progress --delete --delete-excluded --exclude-from '/home/sector11/bin/rsync_m5_exclude.txt' /media/5/

#echo ""
## echo "Syncing /media/8/ to /media/disk/M8/"
## SID
## rsync -avh --progress --delete --delete-excluded --exclude-from '/home/sector11/bin/rsync_m8_exclude.txt' /media/8/ /media/sector11/disk/M8 > "/home/sector11/.s11bk-logs/M8_$(date +%Y·%m·%d·%H·%M·%S).txt"
## Stable
# rsync -avh --progress --delete --delete-excluded --exclude-from '/home/sector11/bin/rsync_m8_exclude.txt' /media/8/ /media/disk/M8 > "/home/sector11/.s11bk-logs/M8_$(date +%Y·%m·%d·%H·%M·%S).txt"
## (sleep 10s && umount /media/disk)
(sleep 10s && cd ~/.s11bk-logs && rm *.txt && cd ..)
echo ""
echo "1. Syncing Complete"
read -p "2. Unmount /media/disk? (Y|n) " yn
case $yn in
[Yy]* ) umount /media/disk;;
[Nn]* ) echo "/media/disk still mounted" && exit;;
esac
echo "/media/disk Unmounted - Unplug now."
exit
/home/sector11/bin/rsync_home_exclude.txt

Code: Select all

.cache
.fontconfig
lost+found
.s11bk-logs
.thumbnails
.Trash-0
.Trash-1000
/home/sector11/bin/rsync_m5_exclude.txt - same for others now unused.

Code: Select all

.Trash-0
.Trash-1000
lost+found
In action, may as well haven't done it today ...

Code: Select all

 26 Jan 15 | 17:08:59 ~
    $ s11bk
Syncing /home     to /media/disk/S11-Jan/
Syncing /media/5/ to /media/disk/M5/

1. Syncing Complete
2. Unmount /media/disk? (Y|n) 
/media/disk Unmounted - Unplug now.
 
 26 Jan 15 | 17:11:21 ~
    $ 
That's it ... thanks to luc and others.
I am THE resident noob, hands down no discussion. ... and
I wear my soap on a rope.

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: rsync backup script

Unread post by wuxmedia » Mon Jan 26, 2015 9:22 pm

umount as well? how spoilt I say, just spoilt :p
nice share.
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
Sector11
Sperminator
Posts: 193
Joined: Mon Aug 19, 2013 4:15 am

Re: rsync backup script

Unread post by Sector11 » Mon Jan 26, 2015 9:52 pm

Tha umount was something I picked up here or directly from machinebacon I believe. That ending has only been a part of the script for the past 3-4 months.

Hope others find it helpful.
I am THE resident noob, hands down no discussion. ... and
I wear my soap on a rope.

User avatar
Sector11
Sperminator
Posts: 193
Joined: Mon Aug 19, 2013 4:15 am

Re: rsync backup script

Unread post by Sector11 » Mon Jan 26, 2015 9:53 pm

That umount was something I picked up here or directly from machinebacon I believe. That ending has only been a part of the script for the past 3-4 months.

But the lines to do the yes/no took some research and playing with a fake script to get it working. I was really pleased with myself when I did that.

Hope others find it helpful.
I am THE resident noob, hands down no discussion. ... and
I wear my soap on a rope.

Post Reply