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="-aAXHx --numeric-ids --delete"
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
53 source "/etc/bacaprc" 2> /dev/null
54 source "/etc/bacap/bacaprc" 2> /dev/null
55 source "$SCRIPT_DIR/bacaprc" 2> /dev/null
56 test -n "$BACAPRC" && source "$BACAPRC"
66 RSYNC_FLAGS="$RSYNC_FLAGS $RSYNC_VERBOSE_FLAGS"
71 exec 1>>"$LOG_FILE" &&
88 ping -c1 "$1" > /dev/null 2>&1
91 date=`date "+$DATE_FMT"`
94 plog "========================================================================="
95 plog "Starting backup for $date at `date '+%Y-%m-%d %H:%M:%S'`"
96 plog "========================================================================="
98 for host_path in "$CONFIG_PATH"/*
101 # Load default config and override config if correspond
103 source "$host_path/bacaprc" 2>/dev/null
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 plog "-----------------------------------------------------------------"
117 plog "Backup for host $host"
118 plog "-----------------------------------------------------------------"
120 plog "Destination: $dst"
121 plog "Last: $current_dir"
123 [ -d "$dst" ] && [ "$FORCE_SYNC" -ne 1 ] &&
124 perror "$dst already exists, skipping..." &&
126 [ "$PING_CHECK" -eq 1 ] && ! ping_host $host &&
127 perror "$host is down, skipping..." &&
130 extra_flags="--exclude-from=$exclude"
132 extra_flags="$extra_flags --include-from=$include"
133 plog "Running rsync..."
134 $run rsync $RSYNC_FLAGS $extra_flags \
135 --link-dest="$current_dir" $src "$dst/" ||
137 plog "Moving current..."
138 $run rm -f $V "$current_link" ||
140 $run ln -s $V "$date" "$current_link" ||
142 if [ $ret -ne $saved_ret ]
144 ERROR_HOSTS="$ERROR_HOSTS $host"
148 plog "========================================================================="
149 plog "Backup for $date finished at `date '+%Y-%m-%d %H:%M:%S'`"
150 plog "========================================================================="
155 pout "There were some errors when running the backup on: $ERROR_HOSTS"
157 if [ -n "$LOG_FILE" ]
159 pout "Please take a look at the log: $LOG_FILE"