| 1 | # make backup using the RSYNC method |
| 2 | # This file is part of Relax-and-Recover, licensed under the GNU General |
| 3 | # Public License. Refer to the included COPYING for full text of license. |
| 4 | |
| 5 | local backup_prog_rc |
| 6 | local backup_log_message |
| 7 | |
| 8 | local host path |
| 9 | |
| 10 | host="$(rsync_host "$BACKUP_URL")" |
| 11 | path="$(rsync_path "$BACKUP_URL")" |
| 12 | |
| 13 | Log "Include list:" |
| 14 | while read -r ; do |
| 15 | Log " $REPLY" |
| 16 | done < $TMP_DIR/backup-include.txt |
| 17 | Log "Exclude list:" |
| 18 | while read -r ; do |
| 19 | Log " $REPLY" |
| 20 | done < $TMP_DIR/backup-exclude.txt |
| 21 | |
| 22 | LogPrint "Creating $BACKUP_PROG backup on '${host}:${path}'" |
| 23 | |
| 24 | ProgressStart "Running backup operation" |
| 25 | ( |
| 26 | case "$(basename $BACKUP_PROG)" in |
| 27 | |
| 28 | (rsync) |
| 29 | # We are in a subshell, so this change will not propagate to later scripts |
| 30 | BACKUP_RSYNC_OPTIONS+=( --one-file-system --delete --exclude-from=$TMP_DIR/backup-exclude.txt --delete-excluded ) |
| 31 | |
| 32 | case $(rsync_proto "$BACKUP_URL") in |
| 33 | |
| 34 | (ssh) |
| 35 | Log $BACKUP_PROG "${BACKUP_RSYNC_OPTIONS[@]}" $(cat $TMP_DIR/backup-include.txt) "$(rsync_remote_full "$BACKUP_URL")/backup" |
| 36 | $BACKUP_PROG "${BACKUP_RSYNC_OPTIONS[@]}" $(cat $TMP_DIR/backup-include.txt) \ |
| 37 | "$(rsync_remote_full "$BACKUP_URL")/backup" |
| 38 | ;; |
| 39 | |
| 40 | (rsync) |
| 41 | $BACKUP_PROG "${BACKUP_RSYNC_OPTIONS[@]}" $(cat $TMP_DIR/backup-include.txt) \ |
| 42 | "$(rsync_remote_full "$BACKUP_URL")/backup" |
| 43 | ;; |
| 44 | |
| 45 | esac |
| 46 | ;; |
| 47 | |
| 48 | (*) |
| 49 | # no other backup programs foreseen than rsync so far |
| 50 | : |
| 51 | ;; |
| 52 | |
| 53 | esac |
| 54 | echo $? >$TMP_DIR/retval |
| 55 | ) >"${TMP_DIR}/${BACKUP_PROG_ARCHIVE}.log" & |
| 56 | BackupPID=$! |
| 57 | starttime=$SECONDS |
| 58 | |
| 59 | sleep 3 # Give the backup software a good chance to start working |
| 60 | |
| 61 | get_size() { |
| 62 | echo $(stat --format '%s' "/$1" 2>/dev/null) |
| 63 | } |
| 64 | |
| 65 | check_remote_df() { |
| 66 | echo $(ssh $(rsync_remote_ssh "$BACKUP_URL") df -P ${path} 2>/dev/null | tail -1 | awk '{print $5}' | sed -e 's/%//') |
| 67 | } |
| 68 | |
| 69 | check_remote_du() { |
| 70 | x=$(ssh $(rsync_remote_ssh "$BACKUP_URL") du -sb $(rsync_path_full "$BACKUP_URL")/backup 2>/dev/null | awk '{print $1}') |
| 71 | [[ -z "${x}" ]] && x=0 |
| 72 | echo $x |
| 73 | } |
| 74 | |
| 75 | # make sure that we don't fall for an old size info |
| 76 | unset size |
| 77 | # while the backup runs in a sub-process, display some progress information to the user |
| 78 | test "$PROGRESS_WAIT_SECONDS" || PROGRESS_WAIT_SECONDS=1 |
| 79 | case "$(basename $BACKUP_PROG)" in |
| 80 | |
| 81 | (rsync) |
| 82 | ofile="" |
| 83 | i=0 |
| 84 | while sleep $PROGRESS_WAIT_SECONDS ; kill -0 $BackupPID 2>/dev/null ; do |
| 85 | i=$((i+1)) |
| 86 | [[ $i -gt 300 ]] && i=0 |
| 87 | case $i in |
| 88 | |
| 89 | 300) |
| 90 | [[ $(check_remote_df) -eq 100 ]] && Error "Disk is full on system ${host}" |
| 91 | ;; |
| 92 | |
| 93 | 15|30|45|60|75|90|105|120|135|150|165|180|195|210|225|240|255|270|285) |
| 94 | size=$(check_remote_du) |
| 95 | ;; |
| 96 | |
| 97 | * ) |
| 98 | nfile="$(tail -1 "${TMP_DIR}/${BACKUP_PROG_ARCHIVE}.log")" |
| 99 | #fsize="$(get_size $(tail -1 "${TMP_DIR}/${BACKUP_PROG_ARCHIVE}.log"))" |
| 100 | [[ "$nfile" != "$ofile" ]] && { |
| 101 | fsize="$(get_size "$nfile")" |
| 102 | size=$((size+fsize)) |
| 103 | ofile="$nfile" |
| 104 | } |
| 105 | ;; |
| 106 | esac |
| 107 | |
| 108 | ProgressInfo "Backed up $((size/1024/1024)) MiB [avg $((size/1024/(SECONDS-starttime))) KiB/sec]" |
| 109 | done |
| 110 | ;; |
| 111 | |
| 112 | (*) |
| 113 | ProgressInfo "Archiving" |
| 114 | while sleep $PROGRESS_WAIT_SECONDS ; kill -0 $BackupPID 2>/dev/null ; do |
| 115 | ProgressStep |
| 116 | done |
| 117 | ;; |
| 118 | |
| 119 | esac |
| 120 | ProgressStop |
| 121 | |
| 122 | wait $BackupPID |
| 123 | |
| 124 | transfertime="$((SECONDS-starttime))" |
| 125 | backup_prog_rc="$(cat $TMP_DIR/retval)" |
| 126 | |
| 127 | sleep 1 |
| 128 | # everyone should see this warning, even if not verbose |
| 129 | test "$backup_prog_rc" -gt 0 && Error " |
| 130 | There was an error (${rsync_err_msg[$backup_prog_rc]}) during backup creation. |
| 131 | Please check the destination and see '$RUNTIME_LOGFILE' for more information. |
| 132 | |
| 133 | If the error is related to files that cannot and should not be saved by |
| 134 | $BACKUP_PROG, they should be excluded from the backup. |
| 135 | |
| 136 | " |
| 137 | |
| 138 | backup_log_message="$(tail -14 ${TMP_DIR}/${BACKUP_PROG_ARCHIVE}.log)" |
| 139 | if [ $backup_prog_rc -eq 0 -a "$backup_log_message" ] ; then |
| 140 | LogPrint "$backup_log_message in $transfertime seconds." |
| 141 | elif [ "$size" ]; then |
| 142 | LogPrint "Backed up $((size/1024/1024)) MiB in $((transfertime)) seconds [avg $((size/1024/transfertime)) KiB/sec]" |
| 143 | fi |
| 144 |