[Share Experiences] 使用timeshift实现系统和数据分开备份
Tofloor
poster avatar
风吹过的绿洲
deepin
2023-06-14 06:10
Author

timeshift如何按不同配置备份呢?实现系统和数据分开

timeshift默认的配置文件在/etc/timeshift/timeshift.json

而我想实现系统和数据分开备份,这个就需要同时运行两个任务。

似乎没找到命令行可以指定配置文件的相关文档。

如:

sudo timeshift --create --comments "new backup" --config /etc/timeshift/data.json --tags D --snapshot-device /dev/sda1

Reply Favorite View the author
All Replies
风吹过的绿洲
deepin
2023-06-14 08:37
#1

自己摸索了一个傻瓜的方案:

image.png

  1. copy 一份timeshift默认的配置,并更改为自己的配置timeshift-data.json

这里的磁盘uuid我选择了另一个磁盘,防止一块磁盘突然损坏导致数据丢失,只备份数据排除其他所有内容。

系统备份有默认的timeshift定时任务。

第一次备份的话,do_first_run要改为true,注意这里需要修改为自己存储备份磁盘的uuid,输入blkid查询

jay@jay-PC:~$ blkid
/dev/nvme0n1p9: LABEL="work" UUID="df1ec32f-3950-484c-9d8b-0126e908e4e5" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="work" PARTUUID="712a0854-7f1c-4465-a7f6-39ca93fb17c4"
/dev/nvme0n1p7: LABEL="dde_home" UUID="722fad97-7ae2-48cf-9493-c1fcbaff044e" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="dde_home" PARTUUID="885d2978-c497-48c3-8647-e76ff306a4ea"
/dev/nvme0n1p5: LABEL="Roota" UUID="b1554732-8c79-4e88-ad79-47d398b35ddc" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="Basic data partition" PARTUUID="5d1a9b1e-9c13-4aac-a645-a30eaf8c3632"
/dev/nvme0n1p3: LABEL="SWAP" UUID="cea73cfa-6483-49fd-957a-129c81231fb5" TYPE="swap" PARTLABEL="primary" PARTUUID="57e3098c-5463-4c53-8690-d27dc34ccdf5"
/dev/nvme0n1p1: LABEL_FATBOOT="EFI" LABEL="EFI" UUID="547C-1CAF" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="EFI system partition" PARTUUID="aa0018e6-8b25-42ec-8dbc-3700757826b3"
/dev/nvme0n1p8: LABEL="Roota" UUID="704b2ebd-38e8-4e0c-acb7-2fa905c4df69" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="Roota" PARTUUID="fe5bb8fc-5107-4cfd-a15a-0c9c67c56434"
/dev/nvme0n1p6: LABEL="_dde_data" UUID="fe81a311-c7e9-4fb7-be56-039f9590ac7a" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="v23_home" PARTUUID="6e5b62e1-6c69-467f-8d5a-e4b46d065ac5"
/dev/nvme0n1p4: LABEL="Boot" UUID="9bc73b96-733e-4a07-9319-e7c970d35015" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="Boot" PARTUUID="17e1ae6d-7492-48c8-b0bb-47329b37853b"
/dev/nvme1n1p4: LABEL="win10-rec" BLOCK_SIZE="512" UUID="34945552945517A6" TYPE="ntfs" PARTUUID="e57cff40-7986-4e96-9ec5-f678f8e801bd"
/dev/nvme1n1p9: LABEL="apps" UUID="0bf94322-a57d-4065-bed0-b45a22c5c8bf" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="a282be2a-5082-4fd3-a587-4ec593f5e690"
/dev/nvme1n1p7: LABEL="data" BLOCK_SIZE="512" UUID="C022AA4B225A6D42" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="820ceea7-ec32-4d56-aca0-cbdacfaba321"
/dev/nvme1n1p10: LABEL="data_backup" UUID="bd99e96e-4e68-4b24-b5ff-29d54f5a9af6" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="6d6dae64-63a7-4b0a-8a88-5edd00b939bb"
/dev/nvme1n1p5: LABEL="soft" BLOCK_SIZE="512" UUID="2D97AD940A9AD661" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="6cae6bc0-7ece-43e7-84e0-76993b5837fa"
/dev/nvme1n1p3: LABEL="win10" BLOCK_SIZE="512" UUID="E88A37248A36EEA6" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="e8fd20fe-1eca-4f4d-8672-46d6c8637d87"
/dev/nvme1n1p1: SEC_TYPE="msdos" LABEL_FATBOOT="EFI" LABEL="EFI" UUID="DA18-EBFA" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="EFI system partition" PARTUUID="f6757783-bcbe-4795-a61c-1c097dc6c475"
/dev/nvme1n1p8: UUID="32912f5b-c4b4-47b8-8480-58e5b64bf490" BLOCK_SIZE="4096" TYPE="apfs" PARTLABEL="Basic data partition" PARTUUID="3f88f26f-5778-4a29-8959-67c8d46fdee8"
/dev/nvme1n1p6: LABEL="doc" BLOCK_SIZE="512" UUID="D4FA828F299D817A" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="9f68d4fa-6ac8-4818-913e-284ed3410a14"

╭─jay at jay-PC in /etc/timeshift 
╰─λ ls
default.json  timeshift-data.json  timeshift.json
╭─jay at jay-PC in /etc/timeshift 
╰─λ cat timeshift-data.json 
{
  "backup_device_uuid" : "bd99e96e-4e68-4b24-b5ff-29d54f5a9af6",
  "parent_device_uuid" : "",
  "do_first_run" : "false",
  "btrfs_mode" : "false",
  "include_btrfs_home_for_backup" : "false",
  "include_btrfs_home_for_restore" : "false",
  "stop_cron_emails" : "false",
  "schedule_monthly" : "false",
  "schedule_weekly" : "true",
  "schedule_daily" : "false",
  "schedule_hourly" : "false",
  "schedule_boot" : "false",
  "count_monthly" : "2",
  "count_weekly" : "2",
  "count_daily" : "5",
  "count_hourly" : "6",
  "count_boot" : "5",
  "snapshot_size" : "8854739085",
  "snapshot_count" : "103914",
  "date_format" : "%Y-%m-%d %H:%M:%S",
  "exclude" : [
    "/home/jay/Documents/WeChat Files/**",
    "/home/osroot/**",
    "+ /opt/***",
    "+ /home/***",
    "/***"
  ],
  "exclude-apps" : [
  ]
}

原则是排除的文件要写在添加的文件夹前面,这样才可以实现最终备份的文件不包含排除的文件。

如上,因为微信时间久了,占用空间很大,我就排除了。(之前排除一直失败,后面才知道是顺序的问题)

  1. 脚本中增加如下内容

╭─jay at jay-PC in /timeshift 
╰─λ cat back-data.sh  
#!/bin/bash
timeshift_sys=/etc/timeshift/timeshift.json
timeshift_data=/etc/timeshift/timeshift-data.json
timeshift_bak=/etc/timeshift/timeshift-bak.json
# 这里加了判断,防止中断导致的文件混乱
if [[ -f $timeshift_sys && -f $timeshift_data ]]; then
    mv /etc/timeshift/timeshift.json /etc/timeshift/timeshift-bak.json
    mv /etc/timeshift/timeshift-data.json /etc/timeshift/timeshift.json
fi
timeshift --check --scripted
if [[ -f $timeshift_sys && -f $timeshift_bak ]]; then
    mv /etc/timeshift/timeshift.json /etc/timeshift/timeshift-data.json
    mv /etc/timeshift/timeshift-bak.json /etc/timeshift/timeshift.json
fi

  1. 复制一个timeshift的定时任务
╭─jay at jay-PC in /etc/cron.d 
╰─λ sudo cp timeshift-hourly timeshift-data
╭─jay at jay-PC in /etc/cron.d 
╰─λ cat timeshift-data 
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
30 * * * * root /timeshift/back-data.sh
  1. 手动触发
╭─jay at jay-PC in /home/jay 
╰─λ sudo /timeshift/back-data.sh 
请输入密码:
验证成功
Mounted '/dev/nvme1n1p10' at '/run/timeshift/285816/backup'
Weekly snapshots are enabled
Last weekly snapshot is 0 days old
------------------------------------------------------------------------------
Added cron task: /etc/cron.d/timeshift-hourly

Found stale mount for device '/dev/nvme1n1p10' at path '/run/timeshift/285816/backup'
Unmounted successfully

不影响正常的系统备份
image.png

新的解决方案:

2024-08-18之后,通过修改timeshift的源代码,传入timeshift-data.json配置文件,现在不需要几个配置文件动态切换了。


复制了一个timeshift-launcher为timeshift-launcher-data,并在命令后加相应的配置文件,这个需要编译新的代码才能够支持。老的timeshift仍可以使用原来的方案

╭─jay at jay-PC in /usr/local/bin 
╰─λ cat timeshift-launcher-data 
#!/usr/bin/env bash

app_command='timeshift-gtk --config /opt/apps/com.idaoteng.data-bak/timeshift-data.json'

if [ "$(id -u)" -eq 0 ]; then
        # user is admin
        ${app_command}
else
        # user is not admin
        if echo $- | grep "i" >/dev/null 2>&1; then
                # script is running in interactive mode
                su - -c "${app_command}"
        else
                # script is running in non-interactive mode
                if [ "$XDG_SESSION_TYPE" = "wayland" ] ; then
                        xhost +SI:localuser:root
                        pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY ${app_command}
                        xhost -SI:localuser:root
                        xhost
                elif command -v pkexec >/dev/null 2>&1; then
                        pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY ${app_command}
                elif command -v sudo >/dev/null 2>&1; then
                        x-terminal-emulator -e "sudo ${app_command}"
                elif command -v su >/dev/null 2>&1; then
                        x-terminal-emulator -e "su - -c '${app_command}'"
                else
                        x-terminal-emulator -e "echo 'Command must be run as root user: ${app_command}'"
                fi
        fi
fi

具体代码可以查看

https://gitee.com/jay763190097/timeshift-jay

Reply View the author
青稚
deepin
2023-06-14 15:34
#2
风吹过的绿洲

自己摸索了一个傻瓜的方案:

image.png

  1. copy 一份timeshift默认的配置,并更改为自己的配置timeshift-data.json

这里的磁盘uuid我选择了另一个磁盘,防止一块磁盘突然损坏导致数据丢失,只备份数据排除其他所有内容。

系统备份有默认的timeshift定时任务。

第一次备份的话,do_first_run要改为true,注意这里需要修改为自己存储备份磁盘的uuid,输入blkid查询

jay@jay-PC:~$ blkid
/dev/nvme0n1p9: LABEL="work" UUID="df1ec32f-3950-484c-9d8b-0126e908e4e5" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="work" PARTUUID="712a0854-7f1c-4465-a7f6-39ca93fb17c4"
/dev/nvme0n1p7: LABEL="dde_home" UUID="722fad97-7ae2-48cf-9493-c1fcbaff044e" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="dde_home" PARTUUID="885d2978-c497-48c3-8647-e76ff306a4ea"
/dev/nvme0n1p5: LABEL="Roota" UUID="b1554732-8c79-4e88-ad79-47d398b35ddc" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="Basic data partition" PARTUUID="5d1a9b1e-9c13-4aac-a645-a30eaf8c3632"
/dev/nvme0n1p3: LABEL="SWAP" UUID="cea73cfa-6483-49fd-957a-129c81231fb5" TYPE="swap" PARTLABEL="primary" PARTUUID="57e3098c-5463-4c53-8690-d27dc34ccdf5"
/dev/nvme0n1p1: LABEL_FATBOOT="EFI" LABEL="EFI" UUID="547C-1CAF" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="EFI system partition" PARTUUID="aa0018e6-8b25-42ec-8dbc-3700757826b3"
/dev/nvme0n1p8: LABEL="Roota" UUID="704b2ebd-38e8-4e0c-acb7-2fa905c4df69" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="Roota" PARTUUID="fe5bb8fc-5107-4cfd-a15a-0c9c67c56434"
/dev/nvme0n1p6: LABEL="_dde_data" UUID="fe81a311-c7e9-4fb7-be56-039f9590ac7a" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="v23_home" PARTUUID="6e5b62e1-6c69-467f-8d5a-e4b46d065ac5"
/dev/nvme0n1p4: LABEL="Boot" UUID="9bc73b96-733e-4a07-9319-e7c970d35015" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="Boot" PARTUUID="17e1ae6d-7492-48c8-b0bb-47329b37853b"
/dev/nvme1n1p4: LABEL="win10-rec" BLOCK_SIZE="512" UUID="34945552945517A6" TYPE="ntfs" PARTUUID="e57cff40-7986-4e96-9ec5-f678f8e801bd"
/dev/nvme1n1p9: LABEL="apps" UUID="0bf94322-a57d-4065-bed0-b45a22c5c8bf" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="a282be2a-5082-4fd3-a587-4ec593f5e690"
/dev/nvme1n1p7: LABEL="data" BLOCK_SIZE="512" UUID="C022AA4B225A6D42" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="820ceea7-ec32-4d56-aca0-cbdacfaba321"
/dev/nvme1n1p10: LABEL="data_backup" UUID="bd99e96e-4e68-4b24-b5ff-29d54f5a9af6" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="6d6dae64-63a7-4b0a-8a88-5edd00b939bb"
/dev/nvme1n1p5: LABEL="soft" BLOCK_SIZE="512" UUID="2D97AD940A9AD661" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="6cae6bc0-7ece-43e7-84e0-76993b5837fa"
/dev/nvme1n1p3: LABEL="win10" BLOCK_SIZE="512" UUID="E88A37248A36EEA6" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="e8fd20fe-1eca-4f4d-8672-46d6c8637d87"
/dev/nvme1n1p1: SEC_TYPE="msdos" LABEL_FATBOOT="EFI" LABEL="EFI" UUID="DA18-EBFA" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="EFI system partition" PARTUUID="f6757783-bcbe-4795-a61c-1c097dc6c475"
/dev/nvme1n1p8: UUID="32912f5b-c4b4-47b8-8480-58e5b64bf490" BLOCK_SIZE="4096" TYPE="apfs" PARTLABEL="Basic data partition" PARTUUID="3f88f26f-5778-4a29-8959-67c8d46fdee8"
/dev/nvme1n1p6: LABEL="doc" BLOCK_SIZE="512" UUID="D4FA828F299D817A" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="9f68d4fa-6ac8-4818-913e-284ed3410a14"

╭─jay at jay-PC in /etc/timeshift 
╰─λ ls
default.json  timeshift-data.json  timeshift.json
╭─jay at jay-PC in /etc/timeshift 
╰─λ cat timeshift-data.json 
{
  "backup_device_uuid" : "bd99e96e-4e68-4b24-b5ff-29d54f5a9af6",
  "parent_device_uuid" : "",
  "do_first_run" : "false",
  "btrfs_mode" : "false",
  "include_btrfs_home_for_backup" : "false",
  "include_btrfs_home_for_restore" : "false",
  "stop_cron_emails" : "false",
  "schedule_monthly" : "false",
  "schedule_weekly" : "true",
  "schedule_daily" : "false",
  "schedule_hourly" : "false",
  "schedule_boot" : "false",
  "count_monthly" : "2",
  "count_weekly" : "2",
  "count_daily" : "5",
  "count_hourly" : "6",
  "count_boot" : "5",
  "snapshot_size" : "8854739085",
  "snapshot_count" : "103914",
  "date_format" : "%Y-%m-%d %H:%M:%S",
  "exclude" : [
    "/home/jay/Documents/WeChat Files/**",
    "/home/osroot/**",
    "+ /opt/***",
    "+ /home/***",
    "/***"
  ],
  "exclude-apps" : [
  ]
}

原则是排除的文件要写在添加的文件夹前面,这样才可以实现最终备份的文件不包含排除的文件。

如上,因为微信时间久了,占用空间很大,我就排除了。(之前排除一直失败,后面才知道是顺序的问题)

  1. 脚本中增加如下内容

╭─jay at jay-PC in /timeshift 
╰─λ cat back-data.sh  
#!/bin/bash
timeshift_sys=/etc/timeshift/timeshift.json
timeshift_data=/etc/timeshift/timeshift-data.json
timeshift_bak=/etc/timeshift/timeshift-bak.json
# 这里加了判断,防止中断导致的文件混乱
if [[ -f $timeshift_sys && -f $timeshift_data ]]; then
    mv /etc/timeshift/timeshift.json /etc/timeshift/timeshift-bak.json
    mv /etc/timeshift/timeshift-data.json /etc/timeshift/timeshift.json
fi
timeshift --check --scripted
if [[ -f $timeshift_sys && -f $timeshift_bak ]]; then
    mv /etc/timeshift/timeshift.json /etc/timeshift/timeshift-data.json
    mv /etc/timeshift/timeshift-bak.json /etc/timeshift/timeshift.json
fi

  1. 复制一个timeshift的定时任务
╭─jay at jay-PC in /etc/cron.d 
╰─λ sudo cp timeshift-hourly timeshift-data
╭─jay at jay-PC in /etc/cron.d 
╰─λ cat timeshift-data 
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
30 * * * * root /timeshift/back-data.sh
  1. 手动触发
╭─jay at jay-PC in /home/jay 
╰─λ sudo /timeshift/back-data.sh 
请输入密码:
验证成功
Mounted '/dev/nvme1n1p10' at '/run/timeshift/285816/backup'
Weekly snapshots are enabled
Last weekly snapshot is 0 days old
------------------------------------------------------------------------------
Added cron task: /etc/cron.d/timeshift-hourly

Found stale mount for device '/dev/nvme1n1p10' at path '/run/timeshift/285816/backup'
Unmounted successfully

不影响正常的系统备份
image.png

新的解决方案:

2024-08-18之后,通过修改timeshift的源代码,传入timeshift-data.json配置文件,现在不需要几个配置文件动态切换了。


复制了一个timeshift-launcher为timeshift-launcher-data,并在命令后加相应的配置文件,这个需要编译新的代码才能够支持。老的timeshift仍可以使用原来的方案

╭─jay at jay-PC in /usr/local/bin 
╰─λ cat timeshift-launcher-data 
#!/usr/bin/env bash

app_command='timeshift-gtk --config /opt/apps/com.idaoteng.data-bak/timeshift-data.json'

if [ "$(id -u)" -eq 0 ]; then
        # user is admin
        ${app_command}
else
        # user is not admin
        if echo $- | grep "i" >/dev/null 2>&1; then
                # script is running in interactive mode
                su - -c "${app_command}"
        else
                # script is running in non-interactive mode
                if [ "$XDG_SESSION_TYPE" = "wayland" ] ; then
                        xhost +SI:localuser:root
                        pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY ${app_command}
                        xhost -SI:localuser:root
                        xhost
                elif command -v pkexec >/dev/null 2>&1; then
                        pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY ${app_command}
                elif command -v sudo >/dev/null 2>&1; then
                        x-terminal-emulator -e "sudo ${app_command}"
                elif command -v su >/dev/null 2>&1; then
                        x-terminal-emulator -e "su - -c '${app_command}'"
                else
                        x-terminal-emulator -e "echo 'Command must be run as root user: ${app_command}'"
                fi
        fi
fi

具体代码可以查看

https://gitee.com/jay763190097/timeshift-jay

like

Reply View the author
vivian_me
deepin testing team
2023-06-14 19:27
#3

哇,感谢分享,我可以分享到WIKI吗

Reply View the author
风吹过的绿洲
deepin
2023-06-14 20:45
#4
vivian_me

哇,感谢分享,我可以分享到WIKI吗

可以的

Reply View the author
阿尼樱奈奈
Moderator
2023-06-14 22:19
#5

感谢分享。👍🏻👍🏻👍🏻

Reply View the author
风吹过的绿洲
deepin
2023-06-16 07:26
#6
阿尼樱奈奈

感谢分享。👍🏻👍🏻👍🏻

因为当丢失数据的时候才知道,备份太重要了!

Reply View the author
风吹过的绿洲
deepin
2023-06-19 03:02
#7

改天研究去改一下源代码,实现此功能

Reply View the author