Duplicity

来自 ArchWiki

Duplicity 是一个网络备份程序。

它可以将目录和文件的快照保存到远程 OpenPGP 加密的 tar 文件中,该文件充当备份仓库。与远程备份仓库的连接可以通过以下任何协议进行:rsync、ftp、HSI、WebDAV、Tahoe-LAFS 或 Amazon S3。

备份是细粒度增量的,这意味着只存储文件中自上次快照以来的更改。

安装

安装 duplicity 软件包。

前端

  • Duply — Duplicity 的 shell 前端。它在配置文件中管理备份作业设置,并允许批量执行命令。
https://duply.net/Main_Page || duplyAUR
  • Déjà Dup Backups — 简单的备份工具,隐藏了备份正确方式的复杂性,并使用 Duplicity 作为后端。
https://apps.gnome.org/DejaDup/ || deja-dup

基本用法

进行备份

要通过 scp/ssh 协议将本地文件夹 /home/me 备份到主机 other.host 上的远程位置 /usr/backup,请使用

$ duplicity /home/me scp://uid@other.host//usr/backup

首次运行此命令时,它将创建完整备份。再次运行完全相同的命令会导致对现有备份仓库进行增量备份。

其他命令行选项允许

  • 从备份中包含或排除特定文件和目录(使用 shell 模式或正则表达式)
  • 微调备份的加密和签名

从备份恢复文件

要将本地文件夹 /home/me 恢复到保存在主机 other.host 上的远程仓库 /usr/backup 中的最新快照状态,请执行

$ duplicity scp://uid@other.host//usr/backup /home/me 

请注意,与上面的备份命令相比,参数的顺序相反。URL 参数始终被视为备份仓库,而本地路径参数被视为与备份同步的目录。(本地备份仓库需要使用 file:// 协议前缀显式指定!)

存在其他命令行选项以允许

  • 恢复特定文件而不是整个仓库
  • 将文件恢复到特定日期的状态,而不是最新的可用快照

仓库检查和维护

存在一些其他命令行选项,用于将仓库状态与本地文件状态进行比较,以及删除旧快照,以便仅保留固定数量的快照或仅保留比给定日期新的快照。

有关详细信息,请参见 duplicity(1)

备份脚本示例

#!/bin/sh
## Remote backup script. Requires duplicity and gpg-agent with the keys and passphrases loaded as root.
## Uses separate encryption and signing keys
## Usage:  'backup_remote.sh'

enc_key=44D79E41
sign_key=F5C978E3
src="/mnt/backup/"
dest="scp://destination.com//backups/homeserver"

# Keychain is used to source the ssh-agent keys when running from a cron job
type -P keychain &>/dev/null || { echo "I require keychain but it's not installed.  Aborting." >&2; exit 1; }
eval `keychain --eval web_rsa` || exit 1
## Note: can't use keychain for gpg-agent because it doesn't currently (2.7.1) read in all the keys correctly.
## Gpg will ask for a passphrase twice for each key...once for encryption/decryption and once for signing.
## This makes unattended backups impossible, especially when trying to resume an interrupted backup.
if [ -f "${HOME}/.gnupg/gpg-agent-info" ]; then
      . "${HOME}/.gnupg/gpg-agent-info"
      export GPG_AGENT_INFO
fi

duplicity --use-agent \
         --verbosity notice \
         --encrypt-key "$enc_key" \
         --sign-key "$sign_key" \
         --full-if-older-than 60D \
         --num-retries 3 \
         --asynchronous-upload \
         --volsize 100 \
         --archive-dir /root/.cache/duplicity \
         --log-file /var/log/duplicity.log \
         --exclude /mnt/backup/fsarchiver \
         --exclude '**rdiff-backup-data' \
         "$src" "$dest"
注意: 当前版本的 pinentry (0.8.1-3) 存在一个问题,即当使用 su - 或 sudo 以 root 身份登录时,不允许为 root gpg-agent 输入密码。如果您要访问不允许(或不希望!)直接 root ssh 登录的远程服务器,则必须修补 pinentry 或在运行 pinentry 之前 chown root `tty`。当以非 root 用户身份运行 gpg-agent 时,这不是问题。

如果您想在 root 登录时启动 gpg-agent,然后在您方便时缓存 gpg-agent 的密码,您可以将这些函数添加到您的 /root/.bashrc

function gpg_start {
       gnupginf="${HOME}/.gnupg/gpg-agent-info"
       if pgrep -u "${USER}" gpg-agent >/dev/null 2>&1; then
           eval "$(cat $gnupginf)"
           eval "$(cut -d= -f1 < $gnupginf | xargs echo export)"
       else
           eval "$(gpg-agent -s --daemon --write-env-file $gnupginf)"
       fi
}
function keys {
       touch test-gpg.txt
       touch test-gpg.txt1
       gpg -r 'Duplicity Encryption Key' -e test-gpg.txt
       gpg -r 'Duplicity Signature Key' -e test-gpg.txt1
       gpg -u <signing key> --detach-sign test-gpg.txt
       gpg -u <encryption key> --detach-sign test-gpg.txt1
       gpg -d test-gpg.txt.gpg
       gpg -d test-gpg.txt1.gpg
       rm test-gpg.txt*
}
gpg_start

故障排除

如果您遇到围绕“设备不适当的 ioctl”的 gpg 错误,则很可能与 gpg 版本 2.1 及更高版本中 gpg 代理行为的更改有关。有关更多信息,请参见此线程。一般来说,需要显式允许程序向 gpg 代理提供密码,而不是提示用户。

补救此问题的步骤在 GnuPG#无人值守密码 中概述。

参见