时光机式备份与rsync (2018)
Time Machine-style Backups with rsync (2018)

原始链接: https://samuelhewitt.com/blog/2018-06-05-time-machine-style-backups-with-rsync

这篇帖子详细介绍了一种使用`rsync`工具创建基于时间的数据备份快照的方法。在意外删除文件后,作者为他们的家庭服务器设置寻找比macOS Time Machine更好的解决方案。 该解决方案的核心是一个shell脚本,利用`rsync`的`--link-dest`选项来创建增量备份。这意味着只有*更改*的文件会被复制,而未更改的文件则通过硬链接指向先前的快照,从而节省大量存储空间。每个快照都带有时间戳,方便识别。 该脚本将用户的数据目录备份到目标目录,然后更新一个符号链接(“current”)以指向最新的快照。一个cron作业自动执行此过程,每天运行该脚本。作者强调在删除旧快照之前进行完整备份的重要性,并感谢Mike Rubel提供的灵感。

这个Hacker News讨论围绕使用`rsync`创建“时光机”风格的备份,最初由一篇2018年的文章(samuelhewitt.com)推广。核心思想是利用`rsync`和硬链接来高效存储增量备份,模拟按时间点的快照。 用户指出`rsnapshot`和Mike Rubel的原始文章是这项技术的先驱,并指出Rubel的工作影响了`rsync`对硬链接的内置支持。 许多评论者分享了他们自己的实现,包括一个在GitHub上提供的bash封装(laurent22/rsync-time-backup)。 对话还涉及`restic`和`kopia`等替代方案,用于更强大的备份,尤其是在文件系统快照(如ZFS或XFS中提供的快照)不可行时,以及Syncthing用于镜像。 一位用户专门请求一个macOS兼容的脚本,利用`launchd`和`FSEvents`进行更改检测。
相关文章

原文
Posted on 05 Jun 2018

Data Hoarding 101

I have digital backups of data, especially of my photos, going back almost a decade (I’m not that old) but it’s only recently that I got into creating time-snapshots of my backups, after I mistakenly deleted a bunch of (non-crucial) personal files with a stray rsync --delete on a directory I had not backed-up.

My home server used be compatible with macOS’s Time Machine and when I kept most things on my Mac that was useful, but I changed my data hoarding habits and that solution started to not be the optimal one.

Time Based Snapshots Using rsync

rsync has got to be one of my favourite and most used utilities. I use it constantly to transfer files between machine on my local network (and remote ones) but I’ve discovered it’s very useful as a backup tool. The following is a short script that uses rsync to create snapshots of the my data directory to another directory (they are mount points on two different drives):

#!/bin/sh

TIMESTAMP=`date "+%Y-%m-%dT%H-%M-%S"`
USER=user
SOURCEDIR=/var/data/backups
TARGETDIR=/var/redundancy

# Create new backup using rsync and output to log
rsync -avPh --delete --link-dest=$TARGETDIR/current $SOURCEDIR/$USER/ $TARGETDIR/$USER-$TIMESTAMP > /var/log/rsync/$TIMESTAMP.log 2>&1

# check exit status to see if backup failed
if [ "$?" = 0 ]; then
    # Remove link to current backup
    rm -f $TARGETDIR/current
    # Create link to the newest backup
    ln -s $TARGETDIR/$USER-$TIMESTAMP $TARGETDIR/current
else
    # Rename directory if failed
    mv $TARGETDIR/$USER-$TIMESTAMP $TARGETDIR/failed-$USER-$TIMESTAMP
fi

Essentially this script creates incremental copies of backups for a given user on the system by comparing any changes to that user’s data directory ($SOURCEDIR) with the current backup ($TARGETDIR) and making a new snapshot of any changes with the date.

Now all that remains is to save this script somewhere and create a cron job to run it on an automatic interval:

# daily at 5am
0 5 * * * bash /usr/local/bin/rsync-time-machine

This method uses rsync’s --link-dest option to create hard-links so that isn’t duplicative of file data (the contents are only stored once, so you don’t use twice as much) and you can delete snapshots at will. (The links do still take up some space but it’s relatively insignificant.)

Be careful when deleting old snapshots. A new full backup of should be made before further incremental ones. I have to give a shoutout to Mike Rubel whose post was helpful in coming up with this solution.

联系我们 contact @ memedata.com