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
94 if ! test -d "$BACKUP_PATH"
96 error "BACKUP_PATH=$BACKUP_PATH doesn't exist!"
100 date=`date "+$DATE_FMT"`
103 log "========================================================================="
104 log "Starting backup for $date at `date '+%Y-%m-%d %H:%M:%S'`"
105 log "========================================================================="
107 for host_path in "$CONFIG_PATH"/*
110 # Load default config and override config if correspond
111 load_config "$host_path/bacaprc"
112 host=`basename "$host_path"`
113 host_backup_path="$BACKUP_PATH/$host"
114 mkdir -p $host_backup_path
115 dst="$BACKUP_PATH/$host/$date"
116 src=`cat "$host_path/paths"`
117 [ "$host" != "$LOCALHOST" ] &&
118 src=`awk "{print \"$host:\"\\$1}" "$host_path/paths"`
119 exclude="$host_path/excludes"
120 include="$host_path/includes"
121 current_link="$host_backup_path/current"
122 current_dir="$host_backup_path/`readlink \"$current_link\"`"
124 log "-----------------------------------------------------------------"
125 log "Backup for host $host started at `date '+%Y-%m-%d %H:%M:%S'`"
126 log "-----------------------------------------------------------------"
128 log "Destination: $dst"
129 log "Last: $current_dir"
131 [ -d "$dst" ] && [ "$FORCE_SYNC" -ne 1 ] &&
132 logerr "$dst already exists, skipping..." &&
134 [ "$PING_CHECK" -eq 1 ] && ! ping_host $host &&
135 logerr "$host is down, skipping..." &&
138 extra_flags="--exclude-from=$exclude"
140 extra_flags="$extra_flags --include-from=$include"
141 log "Running rsync..."
142 $run rsync $RSYNC_FLAGS $extra_flags \
143 --link-dest="$current_dir" $src "$dst/"
150 # Ignore error 24 from rsync ("Partial transfer due to
151 # vanished source files") which is somewhat expected
152 # since we don't do a snapshot
155 # Any other error is important enough
160 if [ $ret -eq $saved_ret ]
162 # Only move current if rsync did not fail, to avoid dangling
164 log "Moving current..."
165 $run rm -f $V "$current_link" ||
167 $run ln -s $V "$date" "$current_link" ||
170 ERROR_HOSTS="$ERROR_HOSTS $host"
174 log "========================================================================="
175 log "Backup for $date finished at `date '+%Y-%m-%d %H:%M:%S'`"
176 log "========================================================================="
181 error "There were some errors when running the backup on: $ERROR_HOSTS"
183 if [ -n "$LOG_FILE" ]
185 error "Please take a look at the log: $LOG_FILE"