GNU/Linux >> Linux の 問題 >  >> Ubuntu

Ubuntu16.04で重複を使用してバックアップする方法

Duplicityは、リモートまたはローカルのファイルサーバーにアップロードできる、暗号化され、デジタル署名された、バージョン管理されたtar形式のバックアップボリュームを提供するソフトウェアアプリケーションです。 Ubuntu、Linux Mint、Debian、Fedoraなどのすべての主要なLinuxディストリビューションでサポートされています。 Duplicityは、SSH / SCP、Rsync、FTP、WebDAVなどを使用してファイルシステムまたはサーバー間でローカルまたはリモートでファイルを同期することをサポートします。

このチュートリアルでは、重複を設定し、それを使用してUbuntuサーバーのバックアップを安全に自動化する方法について説明します。

前提条件

リポジトリからUbuntuのすべてのデフォルトパッケージをインストールする必要があります。

$apt-get update
$apt-get install ncftp python-paramiko python-pycryptopp lftp python-boto python-dev librsync-dev

重複のインストール

このコマンドを実行するだけで、リポジトリパッケージから重複をインストールできます。

$ apt install duplicity

インストール後に重複バージョンを確認しましょう。

$ duplicity -V
duplicity 0.7.06

SSHおよびGPGキーを作成する

次に、SSHキーを使用して、パスワードを入力せずにリモートシステムで安全に認証する必要があります。また、データをバックアップ場所に転送する前に、GPGキーを使用してデータを暗号化します。これらのキーは、サーバー間の安全な相互作用を提供します。

rootユーザーからRSA2048ビット暗号化SSHキーを生成して、バックアップマシンへのパスワードなしのログインを許可しましょう。

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:gxdDFAjm6AUSi7LzczlDE1orygCzplmjQCqBFkxJ8Dc root@duplicity-01
The key's randomart image is:
+---[RSA 2048]----+
|O=o o. o+. |
|o*.= .. |
|Ooo E o |
|** = + . o |
|O.* + . S |
|BB + o . . |
|=.o = |
| o o |
| |
+----[SHA256]-----+

次に、このRSAキーをリモートバックアップの場所にコピーします。

$ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '45.55.165.191 (45.55.165.191)' can't be established.
ECDSA key fingerprint is SHA256:XhFoQ3/mIsjGH7RfMwH6m0MHbj1B1kR4Sug5vfTQLdU.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

したがって、ホストからパスワードを提供しなくても、リモートバックアップサーバーにアクセスできます。バックアップを保存するには、バックアップ先の場所を作成する必要があります。リモートバックアップサーバーにDuplicityフォルダを作成しました。

$mkdir -p remotebackup/Duplicity

次のステップは、GPGキーを作成することです。 GPGキーは、サーバー間で転送されるデータのセキュリティと暗号化を強化するために使用されます。以下のコマンドを実行するだけで作成できます:

$gpg --gen-key

このコマンドはGPGキーを作成し、それを/root/.gnupg/フォルダーに保存します。このキー生成はインタラクティブであり、プロセス中にさまざまな質問を選択して回答する必要があります。

$ gpg --gen-key
gpg (GnuPG) 1.4.20; Copyright (C) 2015 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Please select what kind of key you want:
(1) RSA and RSA (default)
(2) DSA and Elgamal
(3) DSA (sign only)
(4) RSA (sign only)
Your selection?
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
Requested keysize is 2048 bits
Please specify how long the key should be valid.
0 = key does not expire
<n> = key expires in n days
<n>w = key expires in n weeks
<n>m = key expires in n months
<n>y = key expires in n years
Key is valid for? (0)
Key does not expire at all
Is this correct? (y/N) y

You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
"Heinrich Heine (Der Dichter) <[email protected]>"

Real name: Saheetha
Email address: [email protected]
Comment:
You selected this USER-ID:
"Saheetha <[email protected]>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
You need a Passphrase to protect your secret key.

gpg: gpg-agent is not available in this session
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.

Not enough random bytes available. Please do some other work to give
the OS a chance to collect more entropy! (Need 287 more bytes)

この時点で、エントロピーを生成するように求められます。エントロピーは基本的にシステムの予測不可能性です。実際にランダムなキーを作成するには、VPSにエントロピーが必要です。このメッセージが強調表示された部分として表示される場合は、havegedをインストールする必要があります パッケージを使用してランダムなキーを作成し、コマンドを再試行してください。

$apt-get install haveged

このパッケージは、デフォルトのリポジトリからインストールできます。インストール後、構成ファイル/ etc / default/havegedで次の設定が以下のように更新されていることを確認する必要があります。

DAEMON_ARGS="-w 1024"

最後に、起動時に起動するように構成されていることを確認してください:

$update-rc.d haveged defaults

これを設定した後、gpg --gen-keyを再開できます。 もう一度コマンドを実行すると、次のようにプロセスが完了します。

$ gpg --gen-key
gpg (GnuPG) 1.4.20; Copyright (C) 2015 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Please select what kind of key you want:
(1) RSA and RSA (default)
(2) DSA and Elgamal
(3) DSA (sign only)
(4) RSA (sign only)
Your selection?
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
Requested keysize is 2048 bits
Please specify how long the key should be valid.
0 = key does not expire
<n> = key expires in n days
<n>w = key expires in n weeks
<n>m = key expires in n months
<n>y = key expires in n years
Key is valid for? (0)
Key does not expire at all
Is this correct? (y/N)
Key is valid for? (0)
Key does not expire at all
Is this correct? (y/N) y

You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
"Heinrich Heine (Der Dichter) <[email protected]>"

Real name: Saheetha
Email address: [email protected]
Comment:
You selected this USER-ID:
"Saheetha <[email protected]>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
You need a Passphrase to protect your secret key.

gpg: gpg-agent is not available in this session
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
+++++
...+++++
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
.................+++++
........+++++
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key F03B3360 marked as ultimately trusted
public and secret key created and signed.

gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
pub 2048R/F03B3360 2016-11-25
Key fingerprint = F262 785A B5E6 25E9 56E7 4484 3354 43D9 F03B 3360
uid Saheetha <[email protected]>
sub 2048R/C4D85223 2016-11-25

このコマンドを実行して、gpgキー情報を取得することもできます。

$ gpg --list-keys
/root/.gnupg/pubring.gpg
------------------------
pub 2048R/F03B3360 2016-11-25
uid Saheetha <[email protected]>
sub 2048R/C4D85223 2016-11-25

重複の使用方法

バックアップするダミーファイルのフォルダを作成して、重複システムの初期テストを実行してみましょう。テストフォルダを作成し、バックアップするファイルをいくつか作成できます。

$ mkdir Test-DoC
$ cd Test-DoC
$ touch file{1..100}

これで、このコマンドを実行して、サーバーをリモートバックアップサーバーにバックアップできます。

$root@duplicity-01:~# duplicity /root/Test-DoC/ sftp://[email protected]//remotebackup/Duplicity
The authenticity of host '45.55.165.191' can't be established.
SSH-RSA key fingerprint is 20:9d:3b:fe:af:df:d1:40:5d:bc:f4:2c:6e:30:54:e0.
Are you sure you want to continue connecting (yes/no)? yes
Local and Remote metadata are synchronized, no sync needed.
Last full backup date: none
GnuPG passphrase for decryption:
Retype passphrase for decryption to confirm:
No signatures found, switching to full backup.
--------------[ Backup Statistics ]--------------
StartTime 1480059313.48 (Fri Nov 25 07:35:13 2016)
EndTime 1480059313.53 (Fri Nov 25 07:35:13 2016)
ElapsedTime 0.05 (0.05 seconds)
SourceFiles 101
SourceFileSize 4096 (4.00 KB)
NewFiles 101
NewFileSize 4096 (4.00 KB)
DeletedFiles 0
ChangedFiles 0
ChangedFileSize 0 (0 bytes)
ChangedDeltaSize 0 (0 bytes)
DeltaEntries 101
RawDeltaSize 0 (0 bytes)
TotalDestinationSizeChange 1029 (1.00 KB)
Errors 0
-------------------------------------------------

リモートサーバーにログインすると、バックアップを確認できます。

root@duplicity-02:/remotebackup/Duplicity# ll
total 20
drwxr-xr-x 2 root root 4096 Nov 25 07:35 ./
drwxr-xr-x 3 root root 4096 Nov 25 07:11 ../
-rw-r--r-- 1 root root 438 Nov 25 07:35 duplicity-full.20161125T073505Z.manifest.gpg
-rw-r--r-- 1 root root 1029 Nov 25 07:35 duplicity-full.20161125T073505Z.vol1.difftar.gpg
-rw-r--r-- 1 root root 1596 Nov 25 07:35 duplicity-full-signatures.20161125T073505Z.sigtar.gpg

これらのファイルには、バックアップ情報が含まれている必要があります。ファイルをテストしているだけなので、ファイル全体を削除できます。

サーバー全体のバックアップの作成

最初のサーバーバックアップを作成しましょう。 / proc、/ sys、/ tmpなどの一部のフォルダーを除いて、ルートサーバー全体のバックアップを作成しています。ここでは、以前に生成されたGPG公開鍵と言い換えを使用して、データを保護および暗号化する必要があります。これは、バックアップ作成の一般的な構文です。

duplicity --encrypt-key Pub-key_from_GPG --exclude files_to_exclude --include files_to_include path_to_back_up sftp://root@remotebackupHostname//remotebackup/duplicity

このコマンドを実行してサーバーをバックアップしました。

root@duplicity-01:~# PASSPHRASE="docker" duplicity --encrypt-key F03B3360 --exclude /proc --exclude /sys --exclude /tmp  / sftp://[email protected]//remotebackup/Duplicity/
Local and Remote metadata are synchronized, no sync needed.
Last full backup date: none
No signatures found, switching to full backup.
--------------[ Backup Statistics ]--------------
StartTime 1480143194.50 (Sat Nov 26 06:53:14 2016)
EndTime 1480143304.84 (Sat Nov 26 06:55:04 2016)
ElapsedTime 110.34 (1 minute 50.34 seconds)
SourceFiles 69101
SourceFileSize 885736795 (845 MB)
NewFiles 69101
NewFileSize 885736795 (845 MB)
DeletedFiles 0
ChangedFiles 0
ChangedFileSize 0 (0 bytes)
ChangedDeltaSize 0 (0 bytes)
DeltaEntries 69101
RawDeltaSize 847552198 (808 MB)
TotalDestinationSizeChange 345904578 (330 MB)
Errors 0
-------------------------------------------------

このバックアッププロセスを完了するには、しばらく時間がかかります。バックアップを実行するのはこれが初めてなので、重複すると完全バックアップが作成されます。重複により、データのチャンクがボリュームに分割され、ファイル転送が簡素化されます。

root@duplicity-02:/remotebackup/Duplicity# ll
total 354864
drwxr-xr-x 2 root root 4096 Nov 26 06:55 ./
drwxr-xr-x 3 root root 4096 Nov 25 07:11 ../
-rw-r--r-- 1 root root 303666 Nov 26 06:55 duplicity-full.20161126T065314Z.manifest.gpg
-rw-r--r-- 1 root root 26241374 Nov 26 06:54 duplicity-full.20161126T065314Z.vol10.difftar.gpg
-rw-r--r-- 1 root root 26192752 Nov 26 06:54 duplicity-full.20161126T065314Z.vol11.difftar.gpg
-rw-r--r-- 1 root root 26206134 Nov 26 06:54 duplicity-full.20161126T065314Z.vol12.difftar.gpg
-rw-r--r-- 1 root root 26196266 Nov 26 06:54 duplicity-full.20161126T065314Z.vol13.difftar.gpg
-rw-r--r-- 1 root root 4999361 Nov 26 06:55 duplicity-full.20161126T065314Z.vol14.difftar.gpg
-rw-r--r-- 1 root root 26256306 Nov 26 06:53 duplicity-full.20161126T065314Z.vol1.difftar.gpg
-rw-r--r-- 1 root root 26216804 Nov 26 06:53 duplicity-full.20161126T065314Z.vol2.difftar.gpg
-rw-r--r-- 1 root root 26198345 Nov 26 06:53 duplicity-full.20161126T065314Z.vol3.difftar.gpg
-rw-r--r-- 1 root root 26197666 Nov 26 06:53 duplicity-full.20161126T065314Z.vol4.difftar.gpg
-rw-r--r-- 1 root root 26237799 Nov 26 06:53 duplicity-full.20161126T065314Z.vol5.difftar.gpg
-rw-r--r-- 1 root root 26218126 Nov 26 06:53 duplicity-full.20161126T065314Z.vol6.difftar.gpg
-rw-r--r-- 1 root root 26252966 Nov 26 06:53 duplicity-full.20161126T065314Z.vol7.difftar.gpg
-rw-r--r-- 1 root root 26234136 Nov 26 06:54 duplicity-full.20161126T065314Z.vol8.difftar.gpg
-rw-r--r-- 1 root root 26256543 Nov 26 06:54 duplicity-full.20161126T065314Z.vol9.difftar.gpg
-rw-r--r-- 1 root root 17136137 Nov 26 06:55 duplicity-full-signatures.20161126T065314Z.sigtar.gpg

新しい液滴で、私の構成はリモートシステムに転送された15のボリュームを作成しました。バックアップサーバーにサーバーの完全バックアップがあるため、次のバックアップは増分バックアップになります。これらはより高速で、必要なデータ転送時間も短くなります。このコマンドを実行するだけで、別の完全バックアップを強制することもできます。

$PASSPHRASE="docker" duplicity full --encrypt-key F03B3360 --exclude /proc --exclude /sys --exclude /tmp  / sftp://[email protected]//remotebackup/Duplicity/

バックアップのスケジュール

これで、バックアップスクリプトを自動的に実行するように設定することで、バックアップを毎日または毎週スケジュールする方法を学ぶことができます。まず、/ rootフォルダーの下にパスフレーズファイルを作成して、言い換えのインタラクティブなプロンプトなしでパスフレーズを自動的に渡します。

$cat /root/.passphrase
PASSPHRASE="docker"

ファイルのアクセス許可を保護し、600に制限します。

毎日バックアップを作成する方法を見てみましょう。 /etc/cron.dailyフォルダー内にバックアップスクリプトを作成する必要があります。このフォルダ内に設定されているすべてのcronジョブは毎日実行されます。

$:/etc/cron.daily# cat duplicity.inc
#!/bin/sh

test -x $(which duplicity) || exit 0
. /root/.passphrase

export PASSPHRASE
$(which duplicity) --encrypt-key F03B3360 --exclude /proc --exclude /sys --exclude /tmp --exclude /var / sftp://[email protected]//remotebackup/Duplicity/

スクリプトの実行可能権限を必ず付与してください。

$:/etc/cron.daily# chmod +x duplicity.inc

このスクリプトを実行して、すべてが正しく機能することを確認できます。

root@duplicity-01:/etc/cron.daily# ./duplicity.inc
Local and Remote metadata are synchronized, no sync needed.
Last full backup date: Sat Nov 26 06:53:14 2016
--------------[ Backup Statistics ]--------------
StartTime 1480564726.91 (Thu Dec 1 03:58:46 2016)
EndTime 1480564748.83 (Thu Dec 1 03:59:08 2016)
ElapsedTime 21.91 (21.91 seconds)
SourceFiles 69108
SourceFileSize 899520535 (858 MB)
NewFiles 32
NewFileSize 20761841 (19.8 MB)
DeletedFiles 13
ChangedFiles 9
ChangedFileSize 2779 (2.71 KB)
ChangedDeltaSize 0 (0 bytes)
DeltaEntries 54
RawDeltaSize 20737072 (19.8 MB)
TotalDestinationSizeChange 17502790 (16.7 MB)
Errors 0
-------------------------------------------------

同様に、バックアップをスケジュールして、完全バックアップを毎週強制し、維持することができます。増分バックアップは、変更が積み重なるにつれてますます扱いにくくなるためです。ベースを更新するために、毎週の完全バックアップを構成します。

/etc/cron.weeklyフォルダーの下に重複する完全バックアップスクリプトを作成して、これを実行しましょう。このディレクトリ内にduplicity.fullスクリプトを作成できます。

$:/etc/cron.weekly# chmod +x duplicity.full
$:/etc/cron.weekly# cat duplicity.full
#!/bin/sh

test -x $(which duplicity) || exit 0
. /root/.passphrase

export PASSPHRASE
$(which duplicity) full --encrypt-key F03B3360 --exclude /proc --exclude /sys --exclude /tmp --exclude /var / sftp://[email protected]//remotebackup/Duplicity/

このコマンドをduplicity.fullスクリプトに追加して、古いバックアップファイルをクリーンアップすることもできます。このコマンドを追加して、3つのバックアップとそれに対応する増分バックアップのみを保持します。

$(which duplicity) remove-all-but-n-full 3 --force sftp://[email protected]//remotebackup/Duplicity/

したがって、毎週のスクリプトはサーバーの完全バックアップを作成し、サーバーから不要な古いバックアップファイルをすべて削除します。スクリプトを手動で実行して、その動作をテストできます。

$:/etc/cron.weekly# ./duplicity.full
Local and Remote metadata are synchronized, no sync needed.
Last full backup date: Sat Nov 26 06:53:14 2016
--------------[ Backup Statistics ]--------------
StartTime 1480570674.29 (Thu Dec 1 05:37:54 2016)
EndTime 1480570791.54 (Thu Dec 1 05:39:51 2016)
ElapsedTime 117.26 (1 minute 57.26 seconds)
SourceFiles 69109
SourceFileSize 906577560 (865 MB)
NewFiles 69109
NewFileSize 906577560 (865 MB)
DeletedFiles 0
ChangedFiles 0
ChangedFileSize 0 (0 bytes)
ChangedDeltaSize 0 (0 bytes)
DeltaEntries 69109
RawDeltaSize 868388867 (828 MB)
TotalDestinationSizeChange 363488138 (347 MB)
Errors 0
-------------------------------------------------

Local and Remote metadata are synchronized, no sync needed.
Last full backup date: Thu Dec 1 05:37:53 2016
No old backup sets found, nothing deleted.

結論

おめでとう!完全に機能する自動バックアップソリューションをすぐに使用できます。他のバックアップソリューションに対するDuplicityの主な利点の1つは、GnuPGキーを使用してデータを暗号化することです。また、優れた自動バックアップソリューションを提供し、単純なCronジョブを使用して実行できます。この記事がお役に立てば幸いです。これに関する貴重なコメントや提案を投稿してください。


Ubuntu
  1. Ubuntu18.04でNginxを使用してWordPressをインストールする方法

  2. Ubuntu20.04でLsyncdを使用してディレクトリを同期する方法

  3. Duplicity を Ubuntu にインストールするにはどうすればよいですか?

  1. Wireguardを使用してUbuntu20.04でVPNを作成する方法

  2. Ubuntu18.04でSSHキーを設定する方法

  3. Ubuntu20.04でSSHキーを設定する方法

  1. Ubuntu18.04にMavenをインストールする方法

  2. Ubuntu16.04でNetdataを使用してNginxを監視する方法

  3. Ubuntu20.04でDockerを使用してGiteaをインストールする方法