NEW: option to skip backups in a short time interval

Adds the `--delta-time/-d` option. Using it one can specify a time in seconds. All backups initiated in a shorter interval than the set one, are skipped.

This is very useful for cron jobs.
This commit is contained in:
Jean-Claude 2020-12-29 17:14:30 +01:00
parent 6a9014ebef
commit 265701e9e3
Signed by: jeanclaude
GPG Key ID: 8A300F57CBB9F63E
2 changed files with 60 additions and 15 deletions

View File

@ -8,11 +8,14 @@ On macOS, it has a few disadvantages compared to Time Machine - in particular it
## Installation
git clone https://github.com/laurent22/rsync-time-backup
```
git clone https://github.com/laurent22/rsync-time-backup
```
## Usage
Usage: rsync_tmbackup.sh [OPTION]... <[USER@HOST:]SOURCE> <[USER@HOST:]DESTINATION> [exclude-pattern-file]
```
Usage: rsync_tmbackup.sh [OPTION]... <[USER@HOST:]SOURCE> <[USER@HOST:]DESTINATION> [exclude-pattern-file]
Options
-p, --port SSH port.
@ -31,6 +34,10 @@ On macOS, it has a few disadvantages compared to Time Machine - in particular it
After 365 days keep one backup every 30 days.
--no-auto-expire Disable automatically deleting backups when out of space. Instead an error
is logged, and the backup is aborted.
-d, --delta-time Specify the minimal time in seconds between the last successful backup and
the currently initiated one. If the time difference is shorter than the
required delta time, the backup is terminated.
```
## Features
@ -51,27 +58,42 @@ On macOS, it has a few disadvantages compared to Time Machine - in particular it
* "latest" symlink that points to the latest successful backup.
## Examples
* Backup the home folder to backup_drive
rsync_tmbackup.sh /home /mnt/backup_drive
```
rsync_tmbackup.sh /home /mnt/backup_drive
```
* Backup with exclusion list:
rsync_tmbackup.sh /home /mnt/backup_drive excluded_patterns.txt
```
rsync_tmbackup.sh /home /mnt/backup_drive excluded_patterns.txt
```
* Backup to remote drive over SSH, on port 2222:
rsync_tmbackup.sh -p 2222 /home user@example.com:/mnt/backup_drive
```
rsync_tmbackup.sh -p 2222 /home user@example.com:/mnt/backup_drive
```
* Backup from remote drive over SSH:
rsync_tmbackup.sh user@example.com:/home /mnt/backup_drive
```
rsync_tmbackup.sh user@example.com:/home /mnt/backup_drive
```
* To mimic Time Machine's behaviour, a cron script can be setup to backup at regular interval. For example, the following cron job checks if the drive "/mnt/backup" is currently connected and, if it is, starts the backup. It does this check every 1 hour.
0 */1 * * * if grep -qs /mnt/backup /proc/mounts; then rsync_tmbackup.sh /home /mnt/backup; fi
* To mimic Time Machine's behaviour, a cron script can be setup to backup at regular interval. For example, the following cron job checks if the drive `/mnt/backup` is currently connected and, if it is, starts the backup. It does this check every 1 hour.
```
0 */1 * * * if grep -qs /mnt/backup /proc/mounts; then rsync_tmbackup.sh /home /mnt/backup; fi
```
* To backup an external drive which is mounted only erratically we can use a frequently running crone job together with the `--delta-time` option of `rsync_tmbackup.sh`. The following crone job checks if the drive `/mnt/mydrive` is connected and backups it in case it has not been backup for more than 1 hour. In order to detect when the drive is available, it checks this every minute.
```
*/1 * * * * if grep -qs /mnt/mydrive /proc/mounts; then rsync_tmbackup.sh --delta-time 3600 /mnt/mydrive /mnt/backup; fi
```
## Backup expiration logic
@ -100,8 +122,8 @@ To display the rsync options that are used for backup, run `./rsync_tmbackup.sh
## No automatic backup expiration
An option to disable the default behaviour to purge old backups when out of space. This option is set with the `--no-auto-expire` flag.
## How to restore
The script creates a backup in a regular directory so you can simply copy the files back to the original directory. You could do that with something like `rsync -aP /path/to/last/backup/ /path/to/restore/to/`. Consider using the `--dry-run` option to check what exactly is going to be copied. Use `--delete` if you also want to delete files that exist in the destination but not in the backup (obviously extra care must be taken when using this option).

View File

@ -51,6 +51,9 @@ fn_display_usage() {
echo " After 365 days keep one backup every 30 days."
echo " --no-auto-expire Disable automatically deleting backups when out of space. Instead an error"
echo " is logged, and the backup is aborted."
echo " -d, --delta-time Specify the minimal time in seconds between the last successful backup and"
echo " the currently initiated one. If the time difference is shorter than the"
echo " required delta time, the backup is terminated."
echo ""
echo "For more detailed help, please see the README file:"
echo ""
@ -280,6 +283,7 @@ LOG_DIR="${XDG_DATA_HOME:-$HOME}/$APPNAME"
AUTO_DELETE_LOG="1"
EXPIRATION_STRATEGY="1:1 30:7 365:30"
AUTO_EXPIRE="1"
DELTA_TIME="0"
RSYNC_FLAGS="-D --numeric-ids --links --hard-links --one-file-system --itemize-changes --times --recursive --perms --owner --group --stats --human-readable"
@ -322,6 +326,10 @@ while :; do
--no-auto-expire)
AUTO_EXPIRE="0"
;;
-d|--delta-time)
shift
DELTA_TIME=$1
;;
--)
shift
SRC_FOLDER="$1"
@ -508,6 +516,21 @@ while : ; do
# If the path is relative, it needs to be relative to the destination. To keep
# it simple, just use an absolute path. See http://serverfault.com/a/210058/118679
PREVIOUS_DEST="$(fn_get_absolute_path "$PREVIOUS_DEST")"
# Check if it is time for a new backup
latest_stamp=$(fn_parse_date $(basename "$PREVIOUS_DEST"))
now_stamp=$(date +%s)
diff=$(( now_stamp - latest_stamp))
if (( diff < DELTA_TIME )); then
fn_log_info "$diff seconds have passed since the last backup. But a minimal delta time of $DELTA_TIME is configured. Exiting"
fn_log_info ""
exit 1
fi
fn_log_info "Previous backup is $diff seconds ago"
fn_log_info "Previous backup found - doing incremental backup from $SSH_DEST_FOLDER_PREFIX$PREVIOUS_DEST"
LINK_DEST_OPTION="--link-dest='$PREVIOUS_DEST'"
fi