• Stars
    star
    3,290
  • Rank 13,088 (Top 0.3 %)
  • Language
    Shell
  • Created over 10 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Time Machine style backup with rsync.

Rsync time backup

This script offers Time Machine-style backup using rsync. It creates incremental backups of files and directories to the destination of your choice. The backups are structured in a way that makes it easy to recover any file at any point in time.

It works on Linux, macOS and Windows (via WSL or Cygwin). The main advantage over Time Machine is the flexibility as it can backup from/to any filesystem and works on any platform. You can also backup, for example, to a Truecrypt drive without any problem.

On macOS, it has a few disadvantages compared to Time Machine - in particular it does not auto-start when the backup drive is plugged (though it can be achieved using a launch agent), it requires some knowledge of the command line, and no specific GUI is provided to restore files. Instead files can be restored by using any file explorer, including Finder, or the command line.

Installation

git clone https://github.com/laurent22/rsync-time-backup

Usage

Usage: rsync_tmbackup.sh [OPTION]... <[USER@HOST:]SOURCE> <[USER@HOST:]DESTINATION> [exclude-pattern-file]

Options
 -p, --port             SSH port.
 -h, --help             Display this help message.
 -i, --id_rsa           Specify the private ssh key to use.
 --rsync-get-flags      Display the default rsync flags that are used for backup. If using remote
                        drive over SSH, --compress will be added.
 --rsync-set-flags      Set the rsync flags that are going to be used for backup.
 --rsync-append-flags   Append the rsync flags that are going to be used for backup.
 --log-dir              Set the log file directory. If this flag is set, generated files will
                        not be managed by the script - in particular they will not be
                        automatically deleted.
                        Default: /home/backuper/.rsync_tmbackup
 --strategy             Set the expiration strategy. Default: "1:1 30:7 365:30" means after one
                        day, keep one backup per day. After 30 days, keep one backup every 7 days.
                        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.

Features

  • Each backup is on its own folder named after the current timestamp. Files can be copied and restored directly, without any intermediate tool.

  • Backup to/from remote destinations over SSH.

  • Files that haven't changed from one backup to the next are hard-linked to the previous backup so take very little extra space.

  • Safety check - the backup will only happen if the destination has explicitly been marked as a backup destination.

  • Resume feature - if a backup has failed or was interrupted, the tool will resume from there on the next backup.

  • Exclude file - support for pattern-based exclusion via the --exclude-from rsync parameter.

  • Automatically purge old backups - within 24 hours, all backups are kept. Within one month, the most recent backup for each day is kept. For all previous backups, the most recent of each month is kept.

  • "latest" symlink that points to the latest successful backup.

Examples

  • Backup the home folder to backup_drive

      rsync_tmbackup.sh /home /mnt/backup_drive  
    
  • Backup with exclusion list:

      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 [email protected]:/mnt/backup_drive
    
  • Backup from remote drive over SSH:

      rsync_tmbackup.sh [email protected]:/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
    

Backup expiration logic

Backup sets are automatically deleted following a simple expiration strategy defined with the --strategy flag. This strategy is a series of time intervals with each item being defined as x:y, which means "after x days, keep one backup every y days". The default strategy is 1:1 30:7 365:30, which means:

  • After 1 day, keep one backup every 1 day (1:1).
  • After 30 days, keep one backup every 7 days (30:7).
  • After 365 days, keep one backup every 30 days (365:30).

Before the first interval (i.e. by default within the first 24h) it is implied that all backup sets are kept. Additionally, if the backup destination directory is full, the oldest backups are deleted until enough space is available.

Exclusion file

An optional exclude file can be provided as a third parameter. It should be compatible with the --exclude-from parameter of rsync. See this tutorial for more information.

Built-in lock

The script is designed so that only one backup operation can be active for a given directory. If a new backup operation is started while another is still active (i.e. it has not finished yet), the new one will be automaticalled interrupted. Thanks to this the use of flock to run the script is not necessary.

Rsync options

To display the rsync options that are used for backup, run ./rsync_tmbackup.sh --rsync-get-flags. It is also possible to add or remove options using the --rsync-append-flags or --rsync-set-flags option. For example, to exclude backing up permissions and groups:

rsync_tmbackup --rsync-append-flags "--no-perms --no-group" /src /dest

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).

Extensions

  • rtb-wrapper: Allows creating backup profiles in config files. Handles both backup and restore operations.
  • time-travel: Smooth integration into OSX Notification Center

TODO

  • Check source and destination file-system (df -T /dest). If one of them is FAT, use the --modify-window rsync parameter (see man rsync) with a value of 1 or 2
  • Add --whole-file arguments on Windows? See http://superuser.com/a/905415/73619
  • Minor changes (see TODO comments in the source).

LICENSE

The MIT License (MIT)

Copyright (c) 2013-2018 Laurent Cozic

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

joplin

Joplin - the secure note taking and to-do app with synchronisation capabilities for Windows, macOS, Linux, Android and iOS.
TypeScript
42,734
star
2

massren

massren - easily rename multiple files using your text editor
Go
1,306
star
3

joplin-android

Android releases for Joplin
234
star
4

so-sql-injections

SQL injection vulnerabilities in Stack Overflow PHP questions
PHP
161
star
5

ripple

Ripple is a REST API library for the Go programming language. Demo is available below:
Go
138
star
6

wslpath

wslpath - Converts Unix and Windows format paths in WSL
PHP
92
star
7

fastcopy

FastCopy
C++
65
star
8

pmcctv

pmcctv - poor man's CCTV system in Go
Go
56
star
9

toml-go

Toml parser in Go
Go
47
star
10

joplin-nextcloud

Joplin Web API for Nextcloud
JavaScript
45
star
11

appetizer

Official repository for Appetizer, the open source application launcher, or dock, for Windows.
C++
36
star
12

go-sqlkv

An SQL-based key/value store for Golang
Go
36
star
13

ical-go

iCal package for Go (Golang)
Go
35
star
14

pmcam

Poor man's video capture with motion detection in Bash
Shell
34
star
15

go-trash

Cross-platform package to move stuff to the OS trash
Go
19
star
16

Kohana-Mustache

A Mustache module for Kohana.
PHP
14
star
17

github-actions-logs-extension

Display GitHub Action Raw log in color, converting the Ansi colors to HTML
JavaScript
11
star
18

joplin-turndown-plugin-gfm

Fork of turndown-plugin-gfm for Jopin
JavaScript
9
star
19

simple-gettext

C++
9
star
20

joplin-turndown

JavaScript
8
star
21

gannet

A database migration tool in PHP
PHP
7
star
22

proxy-tester

Go application to test web proxies
Go
5
star
23

joplin-renderer

Markdown and HTML renderer for Joplin
JavaScript
5
star
24

ripple-demo

A demo application for Ripple, the REST API framework for Go
Go
5
star
25

joplin-plugin

JavaScript
4
star
26

kimai-desktop

Desktop client for Kimai
C++
4
star
27

poparser

This a PHP parser for Gettext PO files. It can extract the strings and translations from the file. It can also provide statistics about the state of the translation - i.e. how many strings have been translated, how many are fuzzy, etc. which is useful to provide translation reports.
PHP
4
star
28

homebrew-massren

Homebrew tap for massren. NOTE: massren is now officially on Homebrew at https://github.com/Homebrew/homebrew-core/blob/master/Formula/massren.rb
Ruby
3
star
29

laurent22.github.io

https://laurent22.github.io/so-injections/
HTML
3
star
30

tkwidgets

JavaScript
2
star
31

kache

Simple cache system for PHP with support for multiple drivers.
PHP
2
star
32

go-torrent

Golang package to build a torrent client
Go
2
star
33

laurent22

1
star
34

androidsync

Android Sync is a command line tool to synchronize an Android phone with Windows, Mac OS X or Linux via adb
Go
1
star
35

generator-joplin

Scaffolds out a new Joplin plugin
TypeScript
1
star
36

akview

Command line based image viewer and editor
C++
1
star
37

ipify-www

The public facing website for ipify.org.
HTML
1
star