{"id":13375,"date":"2019-04-01T23:04:37","date_gmt":"2019-04-01T23:04:37","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13375"},"modified":"2019-04-01T23:04:37","modified_gmt":"2019-04-01T23:04:37","slug":"how-to-create-encrypted-and-bandwidth-efficient-backups-using-duplicity-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/how-to-create-encrypted-and-bandwidth-efficient-backups-using-duplicity-in-linux\/","title":{"rendered":"How to Create Encrypted and Bandwidth-efficient Backups Using \u2018Duplicity\u2019 in Linux"},"content":{"rendered":"<p>Experience shows that you can never be too paranoid about system backups. When it comes to protecting and preserving precious data, it is best to go the extra mile and make sure you can depend on your backups if the need arises.<\/p>\n<div id=\"attachment_15878\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Create-Encrypted-Linux-File-System-Backups.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-15878\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Create-Encrypted-Linux-File-System-Backups-620x296.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Create-Encrypted-Linux-File-System-Backups-620x296.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Create-Encrypted-Linux-File-System-Backups.png 719w\" alt=\"Create Encrypted Linux File System Backups\" width=\"620\" height=\"296\" aria-describedby=\"caption-attachment-15878\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-15878\" class=\"wp-caption-text\">Duplicity \u2013 Create Encrypted Linux File System Backups<\/p>\n<\/div>\n<p>Even today, when some cloud and hosting providers offer automated backups for VPS\u2019s at a relatively low cost, you will do well to create your own\u00a0<a href=\"https:\/\/www.tecmint.com\/linux-disk-cloning-tools\/\" target=\"_blank\" rel=\"noopener\">backup strategy using your own tools<\/a>\u00a0in order to save some money and then perhaps use it to buy extra storage or get a bigger VPS.<\/p>\n<p>Sounds interesting? In this article we will show you how to use a tool called\u00a0<strong>Duplicity<\/strong>\u00a0to backup and encrypt file and directories. In addition, using incremental backups for this task will help us to save space.<\/p>\n<p>That said, let\u2019s get started.<\/p>\n<h3>Installing Duplicity<\/h3>\n<p>To install duplicity in Fedora-based distros, you will have to enable the EPEL repository first (you can omit this step if you\u2019re using Fedora itself):<\/p>\n<pre># yum update &amp;&amp; yum install epel-release\r\n<\/pre>\n<p>Then run,<\/p>\n<pre># yum install duplicity\r\n<\/pre>\n<p>For Debian and derivatives:<\/p>\n<pre># aptitude update &amp;&amp; aptitude install duplicity\r\n<\/pre>\n<p>In theory, many methods for connecting to a file server are supported although only\u00a0<a href=\"https:\/\/www.tecmint.com\/install-openssh-server-in-linux\/\" target=\"_blank\" rel=\"noopener\">ssh<\/a>\/<a href=\"https:\/\/www.tecmint.com\/scp-commands-examples\/\" target=\"_blank\" rel=\"noopener\">scp<\/a>\/<a href=\"https:\/\/www.tecmint.com\/sftp-command-examples\/\" target=\"_blank\" rel=\"noopener\">sftp<\/a>, local file access,\u00a0<a href=\"https:\/\/www.tecmint.com\/rsync-local-remote-file-synchronization-commands\/\" target=\"_blank\" rel=\"noopener\">rsync<\/a>,\u00a0<a href=\"https:\/\/www.tecmint.com\/ftp-interview-questions-and-answers\/\" target=\"_blank\" rel=\"noopener\">ftp<\/a>, HSI, WebDAV and Amazon S3 have been tested in practice so far.<\/p>\n<p>Once the installation completes, we will exclusively use\u00a0<strong>sftp<\/strong>\u00a0in various scenarios, both to back up and to restore the data.<\/p>\n<p>Our test environment consists of a\u00a0<strong>CentOS 7<\/strong>\u00a0box (to be backed up) and a\u00a0<strong>Debian 8<\/strong>\u00a0machine (backup server).<\/p>\n<h3>Creating SSH keys to access remote servers and GPG keys for encryption<\/h3>\n<p>Let\u2019s begin by creating the\u00a0<a href=\"https:\/\/www.tecmint.com\/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps\/\" target=\"_blank\" rel=\"noopener\">SSH keys in our CentOS<\/a>\u00a0box and transfer them to the\u00a0<strong>Debian<\/strong>\u00a0backup server.<\/p>\n<p>The below commands assumes the\u00a0<strong>sshd<\/strong>\u00a0daemon is listening on port\u00a0<strong>XXXXX<\/strong>\u00a0in the Debian server. Replace\u00a0<strong>AAA.BBB.CCC.DDD<\/strong>\u00a0with the actual IP of the remote server.<\/p>\n<pre># ssh-keygen -t rsa\r\n# ssh-copy-id -p XXXXX root@AAA.BBB.CCC.DDD\r\n<\/pre>\n<p>Then you should make sure that you can connect to the backup server without using a password:<\/p>\n<div id=\"attachment_15867\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Create-SSH-Keys.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-15867\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Create-SSH-Keys-620x225.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Create-SSH-Keys-620x225.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Create-SSH-Keys.png 675w\" alt=\"Create SSH Keys\" width=\"620\" height=\"225\" aria-describedby=\"caption-attachment-15867\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-15867\" class=\"wp-caption-text\">Create SSH Keys<\/p>\n<\/div>\n<p>Now we need to create the\u00a0<strong>GPG<\/strong>\u00a0keys that will be used for\u00a0<a href=\"https:\/\/www.tecmint.com\/disk-encryption-in-linux\/\" target=\"_blank\" rel=\"noopener\">encryption and decryption<\/a>\u00a0of our data:<\/p>\n<pre># gpg --gen-key\r\n<\/pre>\n<p>You will be prompted to enter:<\/p>\n<ol>\n<li>Kind of key<\/li>\n<li>Key size<\/li>\n<li>How long the key should be valid<\/li>\n<li>A passphrase<\/li>\n<\/ol>\n<div id=\"attachment_15868\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Create-GPG-Keys.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-15868\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Create-GPG-Keys-395x450.png\" sizes=\"auto, (max-width: 395px) 100vw, 395px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Create-GPG-Keys-395x450.png 395w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Create-GPG-Keys.png 514w\" alt=\"Create GPG Keys\" width=\"395\" height=\"450\" aria-describedby=\"caption-attachment-15868\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-15868\" class=\"wp-caption-text\">Create GPG Keys<\/p>\n<\/div>\n<p>To create the entropy needed for the creation of the keys, you can log on to the server via another terminal window and perform a few tasks or run some commands to generate entropy (otherwise you will have to wait for a long time for this part of the process to finish).<\/p>\n<p>Once the keys have been generated, you can list them as follows:<\/p>\n<pre># gpg --list-keys\r\n<\/pre>\n<div id=\"attachment_15869\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/List-Generated-GPG-Keys.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15869\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/List-Generated-GPG-Keys.png\" alt=\"List Generated GPG Keys\" width=\"517\" height=\"170\" aria-describedby=\"caption-attachment-15869\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-15869\" class=\"wp-caption-text\">List Generated GPG Keys<\/p>\n<\/div>\n<p>The string highlighted in yellow above is known as the public key ID, and is a requested argument to encrypt your files.<\/p>\n<h3>Creating a backup with Duplicity<\/h3>\n<p>To start simple, let\u2019s only backup the\u00a0<strong>\/var\/log<\/strong>\u00a0directory, with the exception of\u00a0<strong>\/var\/log\/anaconda<\/strong>\u00a0and\u00a0<strong>\/var\/log\/sa<\/strong>.<\/p>\n<p>Since this is our first backup, it will be a full one. Subsequent runs will create incremental backups (unless we add the full option with no dashes right next to duplicity in the command below):<\/p>\n<pre>PASSPHRASE=\"YourPassphraseHere\" duplicity --encrypt-key YourPublicKeyIdHere --exclude \/var\/log\/anaconda --exclude \/var\/log\/sa \/var\/log scp:\/\/root@RemoteServer:XXXXX\/\/backups\/centos7\r\n<\/pre>\n<p><strong>Make sure you don\u2019t miss the double slash in the above command!<\/strong>\u00a0They are used to indicate an absolute path to a directory named\u00a0<strong>\/backups\/centos7<\/strong>\u00a0in the backup box, and is where the backup files will be stored.<\/p>\n<p>Replace\u00a0<strong>YourPassphraseHere<\/strong>,\u00a0<strong>YourPublicKeyIdHere<\/strong>\u00a0and\u00a0<strong>RemoteServer<\/strong>\u00a0with the passphrase you entered earlier, the GPG public key ID, and with the IP or hostname of the backup server, respectively.<\/p>\n<p>Your output should be similar to the following image:<\/p>\n<div id=\"attachment_15872\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Create-Var-Partition-Backup.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15872\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Create-Var-Partition-Backup.png\" alt=\"Create \/var Partition Backup\" width=\"537\" height=\"403\" aria-describedby=\"caption-attachment-15872\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-15872\" class=\"wp-caption-text\">Create Backup using Duplicity<\/p>\n<\/div>\n<p>The image above indicates that a total of\u00a0<strong>86.3 MB<\/strong>\u00a0was backed up into a\u00a0<strong>3.22 MB<\/strong>\u00a0in the destination. Let\u2019s switch to the backup server to check on our newly created backup:<\/p>\n<div id=\"attachment_15873\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Confirm-Backup-File.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-15873\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Confirm-Backup-File-620x166.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Confirm-Backup-File-620x166.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Confirm-Backup-File.png 871w\" alt=\"Confirm Backup File\" width=\"620\" height=\"166\" aria-describedby=\"caption-attachment-15873\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-15873\" class=\"wp-caption-text\">Confirm Backup File<\/p>\n<\/div>\n<p>A second run of the same command yields a much smaller backup size and time:<\/p>\n<div id=\"attachment_15874\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Compress-Backup.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15874\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Compress-Backup.png\" alt=\"Compress Backup\" width=\"548\" height=\"387\" aria-describedby=\"caption-attachment-15874\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-15874\" class=\"wp-caption-text\">Compress Backup<\/p>\n<\/div>\n<h3>Restoring backups using Duplicity<\/h3>\n<p>To successfully restore a file, a directory with its contents, or the whole backup, the destination must not exist (duplicity will not overwrite an existing file or directory). To clarify, let\u2019s delete the\u00a0<strong>cron<\/strong>\u00a0log in the CentOS box:<\/p>\n<pre># rm -f \/var\/log\/cron\r\n<\/pre>\n<div id=\"attachment_15875\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Delete-Cron-Logs.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15875\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/09\/Delete-Cron-Logs.png\" alt=\"Delete Cron Logs\" width=\"532\" height=\"136\" aria-describedby=\"caption-attachment-15875\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-15875\" class=\"wp-caption-text\">Delete Cron Logs<\/p>\n<\/div>\n<p>The syntax to restore a single file from the remote server is:<\/p>\n<pre># PASSPHRASE=\"YourPassphraseHere\" duplicity --file-to-restore filename sftp:\/\/root@RemoteHost\/\/backups\/centos7 \/where\/to\/restore\/filename\r\n<\/pre>\n<p>where,<\/p>\n<ol>\n<li><strong>filename<\/strong>\u00a0is the file to be extracted, with a relative path to the directory that was backed up<\/li>\n<li><strong>\/where\/to\/restore<\/strong>\u00a0is the directory in the local system where we want to restore the file to.<\/li>\n<\/ol>\n<p>In our case, to restore the cron main log from the remote backup we need to run:<\/p>\n<pre># PASSPHRASE=\"YourPassphraseHere\" duplicity --file-to-restore cron sftp:\/\/root@AAA.BBB.CCC.DDD:XXXXX\/\/backups\/centos7 \/var\/log\/cron\r\n<\/pre>\n<p>The cron log should be restored to the desired destination.<\/p>\n<p>Likewise, feel free to delete a directory from\u00a0<strong>\/var\/log<\/strong>\u00a0and restore it using the backup:<\/p>\n<pre># rm -rf \/var\/log\/mail\r\n# PASSPHRASE=\"YourPassphraseHere\" duplicity --file-to-restore mail sftp:\/\/root@AAA.BBB.CCC.DDD:XXXXX\/\/backups\/centos7 \/var\/log\/mail\r\n<\/pre>\n<p>In this example, the\u00a0<strong>mail<\/strong>\u00a0directory should be restored to its original location with all its contents.<\/p>\n<h3>Other features of Duplicity<\/h3>\n<p>At any time you can display the list of archived files with the following command:<\/p>\n<pre># duplicity list-current-files sftp:\/\/root@AAA.BBB.CCC.DDD:XXXXX\/\/backups\/centos7\r\n<\/pre>\n<p>Delete backups older than 6 months:<\/p>\n<pre># duplicity remove-older-than 6M sftp:\/\/root@AAA.BBB.CCC.DDD:XXXXX\/\/backups\/centos7\r\n<\/pre>\n<p>Restore\u00a0<strong>myfile<\/strong>\u00a0inside directory\u00a0<strong>gacanepa<\/strong>\u00a0as it was 2 days and 12 hours ago:<\/p>\n<pre># duplicity -t 2D12h --file-to-restore gacanepa\/myfile sftp:\/\/root@AAA.BBB.CCC.DDD:XXXXX\/\/remotedir\/backups \/home\/gacanepa\/myfile\r\n<\/pre>\n<p>In the last command, we can see an example of the usage of the time interval (as specified by\u00a0<strong>-t<\/strong>): a series of pairs where each one consists of a number followed by one of the characters\u00a0<strong>s<\/strong>,\u00a0<strong>m<\/strong>,\u00a0<strong>h<\/strong>,\u00a0<strong>D<\/strong>,\u00a0<strong>W<\/strong>,\u00a0<strong>M<\/strong>, or\u00a0<strong>Y<\/strong>\u00a0(indicating seconds, minutes, hourse, days, weeks, months, or years respectively).<\/p>\n<h3>Summary<\/h3>\n<p>In this article we have explained how to use Duplicity, a backup utility that provides encryption for files and directories out of the box. I highly recommend you take a look at the\u00a0<a href=\"http:\/\/duplicity.nongnu.org\/index.html\" target=\"_blank\" rel=\"nofollow noopener\">duplicity project\u2019s<\/a>\u00a0web site for further documentation and examples.<\/p>\n<p>We\u2019ve provided man page of\u00a0<a href=\"https:\/\/www.tecmint.com\/wp-content\/pdf\/duplicity.pdf\" target=\"_blank\" rel=\"noopener\">duplicity in PDF format<\/a>\u00a0for your reading convenience, is also a complete reference guide.<\/p>\n<p>Feel free to let us know if you have any questions or comments.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/create-encrypted-linux-file-system-backups-using-duplicity\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Experience shows that you can never be too paranoid about system backups. When it comes to protecting and preserving precious data, it is best to go the extra mile and make sure you can depend on your backups if the need arises. Duplicity \u2013 Create Encrypted Linux File System Backups Even today, when some cloud &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/how-to-create-encrypted-and-bandwidth-efficient-backups-using-duplicity-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Create Encrypted and Bandwidth-efficient Backups Using \u2018Duplicity\u2019 in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13375","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13375"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13375\/revisions"}],"predecessor-version":[{"id":13376,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13375\/revisions\/13376"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}