代码拉取完成,页面将自动刷新
同步操作将从 src-anolis-os/rear 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
From 7539cca9c6f9c6e4965893cf4dab92fdebe029b7 Mon Sep 17 00:00:00 2001
From: Weisson <Weisson@alibaba.linux.com>
Date: Mon, 5 Dec 2022 08:38:51 +0000
Subject: [PATCH] Add local backup script.
Signed-off-by: Weisson <Weisson@alibaba.linux.com>
---
usr/sbin/migrear | 132 ++++++++++++++++++
.../rear/pack/GNU/Linux/500_local_backup.sh | 60 ++++++++
.../system-setup.d/500-start-recovery.sh | 1 +
3 files changed, 193 insertions(+)
create mode 100755 usr/sbin/migrear
create mode 100644 usr/share/rear/pack/GNU/Linux/500_local_backup.sh
create mode 100644 usr/share/rear/skel/default/etc/scripts/system-setup.d/500-start-recovery.sh
diff --git a/usr/sbin/migrear b/usr/sbin/migrear
new file mode 100755
index 0000000..884e66d
--- /dev/null
+++ b/usr/sbin/migrear
@@ -0,0 +1,132 @@
+#! /usr/bin/env python3
+# -*- coding:utf-8 -*-
+import argparse
+import os
+import sys
+import platform
+
+import subprocess
+
+
+class MethodNotSupportException(Exception):
+ def __init__(self, method, message="unsupport method '{}', available choices {}."):
+ self.methods_supported = ["nfs", "local"]
+ self.message = message.format(method, str(self.methods_supported))
+ super().__init__(self.message)
+
+
+class MethodConflictException(Exception):
+ def __init__(self, method, url, message="method local and remote url should not be assigned at the same time."):
+ self.message = message
+ super().__init__(self.message)
+
+
+class NotRootPathException(Exception):
+ def __init__(self, path, message="path '{}' is not available, absolute path should be given for safety sake."):
+ self.message = message.format(path)
+ super().__init__(self.message)
+
+
+def check_output(cmd):
+ output = subprocess.check_output(cmd, shell=True)
+ if type(output) is str:
+ return output.strip()
+ elif sys.version_info[0] == 2 and (type(output) is unicode or type(output) is bytes):
+ return output.encode('utf-8').strip()
+ elif sys.version_info[0] == 3 and type(output) is bytes:
+ return str(output, 'utf-8').strip()
+ else:
+ raise TypeError("Expected bytes or string, but got %s." % type(output))
+
+
+def check_call(cmd):
+ return subprocess.check_call(cmd, shell=True)
+
+
+def build_nfs_url(url, path):
+ return "".join(["nfs://", url, '/', path])
+
+
+def backup(method, initramfs_path, initramfs_url, layout_path, layout_url, excludes):
+ local_cfg = """
+OUTPUT=RAMDISK
+OUTPUT_URL=%s
+BACKUP=NETFS
+BACKUP_URL=%s
+BACKUP_PROG_EXCLUDE=("\$\{BACKUP_PROG_EXCLUDE[@]\}" '/media' '/var/tmp' '/var/crash' %s '/tmp/*')
+NETFS_KEEP_OLD_BACKUP_COPY=
+""" % (initramfs_url, layout_url, " ".join(excludes))
+
+ with open("/etc/rear/local.conf", "w") as f:
+ f.write(local_cfg)
+
+ backup_env = " "
+
+ backup_env = "MBACKUP_MODE=%s MBACKUP_INITRAMFS_PATH=%s MBACKUP_LAYOUT_PATH=%s " % (method.upper(), initramfs_path, layout_path)
+ if method == "local":
+ if not os.path.exists(layout_path):
+ os.mkdir(layout_path)
+ with open("/etc/exports", "a") as f:
+ f.write("\n%s *(rw,sync,no_root_squash,no_subtree_check,crossmnt)" % (layout_path))
+ check_call("systemctl restart nfs-server")
+
+ command = " ".join([backup_env, "rear -v mkbackup"])
+ check_call(command)
+
+ # grub
+ vmlinuz = check_output("ls /boot/vmlinuz-$(uname -r)").strip()
+ vmlinux_ver = vmlinuz.rpartition('/')[-1]
+
+ initramfs_file_path = check_output("find %s -name 'initramfs-%s.img'" % (initramfs_path, platform.node()))
+ command = "grubby --add-kernel %s --initrd %s --title MigReaR-%s " % (vmlinuz, initramfs_file_path, vmlinux_ver)
+ if method == "nfs":
+ args = " --args='ro console=tty0 console=ttyS0,115200n8 console=ttyS0 unattended'"
+ elif method == "local":
+ args = " --args='ro console=tty0 console=ttyS0,115200n8 console=ttyS0'"
+
+ command += args
+ check_call(command)
+
+
+def parse_args():
+ parser = argparse.ArgumentParser(description="Relex and Recover for OS migration.")
+ parser.add_argument('--method', default=None, help="backup up method, local and nfs are supported currently.")
+ parser.add_argument('--url', default=None, help="remote backup url")
+ parser.add_argument('--excludes', default=None, help="excludes these directories or files when backup, splitted by comma.")
+ parser.add_argument('--path', default='/storage', help="where root layout would be kept, /storage by default.")
+ parser.add_argument('--boot', default=None, help="where initramfs would be kept, same with --path if not given.")
+
+ args = parser.parse_args()
+
+ args.excludes = args.excludes.split(',') if args.excludes else []
+
+ if args.method == "local" and args.url is not None:
+ raise MethodConflictException(args.method, args.url)
+ if not args.boot:
+ if args.method == "local":
+ args.boot = args.path
+ else:
+ args.boot = "/boot/"
+
+ for path in [args.path, args.boot] + args.excludes:
+ if not path.startswith('/'):
+ raise NotRootPathException(path)
+
+ if args.method == "local":
+ args.url = "127.0.0.1"
+ args.excludes += [args.path]
+
+ if args.method == "local":
+ args.layout_path = build_nfs_url(args.url, args.path)
+ elif args.method == "nfs":
+ args.layout_path = build_nfs_url(args.url, args.path)
+ else:
+ raise MethodNotSupportException(args.method)
+
+ args.initramfs_path = "file://" + args.boot
+
+ return args
+
+if __name__ == "__main__":
+ args = parse_args()
+ backup(args.method, args.boot, args.initramfs_path, args.path, args.layout_path, args.excludes)
diff --git a/usr/share/rear/pack/GNU/Linux/500_local_backup.sh b/usr/share/rear/pack/GNU/Linux/500_local_backup.sh
new file mode 100644
index 0000000..acf2505
--- /dev/null
+++ b/usr/share/rear/pack/GNU/Linux/500_local_backup.sh
@@ -0,0 +1,60 @@
+if [ $MBACKUP_MODE = "LOCAL" ]; then
+
+LogPrint "Creating recovery script ..."
+
+ROOT_DEVICE=`df -hT / | grep /$ | awk '{print $1}'`
+echo $ROOT_DEVICE
+
+cat << SCRIPT >> $ROOTFS_DIR/usr/bin/recovery
+#! /usr/bin/bash
+
+echo ============= system is recovering in 20 seconds ... ==============
+sleep 20
+
+ROOTFS=\$1
+
+ROOTFS=\${ROOTFS:-$ROOT_DEVICE}
+MOUNT_POINT=/mnt/cdrom/
+BACKUP_POINT=/storage/
+LAYOUT_BACKUP_POINT=$MBACKUP_LAYOUT_PATH
+
+echo "Running recover ..."
+echo "Mounting \$ROOTFS ..."
+mount -v \$ROOTFS \$MOUNT_POINT
+cp -r -v \$MOUNT_POINT/\$LAYOUT_BACKUP_POINT/ \$BACKUP_POINT
+
+umount -v \$ROOTFS
+
+cat << EOF > /etc/rear/local.conf
+OUTPUT_URL=file:///storage/
+BACKUP=NETFS
+BACKUP_URL=file:///storage/
+BACKUP_PROG_EXCLUDE=("\${BACKUP_PROG_EXCLUDE[@]}" '/media' '/var/tmp' '/var/crash' '/storage')
+NETFS_KEEP_OLD_BACKUP_COPY=
+EOF
+
+WORKROUND_ACTOR="/usr/share/rear/lib/framework-functions.sh"
+sed 's/ || BugError "Directory \$BUILD_DIR\/outputfs not empty, can not remove\"\$//g' -i \$WORKROUND_ACTOR
+
+rear -v recover
+SCRIPT
+chmod +x $ROOTFS_DIR/usr/bin/recovery
+
+
+cat << EOF >> $ROOTFS_DIR/etc/systemd/system/SystemRecovery.service
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/recovery $ROOT_DEVICE
+StandardInput=null
+StandardOutput=journal+console
+StandardError=journal+console
+
+[Install]
+WantedBy=multi-user.target
+EOF
+
+# mkdir $ROOTFS_DIR/etc/systemd/system/multi-user.target.wants/
+# cp $ROOTFS_DIR/etc/systemd/system/SystemRecovery.service $ROOTFS_DIR/etc/systemd/system/multi-user.target.wants/
+
+fi
+
diff --git a/usr/share/rear/skel/default/etc/scripts/system-setup.d/500-start-recovery.sh b/usr/share/rear/skel/default/etc/scripts/system-setup.d/500-start-recovery.sh
new file mode 100644
index 0000000..30c54db
--- /dev/null
+++ b/usr/share/rear/skel/default/etc/scripts/system-setup.d/500-start-recovery.sh
@@ -0,0 +1 @@
+systemctl start SystemRecovery
\ No newline at end of file
--
2.30.1 (Apple Git-130)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。