GNU/Linux >> Belajar Linux >  >> Ubuntu

Cara Mencadangkan Menggunakan Duplicity di Ubuntu 16.04

Duplicity adalah aplikasi perangkat lunak yang menyediakan volume cadangan format tar terenkripsi, ditandatangani secara digital, berversi yang dapat diunggah ke server file jarak jauh atau lokal. Ini didukung di semua distribusi Linux utama seperti Ubuntu, Linux Mint, Debian, Fedora, dan banyak lagi. Duplicity mendukung sinkronisasi file secara lokal atau jarak jauh antara sistem file atau server menggunakan, SSH/SCP, Rsync, FTP, WebDAV dll.

Dalam tutorial ini, saya akan menjelaskan tentang cara men-setup duplicity dan menggunakannya untuk mengotomatisasi backup secara aman di server Ubuntu Anda.

Prasyarat

Kita perlu menginstal semua paket default untuk Ubuntu dari repositori.

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

Instal Duplikat

Kita dapat menginstal duplikat dari paket repositori, hanya dengan menjalankan perintah ini.

$ apt install duplicity

Mari konfirmasi versi duplikasi setelah menginstal.

$ duplicity -V
duplicity 0.7.06

Buat Kunci SSH dan GPG

Selanjutnya, kita perlu menggunakan kunci SSH untuk mengautentikasi secara aman dengan sistem jarak jauh tanpa harus memberikan kata sandi. Kami juga menggunakan kunci GPG untuk mengenkripsi data sebelum kami mentransfernya ke lokasi cadangan. Kunci ini memberikan interaksi yang aman antar server.

Mari buat kunci SSH terenkripsi RSA 2048 bit dari pengguna root untuk memungkinkan login tanpa sandi ke mesin cadangan.

$ 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]-----+

Sekarang kami menyalin kunci RSA ini ke lokasi pencadangan jarak jauh kami.

$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.

Oleh karena itu, kami dapat mengakses server cadangan jarak jauh tanpa memberikan kata sandi dari host. Kita perlu membuat lokasi tujuan backup untuk menyimpan backup kita. Saya membuat folder Duplicity di server cadangan jarak jauh saya.

$mkdir -p remotebackup/Duplicity

Langkah kita selanjutnya adalah membuat kunci GPG. Kunci GPG digunakan untuk keamanan ekstra dan enkripsi data yang ditransfer di seluruh server. Buat hanya dengan menjalankan perintah di bawah ini:

$gpg --gen-key

Perintah ini akan membuat kunci GPG dan menyimpannya di bawah folder /root/.gnupg/. Pembuatan Kunci ini bersifat interaktif dan kita perlu memilih dan menanggapi berbagai pertanyaan selama proses berlangsung.

$ 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)

Pada titik ini, Anda diminta untuk menghasilkan entropi. Entropi pada dasarnya adalah ketidakpastian dalam sistem Anda. VPS Anda membutuhkan entropi untuk membuat kunci yang sebenarnya acak. Jika Anda menerima pesan ini sebagai bagian yang disorot, Anda perlu menginstal haveged paket untuk membuat kunci yang acak dan coba perintah lagi.

$apt-get install haveged

Paket ini dapat diinstal dari repositori default Anda. Setelah instalasi, Anda perlu memastikan bahwa pengaturan berikut diperbarui seperti di bawah ini dalam file konfigurasinya /etc/default/haveged.

DAEMON_ARGS="-w 1024"

Terakhir, pastikan sudah dikonfigurasi untuk memulai saat boot:

$update-rc.d haveged defaults

Setelah mengatur ini, Anda dapat memulai kembali gpg --gen-key perintah sekali lagi dan itu akan menyelesaikan proses dengan yang berikut.

$ 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

Anda juga bisa mendapatkan informasi kunci gpg Anda dengan menjalankan perintah ini.

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

Cara menggunakan Duplikat

Mari kita jalankan tes awal untuk sistem duplikat kita dengan membuat folder file dummy untuk dicadangkan. Anda dapat membuat folder uji dan membuat beberapa file untuk dicadangkan.

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

Sekarang, Anda dapat menjalankan perintah ini untuk mencadangkan server ke server cadangan jarak jauh kami.

$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
-------------------------------------------------

Anda dapat mengonfirmasi pencadangan dengan masuk ke server jarak jauh kami.

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

File-file ini diharapkan berisi informasi cadangan. Karena ini hanya menguji file, kami dapat menghapus seluruh file.

Membuat Cadangan Seluruh Server

Mari kita buat cadangan server pertama kita. Kami membuat seluruh cadangan server root tidak termasuk beberapa folder seperti /proc, /sys, dan /tmp. Di sini, kita perlu menggunakan kunci pub GPG dan Paraphrase yang dibuat sebelumnya untuk mengamankan dan mengenkripsi data kita. Ini adalah sintaks umum untuk pembuatan cadangan.

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

Saya telah menjalankan perintah ini untuk membuat cadangan server saya.

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
-------------------------------------------------

Ini akan memakan waktu untuk menyelesaikan proses pencadangan ini. Karena ini pertama kali kita menjalankan backup, duplicity akan membuat full backup. Duplikat membagi potongan data menjadi volume untuk menyederhanakan transfer file.

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

Pada tetesan baru, konfigurasi saya membuat 15 volume yang ditransfer ke sistem jarak jauh. Karena kami memiliki cadangan penuh untuk server saya di server cadangan kami, cadangan berikutnya adalah cadangan tambahan. Ini lebih cepat dan membutuhkan lebih sedikit waktu transfer data. Anda bahkan dapat memaksa pencadangan penuh lainnya hanya dengan menjalankan perintah ini.

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

Menjadwalkan Pencadangan kami

Sekarang kita dapat mempelajari cara menjadwalkan pencadangan harian atau mingguan dengan mengatur skrip pencadangan agar berjalan secara otomatis. Pertama-tama, mari kita buat file frasa sandi di bawah folder /root kita untuk meneruskan frasa sandi secara otomatis tanpa prompt interaktif untuk parafrase.

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

Amankan izin file dan batasi hingga 600.

Mari kita lihat cara membuat cadangan harian. Kita perlu membuat skrip cadangan di dalam folder /etc/cron.daily. Semua tugas cron yang diatur di dalam folder ini akan berjalan setiap hari.

$:/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/

Pastikan untuk memberikan izin yang dapat dieksekusi untuk skrip.

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

Kami dapat menjalankan skrip ini untuk memastikan semuanya berfungsi dengan baik.

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
-------------------------------------------------

Demikian pula, kami dapat menjadwalkan pencadangan untuk memaksa pencadangan penuh setiap minggu dan memelihara. Karena pencadangan inkremental akan semakin berat saat perubahan menumpuk. Kami akan mengonfigurasi pencadangan penuh mingguan untuk menyegarkan basis.

Mari lakukan ini dengan membuat skrip cadangan penuh duplikat di bawah folder /etc/cron.weekly. Anda dapat membuat skrip duplicity.full di dalam direktori ini.

$:/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/

Kami juga dapat menambahkan perintah ini ke skrip duplicity.full kami untuk membersihkan file cadangan lama. Tambahkan perintah ini untuk menyimpan hanya tiga cadangan dan cadangan tambahan yang sesuai.

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

Oleh karena itu, skrip mingguan kami akan membuat cadangan lengkap untuk server kami dan menghapus semua file cadangan lama yang tidak diinginkan dari server. Kami dapat menjalankan skrip kami secara manual untuk menguji kerjanya.

$:/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.

Kesimpulan

Selamat! Kami telah menyiapkan solusi pencadangan otomatis yang beroperasi penuh. Salah satu keuntungan utama Duplicity dibandingkan solusi pencadangan lainnya adalah ia mengenkripsi data menggunakan kunci GnuPG. Ini juga menyediakan solusi pencadangan otomatis yang hebat dan dapat dilakukan menggunakan pekerjaan Cron sederhana. Saya harap artikel ini bermanfaat untuk Anda! Silakan kirim komentar dan saran Anda yang berharga tentang ini.


Ubuntu
  1. Cara Menginstal WordPress Menggunakan Nginx di Ubuntu 18.04

  2. Cara Menyinkronkan Direktori Menggunakan Lsyncd di Ubuntu 20.04

  3. Bagaimana cara menginstal Duplicity di Ubuntu?

  1. Cara membuat VPN di Ubuntu 20.04 menggunakan Wireguard

  2. Cara Mengatur Kunci SSH di Ubuntu 18.04

  3. Cara Mengatur Kunci SSH di Ubuntu 20.04

  1. Cara Menginstal Maven di Ubuntu 18.04

  2. Cara Memantau Nginx menggunakan Netdata di Ubuntu 16.04

  3. Cara Menginstal Gitea menggunakan Docker di Ubuntu 20.04