nazar-pc / just-backup-btrfs
脚本可以做到这一点——使用btrfs文件系统的快照创建备份。它还会通过删除旧快照并保留您想要的快照数量来执行快照轮换。
Requires
- php: >=5.4
- ext-sqlite3: *
This package is not auto-updated.
Last update: 2024-09-14 18:53:03 UTC
README
仅备份btrfs
脚本可以做到这一点——使用btrfs文件系统的快照创建备份。它还会通过删除旧快照并保留您想要的快照数量来执行快照轮换。
为什么?
我想做简单的事情:将多个子卷的快照存储在一个地方,并为每个时间段保留特定数量的快照(更近期的和较旧的快照)。
我很惊讶没有现有的解决方案符合我的需求,所以我决定写它:)
要求
- php-cli (版本 5.4+)
- php-sqlite3
您可以在Ubuntu 15.10-15.10上获得它们-
sudo apt-get install php5-cli php5-sqlite
Ubuntu 16.04+
sudo apt-get install php-cli php-sqlite3
同时启用sqlite3扩展,因为它默认可能被禁用。Ubuntu 15.10-
sudo php5enmod sqlite3
Ubuntu 16.04+
sudo phpenmod sqlite3
安装
整个程序仅是一个文件,所以首先的方式就是将文件just-backup-btrfs
复制到某处。
或者您可以使用Composer全局安装它,如下所示
sudo COMPOSER_BIN_DIR=/usr/local/bin composer global require nazar-pc/just-backup-btrfs
COMPOSER_BIN_DIR=/usr/local/bin
将指示Composer将二进制文件安装到/usr/local/bin
,这样您就可以在安装后立即调用just-backup-btrfs
。或者您可以选择跳过它并将~/.composer/vendor/bin/
添加到您的PATH中。
移除
如果手动安装,只需删除文件,如果使用Composer安装,则使用以下命令删除
sudo COMPOSER_BIN_DIR=/usr/local/bin composer global remove nazar-pc/just-backup-btrfs
或者在没有使用COMPOSER_BIN_DIR=/usr/local/bin
的情况下删除它,如果您在安装期间没有使用它。
使用方法
脚本期望配置文件位于/etc/just-backup-btrfs.json
的位置,以下是一个此类文件的示例。另外,如果您想使用其他配置文件路径,请将其指定为参数。
有几种运行脚本的方式。
sudo php just-backup-btrfs
或者将文件标记为可执行文件并直接运行
sudo ./just-backup-btrfs
或者将其标记为可执行文件并放入/etc/cron.daily
以每天进行备份。
使用自定义配置文件路径
sudo ./just-backup-btrfs /path/to/config.json
输出将如下所示
Just backup btrfs started...
CMD: /bin/btrfs subvolume snapshot -r "/" "/backup/root/2016-03-12_16:29:52"
CMD: sync
Snapshot 2016-03-12_16:29:52 for / created successfully
CMD: mount
Mounting root subvolume skipped, since already mounted
Making incremental backup
CMD: /bin/btrfs send -p "/backup/root/2016-03-12_16:26:48" "/backup/root/2016-03-12_16:29:52" | /bin/btrfs receive /tmp/just_backup_btrfs_45e177f34dddc33a5b1537ce8f4913ba/root
At subvol /backup/root/2016-03-12_16:29:52
CMD: /bin/btrfs subvolume delete "/backup_hdd/root/2016-03-11_16:30:01"
Old snapshot 2016-03-11_16:30:01 removed successfully from /backup_hdd/root
Creating incremental backup 2016-03-12_16:29:52 of / to /backup_hdd/root finished successfully
Unmounting root subvolume skipped
CMD: /bin/btrfs subvolume delete "/backup/root/2016-03-12_15:30:01"
Old snapshot 2016-03-12_15:30:01 removed successfully from /backup/root
CMD: /bin/btrfs subvolume snapshot -r "/home" "/backup/home/2016-03-12_16:30:04"
CMD: sync
Snapshot 2016-03-12_16:30:04 for /home created successfully
CMD: mount
Mounting root subvolume skipped, since already mounted
Making incremental backup
CMD: /bin/btrfs send -p "/backup/home/2016-03-12_16:26:51" "/backup/home/2016-03-12_16:30:04" | /bin/btrfs receive /tmp/just_backup_btrfs_45e177f34dddc33a5b1537ce8f4913ba/home
At subvol /backup/home/2016-03-12_16:30:04
CMD: /bin/btrfs subvolume delete "/backup_hdd/home/2016-03-11_16:30:08"
Old snapshot 2016-03-11_16:30:08 removed successfully from /backup_hdd/home
Creating incremental backup 2016-03-12_16:30:04 of /home to /backup_hdd/home finished successfully
Unmounting root subvolume skipped
CMD: /bin/btrfs subvolume delete "/backup/home/2016-03-12_15:30:05"
Old snapshot 2016-03-12_15:30:05 removed successfully from /backup/home
CMD: /bin/btrfs subvolume snapshot -r "/web" "/backup/web/2016-03-12_16:30:30"
CMD: sync
Snapshot 2016-03-12_16:30:30 for /web created successfully
CMD: mount
Mounting root subvolume skipped, since already mounted
Making incremental backup
CMD: /bin/btrfs send -p "/backup/web/2016-03-12_16:26:58" "/backup/web/2016-03-12_16:30:30" | /bin/btrfs receive /tmp/just_backup_btrfs_45e177f34dddc33a5b1537ce8f4913ba/web
At subvol /backup/web/2016-03-12_16:30:30
CMD: /bin/btrfs subvolume delete "/backup_hdd/web/2016-03-11_16:30:26"
Old snapshot 2016-03-11_16:30:26 removed successfully from /backup_hdd/web
Creating incremental backup 2016-03-12_16:30:30 of /web to /backup_hdd/web finished successfully
Unmounting root subvolume skipped
CMD: /bin/btrfs subvolume delete "/backup/web/2016-03-12_15:30:15"
Old snapshot 2016-03-12_15:30:15 removed successfully from /backup/web
Just backup btrfs finished!
您也可以用cron或其他方式调用它:)
它实际上做什么?
- 从
/etc/just-backup-btrfs.json
读取配置 - 在
destination_within_partition
中创建带有日期名称的快照(如果尚不存在,则创建destination_within_partition/history.db
SQLite数据库) - 在
destination_within_partition
中创建快照 - 在
destination_other_partition
中创建备份(快照的副本,位于destination_within_partition
中),以防源文件系统崩溃 - 将快照名称、日期以及应保留快照多长时间存储在
destination_within_partition/history.db
(以及destination_other_partition/history.db
,因为备份可能有自己的保留设置) - 从
destination_within_partition/history.db
(以及destination_other_partition/history.db
)中读取旧快照,并将它们从history.db
和文件系统中删除
配置
配置选项设计得特别直观
[ { "source_mounted_volume" : "/", "destination_within_partition" : "/backup/root", "destination_other_partition" : null, "date_format" : "Y-m-d_H:i:s", "keep_snapshots" : { "hour" : 60, "day" : 24, "month" : 30, "year" : 48 } }, { "source_mounted_volume" : "/home", "destination_within_partition" : "/backup/home", "destination_other_partition" : "/backup_external/home", "date_format" : "Y-m-d_H:i:s", "optimize_mounts" : false, "keep_snapshots" : { "hour" : 120, "day" : 48, "month" : 60, "year" : 96 }, "keep_other_snapshots" : { "hour" : -1, "day" : 96, "month" : 120, "year" : 192 }, "minimum_delete_count" : 10, "minimum_delete_count_other" : 10 } ]
source_mounted_volume
- 字符串,绝对路径,要备份/创建快照的子卷destination_within_partition
- 字符串,绝对路径,存储快照的位置,应与source_mounted_volume
在同一个分区上destination_other_partition
- 字符串,绝对路径(如果不需要则可为null
),存储实际备份的位置,预期为另一个BTRFS分区date_format
- 字符串,日期格式,如PHP date()函数keep_snapshots
- 包含键hour
、day
、month
和year
的数组,每个键包含在相应时间间隔内必须保留的快照数量(-1
表示无限量)keep_other_snapshots
- 与keep_snapshots
相同,但用于备份,默认为keep_snapshots
optimize_mounts
- 允许在外部备份期间避免不断重新挂载根分区,因为它可能很慢;默认为true
,必要时可能被禁用minimum_delete_count
- 最小删除快照数量,用于减少碎片并提高性能,默认为1
minimum_delete_count_other
- 与minimum_delete_count
相同,但用于备份,默认为minimum_delete_count
将此配置保存为 /etc/just-backup-btrfs.json
,并根据需要进行自定义。
许可证
MIT,您可以随意修改并分享!