补充最后的BTRFS工具 btrfs-du,我想分享一个用于创建BTRFS快照的简单脚本,该脚本可与cron和systemd计时器完美配合。这使我们可以轻松地自动创建快照。
我受到启发 btrfs快照 由Birger Monsen。
用法
btrfs-snp 可以手动运行,也可以从cron召唤。不带参数调用即可查看用法详细信息。
# btrfs-snp Usage: btrfs-snp <dir> (<tag>) (<limit>) (<seconds>) (<destdir>) dir │ create snapshot of <dir> tag │ name the snapshot <tag>_<timestamp> limit │ keep <limit> snapshots with this tag. 0 to disable seconds │ don't create snapshots before <seconds> have passed from last with this tag. 0 to disable destdir │ store snapshot in <destdir>, relative to <dir>
的快照 /家
# btrfs-snp /家
根的标记快照
# btrfs-snp / preupgrade
已标记根快照,但最多保留10个快照
# btrfs-snp / preupgrade 10
从cron运行的一个示例:创建并保留一天的每小时快照,一天的一周快照,一个月的每周快照和一年的每月快照。
#!/bin/bash /usr/local/sbin/btrfs-snp /家 hourly 24 3600 /usr/local/sbin/btrfs-snp /家 daily 7 86400 /usr/local/sbin/btrfs-snp /家 weekly 4 604800 /usr/local/sbin/btrfs-snp / weekly 4 604800 /usr/local/sbin/btrfs-snp /家 monthly 12 2592000
唐’t forget to run
# chmod +x /etc/cron.hourly/btrfs-snp
几个小时后,您将看到快照填充
$ tree -L 1 /家/.snapshots /home/.snapshots ├── daily_2017-12-27_130102 ├── hourly_2017-12-27_130101 ├── hourly_2017-12-27_140101 ├── hourly_2017-12-27_150101 ├── hourly_2017-12-27_160101 ├── monthly_2017-12-27_130104 └── weekly_2017-12-27_130103 7 directories, 0 files
安装
得到 脚本 并使其可执行。您可以分两行执行此操作,但最好先对其进行检查。不要盲目相信任何人。
sudo wget //raw.githubusercontent.com/nachoparker/btrfs-snp/master/btrfs-snp -O /usr/local/sbin/btrfs-snp sudo chmod +x /usr/local/sbin/btrfs-snp
如果您想从 克朗,您可能必须首先安装它,因为某些发行版已经完全被systemd计时器取代。在Arch Linux中,我就是这种情况。
就我而言,我安装了 克罗妮
sudo pacman -S 克罗妮
克罗妮 默认情况下会将输出记录到系统日志中,但是如果您想要旧样式的cron邮件,则可以设置电子邮件系统。
另外,请注意,您可以使用 慢性的 如果您只希望仅在出现问题时才进行日志记录。
细节
有一个 二十亿个脚本 安排快照,但我不能决定一个。
我没有’不想使用可用的python脚本,因为在我看来它们过于复杂。这是一个简单的任务,而且我喜欢易于阅读的简单代码。
btrfs快照 我最喜欢它的原因有几个:它使用正确的工具进行工作,即bash或python。另外,我喜欢防止过于频繁地创建快照以简化cron使用的想法。
我喜欢这个主意,但不喜欢实现。 btrfs快照 它的功能太多了,还有几个 被认为是不良做法 在编码风格上。另外,有些选择只是不’t work.
使用的想法 过时的 很好,但是在我的测试中 过时的 changes 通过 checking it, so 没有’t quite work. Also, btrfs快照 没有’•无法检测到安装点根目录之外的更改,因此即使发生更改,它也拒绝快照。
我开始编写请求请求,但最终更改了所有代码,因此我从零开始。
希望能帮助到你!
码
#!/bin/bash # # Script that creates BTRFS snapshots, manually or from 克朗 # # 用法: # sudo btrfs-snp <dir> (<tag>) (<limit>) (<seconds>) (<destdir>) # # Copyleft 2017 通过 Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com> # GPL licensed (see end of file) * Use at your own risk! # # Based on btrfs快照 通过 Birger Monsen # # More at //ownyourbits.com # function btrfs-snp() { local BIN="${0##*/}" local DIR="${1}" local TAG="${2:-snapshot}" local LIMIT="${3:-0}" local TIME="${4:-0}" local DST="${5:-.snapshots}" ## usage [[ "$*" == "" ]] || [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]] && { echo "用法: $BIN <dir> (<tag>) (<limit>) (<seconds>) (<destdir>) dir │ create snapshot of <dir> tag │ name the snapshot <tag>_<timestamp> limit │ keep <limit> snapshots with this tag. 0 to disable seconds │ don't create snapshots before <seconds> have passed from last with this tag. 0 to disable destdir │ store snapshot in <destdir>, relative to <dir> Cron example: Hourly snapshot for one day, daily for one week, weekly for one month, and monthly for one year. cat > /etc/cron.hourly/$BIN <<EOF #!/bin/bash /usr/local/sbin/$BIN /家 hourly 24 3600 /usr/local/sbin/$BIN /家 daily 7 86400 /usr/local/sbin/$BIN /家 weekly 4 604800 /usr/local/sbin/$BIN / weekly 4 604800 /usr/local/sbin/$BIN /家 monthly 12 2592000 EOF chmod +x /etc/cron.hourly/$BIN" return 0 } ## checks local SNAPSHOT=${TAG}_$( date +%F_%H%M%S ) [[ ${EUID} -ne 0 ]] && { echo "Must be run as root. Try 'sudo $BIN'" ; return 1; } [[ -d "$SNAPSHOT" ]] && { echo "$SNAPSHOT already exists" ; return 1; } mount -t btrfs | cut -d' ' -f3 | grep -q "^${DIR}$" || { btrfs subvolume show "$DIR" | grep -q "${DIR}$" || { echo "$DIR is not a BTRFS mountpoint or snapshot" return 1 } } DST="$DIR/$DST" mkdir -p "$DST" local SNAPS=( $( btrfs subvolume list -s --sort=gen "$DST" | awk '{ print $14 }' | grep "${TAG}_" ) ) ## check time of the last snapshot for this tag [[ "$TIME" != 0 ]] && [[ "${#SNAPS[@]}" != 0 ]] && { local LATEST=$( sed -r "s|.*_(.*_.*)|\\1|;s|_([0-9]{2})([0-9]{2})([0-9]{2})| \\1:\\2:\\3|" <<< "${SNAPS[-1]}" ) LATEST=$( date +%s -d "$LATEST" ) || return 1 [[ $(( LATEST + TIME )) -gt $( date +%s ) ]] && { echo "No new snapshot needed for $TAG"; return 0; } } ## do it btrfs subvolume snapshot -r "$DIR" "$DST/$SNAPSHOT" || return 1 ## prune older backups [[ "$LIMIT" != 0 ]] && \ [[ ${#SNAPS[@]} -ge $LIMIT ]] && \ echo "Pruning old snapshots..." && \ for (( i=0; i <= $(( ${#SNAPS[@]} - LIMIT )); i++ )); do btrfs subvolume delete "$DIR/${SNAPS[$i]}" done echo "snapshot $SNAPSHOT generated" } btrfs-snp "$@" # License # # This 脚本 is free 软件; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published 通过 # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This 脚本 is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this 脚本; if not, write to the # Free Software Foundation, Inc., 59 Temple Place, Suite 330, # Boston, MA 02111-1307 USA
你好我’我试图改用btrfs-snp。以前我使用的是btrfs-snap,但是它不再适用于我的内核,
我喜欢使用cron进行安排的想法。但是我遇到了一个问题,与btrfs-snap不同,我无法在装入子卷的过程中快照子卷的内容
即/ mnt是我唯一的btrfs挂载点,快照可以工作,但我无法为未挂载的快照指定子卷,如/ mnt / Data / Doumentation。因为这些是/ mnt的子卷。
我是否必须安装每个子卷才能对其进行快照?还是有递归选项?
谢谢
特里斯坦
我希望这是有道理的,
嗯,我避风港’以前没有遇到过这种情况。我不’不知道btrfs-snap是做什么的,可能只是自动安装它。如果其他人有此用例,则可以在github上打开功能要求,或发送PR。