thinkhdaps v0.2
[thpani.git] / backup
blob66cfff25563545029957ced9ad02a2e443f0f7c6
1 #!/bin/bash
3 # Copyright (c) 2007-2008 Thomas Pani <thomas.pani/at/gmail.com>
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 # THE SOFTWARE.
23 ### EDIT THESE THREE LINES TO MATCH YOUR SETUP!!! ###
24 # backup source
25 SRC="/home/thomas"
26 # backup destination
27 # (hostname and source will be added automatically)
28 DST="/media/PANIHDD/backup"
29 # file containing a list of files/dirs to exclude
30 # (see `man rsync' on --exclude-from for more info)
31 EXCLUDES="/home/thomas/.backup_excludes"
32 ##### DO NOT EDIT ANYTHING BELOW HERE #####
33 ### (unless you know what you're doing) ###
35 VERSION="0.1"
36 echo "Backup script v${VERSION}."
37 echo
39 date=$(date "+%Y-%m-%d__%H-%M-%S")
40 BAK_DST="${DST}/${HOSTNAME}/${SRC//\//_}"
42 # make sure we're running as root
43 if [ $(id -u) != 0 ] ; then
44 echo "Sorry, must be root. Exiting..."
45 exit 1
48 # make sure backup destination is mounted
49 if [ ! -d $DST ] ; then
50 echo "Please mount backup media. Exiting..."
51 exit 1
54 # create backup dir
55 if [ ! -d $BAK_DST ] ; then
56 mkdir -p $BAK_DST
59 # list changes
60 echo "Here's a list of changes:"
61 echo
62 RSYNC_OUTPUT=$(rsync -n -a -i -x -P --delete --delete-excluded --exclude-from="$EXCLUDES" --link-dest=${BAK_DST}/current/ ${SRC}/ ${BAK_DST}/backup-${date} | grep -v ^cd)
63 if [ $(echo "$RSYNC_OUTPUT" | wc -l ${TMPFILE} | cut -d ' ' -f1) -gt 22 ] ; then
64 echo "$RSYNC_OUTPUT" | less
65 else
66 echo "$RSYNC_OUTPUT"
69 echo
70 echo -n "Would you like to sync these changes? [y/n] "
71 read answer
73 case $answer in
74 'y' ) echo ;;
75 'n' ) echo "Exiting..." ; exit ;;
76 * ) echo "Invalid answer. Exiting..." ; exit 1 ;;
77 esac
79 # rsync
80 rsync -a -i -x -P --delete --delete-excluded --exclude-from="$EXCLUDES" --link-dest=${BAK_DST}/current/ ${SRC}/ ${BAK_DST}/backup-${date} | grep -v ^cd
81 if [ $? -ne 0 ] ; then
82 echo "Error during rsync. Exiting..."
83 exit 1
86 # safe link to current backup
87 if [ -L ${BAK_DST}/current ] ; then
88 rm ${BAK_DST}/current
90 ln -s backup-${date} ${BAK_DST}/current
92 # horray!
93 echo
94 echo "Finished backup to ${BAK_DST}"
95 echo
96 du -sch $BAK_DST/*