4 # Default config values
12 # Don't actually do anything, just print the commands
15 # Force synchronization, even when the target already exist
18 # Log file (if empty, print to stdout/err)
21 # Where to find the configuration of the hosts to backup
22 CONFIG_PATH=/etc/bacap/hosts
24 # Name of the local host (so no ssh would be used with this host)
27 # Where to put the backups
30 # Date format used for backed up directories (passed to the date command)
33 # Ping remote hosts to check if they are up (set to 0 if your hosts don't
34 # reply to ICMP pings).
38 RSYNC_FLAGS="-aAHSXx --numeric-ids"
40 # rsync flags to use when in verbose mode
41 RSYNC_VERBOSE_FLAGS="-v --stats"
43 # rsync remote shell to use
48 SCRIPT_DIR=$(dirname `readlink -f $0`)
51 # Load configuration files
54 source "/etc/bacaprc" 2> /dev/null
55 source "/etc/bacap/bacaprc" 2> /dev/null
56 source "$SCRIPT_DIR/bacaprc" 2> /dev/null
57 test -n "$BACAPRC" && source "$BACAPRC"
58 test -n "$extra_config" && source "$1" 2> /dev/null
64 RSYNC_FLAGS="$RSYNC_FLAGS $RSYNC_VERBOSE_FLAGS"
69 exec 1>>"$LOG_FILE" &&
89 ping -c1 "$1" > /dev/null 2>&1
92 date=`date "+$DATE_FMT"`
95 log "========================================================================="
96 log "Starting backup for $date at `date '+%Y-%m-%d %H:%M:%S'`"
97 log "========================================================================="
99 for host_path in "$CONFIG_PATH"/*
102 # Load default config and override config if correspond
103 load_config "$host_path/bacaprc"
104 host=`basename "$host_path"`
105 host_backup_path="$BACKUP_PATH/$host"
106 mkdir -p $host_backup_path
107 dst="$BACKUP_PATH/$host/$date"
108 src=`cat "$host_path/paths"`
109 [ "$host" != "$LOCALHOST" ] &&
110 src=`awk "{print \"$host:\"\\$1}" "$host_path/paths"`
111 exclude="$host_path/excludes"
112 include="$host_path/includes"
113 current_link="$host_backup_path/current"
114 current_dir="$host_backup_path/`readlink \"$current_link\"`"
116 log "-----------------------------------------------------------------"
117 log "Backup for host $host started at `date '+%Y-%m-%d %H:%M:%S'`"
118 log "-----------------------------------------------------------------"
120 log "Destination: $dst"
121 log "Last: $current_dir"
123 [ -d "$dst" ] && [ "$FORCE_SYNC" -ne 1 ] &&
124 logerr "$dst already exists, skipping..." &&
126 [ "$PING_CHECK" -eq 1 ] && ! ping_host $host &&
127 logerr "$host is down, skipping..." &&
130 extra_flags="--exclude-from=$exclude"
132 extra_flags="$extra_flags --include-from=$include"
133 log "Running rsync..."
134 $run rsync $RSYNC_FLAGS $extra_flags \
135 --link-dest="$current_dir" $src "$dst/"
142 # Ignore error 24 from rsync ("Partial transfer due to
143 # vanished source files") which is somewhat expected
144 # since we don't do a snapshot
147 # Any other error is important enough
152 if [ $ret -eq $saved_ret ]
154 # Only move current if rsync did not fail, to avoid dangling
156 log "Moving current..."
157 $run rm -f $V "$current_link" ||
159 $run ln -s $V "$date" "$current_link" ||
162 ERROR_HOSTS="$ERROR_HOSTS $host"
166 log "========================================================================="
167 log "Backup for $date finished at `date '+%Y-%m-%d %H:%M:%S'`"
168 log "========================================================================="
173 error "There were some errors when running the backup on: $ERROR_HOSTS"
175 if [ -n "$LOG_FILE" ]
177 error "Please take a look at the log: $LOG_FILE"