### [筆記] Linux 底下好用的備份工具 rsync-time-backup

2018-04-10 筆記

很好用的工具，利用rsync 去作到類似MAC上的 time machine的功能 備份後的目錄大概會是這樣子

    drwxr-xr-x 1 mini sudo 3100 Apr  9 16:14 2018-04-10-160719
    drwxr-xr-x 1 mini sudo 3100 Apr  9 16:14 2018-04-10-160224
    drwxr-xr-x 1 mini sudo 3100 Apr  9 16:14 2018-04-10-160054
    drwxr-xr-x 1 mini sudo 3100 Apr  9 16:14 2018-04-10-155836
    drwxr-xr-x 1 mini sudo 3100 Apr  9 16:14 2018-04-10-155826
    -rw-r--r-- 1 mini sudo    0 Apr 10 15:58 backup.marker
    drwxr-xr-x 1 mini sudo  472 Apr 10 16:07 ..
    lrwxrwxrwx 1 mini sudo   17 Apr 10 16:07 latest -> 2018-04-10-160719
    drwxr-xr-x 1 mini sudo  208 Apr 10 16:07 .
    2018-04-10 16:12:37 [mini@h9 ~]$

前面幾個目錄都可以進去看檔案內容，需要還原就直接cp 出來就好 基本上每個目錄都只會佔用「異動」後的檔案空間

    du -sh *
    125M	2018-04-10-155826
    0	2018-04-10-155836
    0	2018-04-10-160054
    4.0K	2018-04-10-160224
    0	2018-04-10-160719
    0	backup.marker
    4.0K	latest

備份的策略在readme裡面有提到

一天之內，所有的備份都會被保留(前提是空間足夠) 一個月內，每一天的最後一份備份會被保留(或者說「最近」的一份) 超過一個月，每個月的最後一份備份會被保留(或者說「最近」的一份)

    The script automatically deletes old backups using the following logic:

     Within the last 24 hours, all the backups are kept.
     Within the last 31 days, the most recent backup of each day is kept.
     After 31 days, only the most recent backup of each month is kept.
     Additionally, if the backup destination directory is full, the oldest backups are deleted until enough space is available.

original link : https://github.com/laurent22/rsync-time-backup

README.md

# 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. –rsync-get-flags Display the default rsync flags that are used for backup. –rsync-set-flags Set 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.

## 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 user@example.com:/mnt/backup_drive

- Backup from remote drive over SSH:

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 [[ -d /mnt/backup ]]; then rsync_tmbackup.sh /home /mnt/backup; fi

## Backup expiration logic

The script automatically deletes old backups using the following logic: - Within the last 24 hours, all the backups are kept. - Within the last 31 days, the most recent backup of each day is kept. - After 31 days, only the most recent backup of each month is kept. - Additionally, if the backup destination directory is full, the oldest backups are deleted until enough space is available.

## Exclude 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-set-flags` option. For example, to exclude backing up permissions and groups:

rsync_tmbackup –rsync-set-flags “–numeric-ids –links –hard-links
–one-file-system –archive –no-perms –no-groups –itemize-changes” /src /dest

## 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 © 2013-2017 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.

comments powered by Disqus
© 2017 by Lednerb
Bilberry Hugo Theme