You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
aney.co.uk/guides/backup-with-rsync.html

88 lines
4.0 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="keywords" content="Blog, articles, template, rsync, backup">
<meta name="author" content="Nathan (Aney) Steel">
<meta name="theme-color" content="white">
<meta name="theme-color" content="black">
<link rel="stylesheet" type="text/css" href="/main.css">
<link rel="icon" type="image/png" href="/images/favicon.svg">
<title>Backup with rsync</title>
</head>
<body>
<header>
<a href="#main" class="vh">Jump directly to main content</a>
<h1>Backup with rsync</h1>
<input id="burger-toggle" type="checkbox"/>
<label class="burger-container" for="burger-toggle"><div class="burger"></div><span class="sr">Burger menu</span></label>
<hr/>
<nav>
<a href="/">home</a>
<a href="/about.html">about</a>
<a href="/projects.html">projects</a>
<a href="/blog/">blog</a>
<a href="/sitemap.html">misc</a>
<a href="/support.html">support</a>
</nav>
<hr/>
</header>
<main id="main">
<section>
<p class="intro">Rsync is a program that allows for incremental backups. This means that rsync will not create an additional copy of the data when backing up, it will only backup changes to the files/directories, saving bandwidth and storage space.</p>
<h2>Installation</h2>
<pre><code>sudo apt install rsync</code></pre>
<h2>Backup</h2>
<pre><code>rsync -azh $ORIGINAL $BACKUP</code></pre>
<p>Replace $ORIGINAL with the file/directory to backup, and $BACKUP with the location for the backup to reside.</p>
<p>The $BACKUP destination must be a blank directory, an rsync directory, or not currently exist.</p>
<h3>Remote rsync backup</h3>
<p>If you need to rsync from one PC to another, it's essential the same command, but with the additional layer of ssh</p>
<pre><code>rsync -azh -e ssh $ORIGINAL $BACKUP</code></pre>
<p>$BACKUP here will be an ssh connection pointed to a location, much like when using scp, so the command will look like</p>
<pre><code>rsync -azh -e ssh $ORIGINAL $USER@$HOST:$LOCATION</code></pre>
<p>Replacing $USER and $HOST with the username and hostname/IP for the server</p>
<h2>Restore</h2>
<p>A restore in rsync doesn't require any rsync code per-se, as you can just copy individual files from the backup location to the restore location.</p>
<p>Alternatively to restore the entire directory, keeping files that haven't changes, and those that have to the time of the last backup, rsync can do that as below</p>
<pre><code>rsync -auv $BACKUP $RESTORE</code></pre>
<h3>Over the internet</h3>
<p>Like with backups, these restores can be done over the network/internet too</p>
<pre><code>rsync -auv $USER@$HOST:$BACKUP $RESTORE</code></pre>
<h2>Notes/Advanced</h2>
<pre><code>
-r recursive. All files/directories in the path will be backed up
-a archive mode. Recursive, but with file permissions, symlinks, etc retained.
-z compress
-b backups
-R relative
-u update - copy only changed files
-P progress
-c compress
-p preserve permissions
-h human readable. Make the output readible by humans
</code></pre>
<h2>Downsides</h2>
<p>Rsync only keeps one copy of the data, and doesn't keep the changes that were made, making it impossible* to restore a file's contents from the day previous. If this is what you're after, look at <a href="/guides/backup-with-rdiff.html">rdiff-backup</a>.</p>
<p>* Not impossible, as you <em>can</em> set rsync to do this, but it requires a bit of scripting, and isn't as easy as just running the program</p>
</section>
</main>
<footer>
<hr/>
<p>Written by <a href="https://aney.co.uk" target="_blank" rel="noopener">@aney</a> with <a href="https://danluu.com/web-bloat/" target="_blank" rel="noopener">web bloat</a> in mind | <a href="https://github.com/Aney/website" target="_blank" rel="noopener">Source Code</a></p>
</footer>
</body>
</html>