Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 172 lines (138 sloc) 4.765 kb
68b1571 @falconindy Initial commit: extremely alpha
authored
1 #!/bin/bash
2
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
3 DEBUG=true
68b1571 @falconindy Initial commit: extremely alpha
authored
4 debug () {
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
5 [[ $DEBUG ]] && echo -e '\033[1;33m??\033[1;m ' $*
6 }
7
8 info () {
9 echo -e '\033[1;34m::\033[1;m ' $*
68b1571 @falconindy Initial commit: extremely alpha
authored
10 }
11
12 die () {
13 echo -e '\033[1;31m!!\033[1;m ' $*
14 exit 1
15 }
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
16
17 # Declare base options, overwrite with user specs as necessary
18 # MAKE SURE TO CHANGE THIS BEFORE PUSHING PUBLIC
19 CONFIG=/home/haruko/dev/git/squashfu/etc/squashfu
20 source $CONFIG
21 [[ $? -gt 0 ]] && die "Error in config file. Please check your syntax"
22
23 # Informational output w/ happy colors
cd9eab2 @falconindy Less babysitting. Appears to behave better when the entirety is mounted ...
authored
24 mount_union_ro () {
68b1571 @falconindy Initial commit: extremely alpha
authored
25 # Check for the union already being mounted
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
26 grep "${BKUP_ROOT}/rw" /proc/mounts && umount "$BKUP_ROOT/rw"
68b1571 @falconindy Initial commit: extremely alpha
authored
27
28 # build branch string
29 branches="br="
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
30 for i in `seq 7 -1 1`; do
31 branches="${branches}${BKUP_ROOT}/bins/${i}=ro:"
68b1571 @falconindy Initial commit: extremely alpha
authored
32 done
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
33 branches="${branches}${BKUP_ROOT}/ro=ro"
68b1571 @falconindy Initial commit: extremely alpha
authored
34
35 # build and execute mount command
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
36 debug "Mounting union as entirely read only"
68b1571 @falconindy Initial commit: extremely alpha
authored
37 mount -t aufs none $BKUP_ROOT/rw -o udba=reval,$branches
cd9eab2 @falconindy Less babysitting. Appears to behave better when the entirety is mounted ...
authored
38 }
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
39
cd9eab2 @falconindy Less babysitting. Appears to behave better when the entirety is mounted ...
authored
40 mount_union_branch_rw () {
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
41 debug "Remount branch $i as read-write"
42 mount -o remount,mod:bins/$1=rw "${BKUP_ROOT}/rw"
68b1571 @falconindy Initial commit: extremely alpha
authored
43 }
44
45 create_new_seed () {
46 # For our very first seed, we're writing directly to disk, so
47 # Delete the data after the squashed seed has been created
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
48 #[[ "$1" == "--initial" ]] && run_rsync
68b1571 @falconindy Initial commit: extremely alpha
authored
49
50 # Create a new squashfs based on the contents of the union
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
51 debug "Making new squash seed $(basename $SEED)"
68b1571 @falconindy Initial commit: extremely alpha
authored
52 mksquashfs "${BKUP_ROOT}/rw" "$SEED" -b 65536
53
54 # Delete the rsync source since its now squashed
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
55 #[[ "$1" == "--initial" ]] && rm -rf "${BKUP_ROOT}/rw/*"
68b1571 @falconindy Initial commit: extremely alpha
authored
56 }
57
58 move_old_tree () {
59 storage="${BKUP_ROOT}/bkup-$(date +%Y-%m-%d)"
60 mkdir "$storage"
61 cd "$BKUP_ROOT" && mv {$SEED,bins/} "$storage"
62 }
63
64 mount_seed () {
65 debug "Mounting seed"
66 # Mount the squashed seed, failing if we can't
67 mount -o loop,ro "${SEED}" "${BKUP_ROOT}/ro" || {
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
68 die FATAL: Error mounting $SEED;
68b1571 @falconindy Initial commit: extremely alpha
authored
69 }
70 }
71
72 mount_aufs_by_day() {
73 # convert DoW to a number
cd9eab2 @falconindy Less babysitting. Appears to behave better when the entirety is mounted ...
authored
74 mount_union_ro `date --date=$1 +%u`
68b1571 @falconindy Initial commit: extremely alpha
authored
75 }
76
77 run_rsync() {
78 # Gather includes and excludes from config file
79 # No error checking here -- user better not have
80 # effed up the config
81 INCLUDES=($(grep ^#+ $CONFIG | cut -d+ -f2-))
82 EXCLUDES=($(grep ^#- $CONFIG | cut -d- -f2-))
83
84 # rsync source to $BKUP_ROOT/rw
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
85 debug "Rsync executing with:"
86 debug " Options: ${RSYNC_OPTS[@]}"
87 debug " Includes: ${INCLUDES[@]}"
88 debug " Excludes: ${EXCLUDES[@]}"
68b1571 @falconindy Initial commit: extremely alpha
authored
89 rsync ${RSYNC_OPTS[@]} ${INCLUDES[@]} ${EXCLUDES[@]} ${BKUP_ROOT}/rw || return 1
90 }
91
92 # Unmount union and squash
93 unmount_all () {
94 #Union must be unmounted first, or bad things happen
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
95 debug Unmounting union...
cd9eab2 @falconindy Less babysitting. Appears to behave better when the entirety is mounted ...
authored
96 #read -p "Continue..."
68b1571 @falconindy Initial commit: extremely alpha
authored
97 umount "${BKUP_ROOT}/rw"
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
98 debug Unmounting squash...
cd9eab2 @falconindy Less babysitting. Appears to behave better when the entirety is mounted ...
authored
99 #read -p "Continue..."
68b1571 @falconindy Initial commit: extremely alpha
authored
100 umount "$SEED"
101 }
102
103 # Sanity checks
104 # - Are we root?
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
105 [[ $UID -eq 0 ]] || die Must be root!
68b1571 @falconindy Initial commit: extremely alpha
authored
106
107 # - is our BKUP_ROOT valid? (FAIL)
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
108 [[ -w "${BKUP_ROOT}" ]] ||
109 die "Backup root is not accessible. Please check your setting in /etc/squashfu"
68b1571 @falconindy Initial commit: extremely alpha
authored
110
111 # Blindly unmount all just in case
112 unmount_all
113
cd9eab2 @falconindy Less babysitting. Appears to behave better when the entirety is mounted ...
authored
114 #read -p "Continue..."
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
115
68b1571 @falconindy Initial commit: extremely alpha
authored
116 # - do we have a proper (expected) directory structure in place?
117 # Use cd to BKUP_ROOT to avoid issues with brace expansion in a quoted path
118 cd "$BKUP_ROOT" && mkdir -p {rw,ro,bins/{1,2,3,4,5,6,7}}
119
120 # Prep work
121 # - does seed exist? (if not, our backup is creating the seed)
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
122 [[ -f "$SEED" ]] || {
123 debug "No seed found -- creating a new one...";
124 create_new_seed;
68b1571 @falconindy Initial commit: extremely alpha
authored
125 }
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
126
cd9eab2 @falconindy Less babysitting. Appears to behave better when the entirety is mounted ...
authored
127 #read -p "Continue..."
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
128
68b1571 @falconindy Initial commit: extremely alpha
authored
129 # mount seed if it exists and is not already mounted
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
130 grep "${BKUP_ROOT}/ro" /proc/mounts || mount_seed
131
cd9eab2 @falconindy Less babysitting. Appears to behave better when the entirety is mounted ...
authored
132 #read -p "Continue..."
68b1571 @falconindy Initial commit: extremely alpha
authored
133
134 # Prepare union mount with proper bins
cd9eab2 @falconindy Less babysitting. Appears to behave better when the entirety is mounted ...
authored
135 mount_union_ro
136 mount_union_branch_rw $(( $(date +%u) + $MODIFIER ))
68b1571 @falconindy Initial commit: extremely alpha
authored
137
cd9eab2 @falconindy Less babysitting. Appears to behave better when the entirety is mounted ...
authored
138 #read -p "Continue..."
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
139
68b1571 @falconindy Initial commit: extremely alpha
authored
140 # Ready for backup!
141 run_rsync
142
143 # 5) Cleanup
144 # - Is this resquash day? If so, we need a new squash
145 # - If new squash creation fails, we're in trouble. (by default, keep previous week)
146 [[ $(date +%u) -eq $RESQUASH_DAY ]] && {
147 create_new_seed
148 # Set aside last week's tree if user opted to, else delete it all
149 if [[ $KEEP_LAST_WEEK -eq 1 ]]; then
150 move_old_tree
151 else
152 find "${BKUP_ROOT}/bins/" -type f -delete
153 rm $SEED
154 fi
155 }
156
cd9eab2 @falconindy Less babysitting. Appears to behave better when the entirety is mounted ...
authored
157 #read -p "Continue..."
68b1571 @falconindy Initial commit: extremely alpha
authored
158 unmount_all
159
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
160 # Do another sanity check -- check sizes of bins versus squash.
161 bin_size=$(du -s ${BKUP_ROOT}/bins | awk '{print $1}')
cd9eab2 @falconindy Less babysitting. Appears to behave better when the entirety is mounted ...
authored
162 sfs_size=$(du -s $SEED | awk '{print $1}')
163 if [[ $bin_size -gt $sfs_size ]]; then
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
164 info "Your incrementals are larger than your seed! You might consider resquashing your backup with $0 --resquash"
165 fi
166
68b1571 @falconindy Initial commit: extremely alpha
authored
167 # 6) Optional behavior
168 # --seed-initial Create new seed
169 # --rollback $1 $2 Rollback to the day specified by $1, mounting at $2
170 # --resquash Create a new seed
171 #
Something went wrong with that request. Please try again.