2 # backupninja handler to do incremental backups using
3 # rsync and hardlinks, based on
5 # http://www.mikerubel.org/computers/rsync_snapshots/
7 # feedback: rhatto at riseup.net | gpl
13 # log = rsync log file
14 # partition = partition where the backup lives
15 # fsck = set to 1 if fsck should run on $partition after the backup is made
16 # read_only = set to 1 if $partition is mounted read-only
17 # mountpoint = backup partition mountpoint or backup main folder
18 # backupdir = folder relative do $mountpoint where the backup should be stored
19 # days = number of backup increments (min = 5)
20 # lockfile = lockfile to be kept during backup execution
23 # include = include folder on backup
24 # exclude = exclude folder on backup
25 # from = local or remote
26 # ssh = ssh command line (remote only)
27 # rsync = rsync command line
28 # exclude_vserver = vserver-name (valid only if vservers = yes on backupninja.conf)
31 # initscripts = absolute path where scripts are located
32 # service = script name to be stoped at the begining of the backup and started at its end
34 # You dont need to manually specify vservers using "include = /vservers".
35 # They are automatically backed-up if vserver is set to "yes" on your backupninja.conf.
39 getconf log /var/log/backupninja-rub.log
51 getconf rsync "rsync -av --delete"
57 getconf exclude_vserver
63 backupdir="$mountpoint/$backupdir"
65 if [ ! -d "$backupdir" ]; then
66 error "Backupdir $backupdir does not exist"
70 if [ -z "$days" ]; then
73 keep="`echo $days - 1 | bc -l`"
76 if [ ! -z "$lockfile" ]; then
77 touch $lockfile || warning "Could not create lockfile $lockfile"
80 for path in $exclude; do
81 EXCLUDES="$EXCLUDES --exclude=$path"
84 if [ ! -z "$service" ]; then
85 for daemon in $service; do
86 info "Stopping service $daemon..."
87 $initscripts/$daemon stop
93 # please use an absolute path
95 if [[ "$2" < 4 ]]; then
96 error "Rotate: minimum of 4 rotations"
100 if [ -d $1.$2 ]; then
104 for ((n=`echo "$2 - 1" | bc`; n >= 0; n--)); do
105 if [ -d $1.$n ]; then
106 dest=`echo "$n + 1" | bc`
112 if [ -d $1.tmp ]; then
122 echo "Starting backup at `date`" >> $log
124 if [ "$read_only" == "1" ] || [ "$read_only" == "yes" ]; then
125 if [ -d "$mountpoint" ]; then
126 mount -o remount,rw $mountpoint
128 error "Could not mount $mountpoint"
134 if [ "$vservers_are_available" == "yes" ]; then
136 # sane permission on backup
137 mkdir -p $backupdir/$VROOTDIR
138 chmod 000 $backupdir/$VROOTDIR
140 for candidate in `ls $VROOTDIR`; do
141 found_excluded_vserver="0"
142 if [ "$candidate" != "lost+found" ]; then
143 for excluded_vserver in $exclude_vserver; do
144 if [ "$excluded_vserver" == "$candidate" ]; then
145 found_excluded_vserver="1"
149 if [ "$found_excluded_vserver" == "0" ]; then
150 include="$include $VROOTDIR/$candidate"
156 for SECTION in $include; do
158 section="`basename $SECTION`"
160 if [ ! -d "$backupdir/$SECTION/$section.0" ]; then
161 mkdir -p $backupdir/$SECTION/$section.0
164 info "Rotating $backupdir/$SECTION/$section..."
165 echo "Rotating $backupdir/$SECTION/$section..." >> $log
166 rotate $backupdir/$SECTION/$section $keep
167 info "Syncing $SECTION on $backupdir/$SECTION/$section.0..."
169 if [ "$from" == "local" ]; then
170 debug $rsync $EXCLUDES /$SECTION/ $backupdir/$SECTION/$section.0/
171 $rsync $EXCLUDES /$SECTION/ $backupdir/$SECTION/$section.0/ >> $log
172 if [ "$?" != "0" ]; then
173 warning "Rsync error when trying to transfer $SECTION"
175 elif [ "$from" == "remote" ]; then
176 if [ -z "$user" ] || [ -z "$host" ]; then
177 error "Config file error: either user or host was not specified"
180 debug $rsync $EXCLUDES -e "$ssh" $user@$host:/$SECTION/ $backupdir/$SECTION/$section.0
181 $rsync $EXCLUDES -e "$ssh" $user@$host:/$SECTION/ $backupdir/$SECTION/$section.0 >> $log
182 if [ "$?" != "0" ]; then
183 warning "Rsync error when trying to transfer $SECTION"
187 error "Invalid source $from"
191 touch $backupdir/$SECTION/$section.0
195 if [ "$read_only" == "1" ] || [ "$read_only" == "yes" ]; then
196 mount -o remount,ro $mountpoint
199 if [ "$fsck" == "1" ] || [ "$fsck" == "yes" ]; then
202 warning "Could not umount $mountpoint to run fsck"
204 fsck -v -y $partition >> $log
209 if [ ! -z "$service" ]; then
210 for daemon in $service; do
211 info "Starting service $daemon..."
212 $initscripts/$daemon start
216 if [ ! -z "$lockfile" ]; then
217 rm $lockfile || warning "Could not remove lockfile $lockfile"
220 echo "Finnishing backup at `date`" >> $log