rsync backup script
Posted: 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
/home/sector11/bin/rsync_home_exclude.txt
/home/sector11/bin/rsync_m5_exclude.txt - same for others now unused.
In action, may as well haven't done it today ...
That's it ... thanks to luc and others.
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
Code: Select all
.cache
.fontconfig
lost+found
.s11bk-logs
.thumbnails
.Trash-0
.Trash-1000
Code: Select all
.Trash-0
.Trash-1000
lost+found
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 ~
$