<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>rsync &#8211; Sandy Scott&#039;s Web Spot</title>
	<atom:link href="https://www.sandyscott.net/tag/rsync/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sandyscott.net</link>
	<description>My little corner of the web</description>
	<lastBuildDate>Fri, 05 Jun 2020 11:39:11 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.2</generator>
	<item>
		<title>Server Setup 14: Backups</title>
		<link>https://www.sandyscott.net/2020/06/server-setup-14-backups/</link>
					<comments>https://www.sandyscott.net/2020/06/server-setup-14-backups/#respond</comments>
		
		<dc:creator><![CDATA[Sandy]]></dc:creator>
		<pubDate>Mon, 01 Jun 2020 17:43:04 +0000</pubDate>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[Debian]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[mysqldump]]></category>
		<category><![CDATA[rsync]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[SpiderOak]]></category>
		<guid isPermaLink="false">https://www.sandyscott.net/?p=516</guid>

					<description><![CDATA[There&#8217;s lots of ways you could lose your website &#8211; a hard drive can fail, the server could be damaged by a lightning strike, or even stolen in a burglary. To get back up and running you&#8217;ll need to fix or replace the hardware, but this post is meant to...<p> <a class="continue-reading-link" href="https://www.sandyscott.net/2020/06/server-setup-14-backups/"><span>Continue reading</span><i class="crycon-right-dir"></i></a> </p>]]></description>
										<content:encoded><![CDATA[
<p><em>If this is your first time looking at a post from this series, have a look at this summary: <a href="https://www.sandyscott.net/2020/06/04/server-setup-0-contents/" data-type="666">Server Setup 0: Contents</a></em></p>



<p>There&#8217;s lots of ways you could lose your website &#8211; a hard drive can fail, the server could be damaged by a lightning strike, or even stolen in a burglary. To get back up and running you&#8217;ll need to fix or replace the hardware, but this post is meant to make sure you have the data you need restore all the applications too.</p>



<h2 class="wp-block-heading">Backup Options</h2>



<p>There are a few ways to approach backups, but the key things are:</p>



<ul class="wp-block-list"><li>What to back up</li><li>Where to back up</li></ul>



<p>I&#8217;ll be focussing on <strong>What</strong>, but will mention a few of your options for <strong>Where</strong> at the end.</p>



<h2 class="wp-block-heading">What to back up</h2>



<h3 class="wp-block-heading">Whole system</h3>



<p>The first thing I should mention is that you might have the option of backing up the whole system in one go. Many systems administrators don&#8217;t install their servers directly onto a computer, they use Virtualisation, which means first you install a special kind of operating system called a hypervisor, whose only job is to create and run virtual machines (VM) &#8211; a computer within the computer. You can then install the OS you really want for your server into one of these virtual machines.</p>



<p>It has many advantages, but I&#8217;ve brought it up now because many hypervisors have the option to create snapshots. This is a complete picture of the installation, so if you need to restore your system, it&#8217;s just a case of loading the most recent snapshot and you&#8217;re back up and running.</p>



<p>However a big disadvantage of this system is performance, especially on older computers with limited resources, which is why I haven&#8217;t done it in this tutorial. Virtualisation is a huge topic, so if it sounds interesting, then go read up on it.</p>



<p>There are also utilites that can clone your entire hard drive, so restoring your system should be a case of of putting that cloned hard drive into new server and starting it up. This approach has it&#8217;s disadvantages too &#8211; the backup will be as big as your hard drive, so your backup location needs a lot of storage space. It also works best if the system is off while the cloning occurs, so you&#8217;ll need to plan some regular downtime to use this backup strategy.</p>



<h3 class="wp-block-heading">Specific Items</h3>



<p>An advantage of backing up specific items is knowledge &#8211; you know what you&#8217;ve got. The tools for managing those pieces are often more common and more widely compatible than those for managing whole system backups.</p>



<p>Our strategy has two steps:</p>



<p></p>



<ul class="wp-block-list"><li>Create and populate a staging directory to hold all of the data we want to back up.</li><li>Actually do the backup to get the data off the server to somewhere else. (Backups stored on the same computer, especially on the same HDD, are a waste of time)</li></ul>



<p>Create the backup staging area:</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text/x-sh&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;sudo Shell&quot;,&quot;language&quot;:&quot;Shell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;shell&quot;}">sudo mkdir /var/backup_staging
sudo chmod -R go-rwx /var/backup_staging
sudo mkdir /var/backup_staging/scripts</pre></div>



<p>We&#8217;ll write a <strong>Shell</strong> script. You might not realise it, but the shell is the program you&#8217;re interracting with when you are entering commands. This means our script can use the same commands.</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text/x-sh&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Shell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;shell&quot;}">sudoedit /var/backup_staging/scripts/backup.sh</pre></div>



<p>Enter the following as the first lists of this file:</p>



<p></p>



<pre class="wp-block-code"><code>#!/bin/sh
# Backup scripts</code></pre>



<p>Like a lot of files we&#8217;ve worked on, the # at the beginning of a line means a comment, so wouldn&#8217;t normally matter. However, in this case, the first line is important to tell the system how to run this file. The second line is a real comment &#8211; a title for our script.</p>



<h3 class="wp-block-heading">General</h3>



<p>You probably what to back up these areas:</p>



<ul class="wp-block-list"><li>We&#8217;ve done a lot of work configuring Apache &#8211; we should definitely save those modifed configuration files: <code>/etc/apache2</code></li><li>Getting new SSL certificates isn&#8217;t too hard, but it doesn&#8217;t hurt to have them if speed things along : <code>/etc/letsencrypt</code></li><li>If you&#8217;re doing other things with this server you might have saved files your home directory: <code>/home/[your username].</code></li></ul>



<p>Create backup directories for these:</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text/x-sh&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Shell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;shell&quot;}">sudo mkdir --parents /var/backup_staging/etc/apache2
sudo mkdir /var/backup_staging/etc/letsencrypt
sudo mkdir --parents /var/backup_staging/home/[your username]</pre></div>



<p>The idea is we&#8217;ll mirror the structure of the filesystem, which means you won&#8217;t get any clashes if you add to this in future. Add these lines our script <code>/var/backup_staging/scripts/backup.sh</code></p>



<pre class="wp-block-code"><code>rsync -Aax --delete /etc/apache2/ /var/backup_staging/etc/apache2/
rsync -Aax --delete /etc/letsencrypt/ /var/backup_staging/etc/letsencrypt
rsync -Aax --delete /home/&#91;your username]/ /var/backup_staging/home/&#91;your username]/</code></pre>



<p>Each of these lines runs the <code>rsync</code> program, which basically copies one folder into another. The options we&#8217;ve selected mean that the backup should be an exact mirror of the original, but it&#8217;ll only move or update the files that have changed. rsync can do a lot more than this, including transferring files to another computer, but you can research that on your own.</p>



<p></p>



<h3 class="wp-block-heading">WordPress</h3>



<p>There are two main pieces to the backup, files and the database.</p>



<h4 class="wp-block-heading">Files</h4>



<ul class="wp-block-list"><li>WordPress uploads folder. Any images you upload to your site are saved in here, and that&#8217;s the only copy. Find it at <code>/var/www/wordpress/wp-content/uploads</code>.</li><li>At the time of writing the entire size of my wordpress folder is only 114 megabytes, including the uploads folder mentioned above. You might as well back up the whole thing. <code>/var/www/wordpress</code>.</li><li>We moved the wordpress configuration into a file outsite the web root, so make sure that&#8217;s backed up: <code>/var/www_config</code>.</li></ul>



<p>Create directories for these:</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text/x-sh&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Shell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;shell&quot;}">sudo mkdir --parents /var/backup_staging/www/wordpres
sudo mkdir /var/backup_staging/www_config</pre></div>



<p>Add add these lines to the script to copy them into the backup staging location.</p>



<pre class="wp-block-code"><code>rsync -Aax --delete /var/www/wordpress /var/backup_staging/var/www/wordpress
rsync -Aax --delete /var/www_config/ /var/backup_staging/var/www_config/
</code></pre>



<p></p>



<h4 class="wp-block-heading">Database</h4>



<p>The database is a critical component of both WordPress and Nextcloud, so we need to back this up, but it takes a little planning.</p>



<p>We&#8217;re going to use mysqldump, which logs into your database server, and saves the whole database (or even multiple databases) in a sql file. The idea is you can just execute this sql file on a database server and the database will be recreated.</p>



<p>The command history is considered insecure, so we don&#8217;t want to put our database password in on the command line. The database credentials go in a separate file:</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text/x-sh&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Shell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;shell&quot;}">sudoedit /var/backup_staging/scripts/wordpress.cnf</pre></div>



<p>Add this to wordpress.cnf, filling in your database username and password, (with single quote round it in case of special characters)</p>



<pre class="wp-block-code"><code>&#91;mysqldump]
user=wp_usr
password='wordpress_database_password'</code></pre>



<p>And now add this line to your backup script:</p>



<pre class="wp-block-code"><code>mysqldump --defaults-file=/var/backup_temp/scripts/wordpress.cnf --skip-dump-date --add-drop-table sswp > /var/backup_temp/sswp-current.sql
</code></pre>



<p>Explanation:</p>



<ul class="wp-block-list"><li><code>mysqldump </code>is the utility that spits out a sql that&#8217;s a backup of a database.<ul><li><code>--defaults-file</code> tells mysqldump to read the username and password the file we specified</li><li><code>--skip-dump-date</code> means the sql won&#8217;t include the date the dump was done, so if the database hasn&#8217;t changed between dumps, the sql file will be identical</li><li>&#8211;add-drop-table means the recovery can be more reliable, if riskier for the data you have on the system you&#8217;re installing the recovered data on.</li><li>&gt; means the data is written the file specified.</li></ul></li></ul>



<h3 class="wp-block-heading">Nextcloud</h3>



<p>Nextcloud also has files and data that we want to back up. There&#8217;s a greater chance of inconsistencies between the files and database, and it&#8217;s down to you how much care you take over this.</p>



<h4 class="wp-block-heading">Maintenance Mode</h4>



<p>Like we did for WordPress, create a file containing the Nextcloud database login details. I&#8217;d call it <code>/var/backup_staging/scripts/nextcloud.cnf</code>. Use the same format, just with the Nextcloud database details instead of the WordPress ones.</p>



<p>First, we&#8217;ll put Nextcloud into maintenance mode, add this line to your script:</p>



<pre class="wp-block-code"><code>sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on</code></pre>



<p>This runs the occ script, which performs various utility tasks on a Nextcloud installation.</p>



<h4 class="wp-block-heading">Database</h4>



<p>Now we can safely dump the database contents. Add this line to the backup script:.</p>



<pre class="wp-block-code"><code>mysqldump --defaults-file=/var/backup_staging/scripts/nextcloud.cnf --single-transaction --skip-dump-date --add-drop-table nextcloud > /var/backup_staging/nextcloud.sql
</code></pre>



<h4 class="wp-block-heading">Files</h4>



<p>We have a choice here:</p>



<ol class="wp-block-list"><li>Minimum Downtime. Copy the enitire Nextcloud Data folder into the staging folder, then turn off maintenance mode. The downside of this you need twice as much storage space.</li><li>Minimum Storage: Back up the Nextcloud data folder in place, then turn off maintenance mode. Your site will be down for as long as it takes the backup to complete.</li></ol>



<p>Pick an option, and add the relevant lines to the end of your backup script</p>



<p>Option 1:</p>



<pre class="wp-block-code"><code>rsync -Aax --delete /var/www/wordpress /var/backup_staging/var/www/wordpress
rsync -Aax --delete /var/nc_data/ /var/backup_staging/var/nc_data/
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off
# # # # # # # # # # # # # # # # # #
# Placeholder for backup commands
# Back up /var/backup_staging only
# # # # # # # # # # # # # # # # # #</code></pre>



<p>Option 2:</p>



<pre class="wp-block-code"><code>rsync -Aax --delete /var/www/wordpress /var/backup_staging/var/www/wordpress
# # # # # #
# Placeholder for backup commands
# Back up /var/backup_staging and /var/nc_data
# # # # # #
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off</code></pre>



<h2 class="wp-block-heading">Where to back up</h2>



<p>All we&#8217;ve done so far is to stage all the useful data from our websites in a single folder.</p>



<p>We need to get it off your server to different location, preferably serveral locations. A common rule about backups is the 3-2-1 rule:</p>



<ul class="wp-block-list"><li>3 copies of your data</li><li>2 of those copies on different of media</li><li>1 copy is offsite.</li></ul>



<p>The &#8220;2 different media&#8221; rule protects against hard drive failure, and the offsite rule can protect against a whole host of ways to lose your data: physical theft, fire, lightning damage and more.</p>



<p>So how can you do this?</p>



<ul class="wp-block-list"><li>A few portable HDDs can be a simple option &#8211; you&#8217;ll need to remember to plug them in, you may need to configure your backup script to mount it. Keep one at your mum&#8217;s house and swap it out every week or so.</li><li>Another server. If you (or a friend) have another server with plenty of space, you could back up to that. <code>rsync </code>doesn&#8217;t just copy folders on one computer &#8211; it&#8217;s really meant for transfering files to a remote computer over SSH. A sample command might look like this:</li></ul>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text/x-sh&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Shell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;shell&quot;}">rsync -az /var/backup_staging/ username@remoteserver:/var/remote_backups</pre></div>



<ul class="wp-block-list"><li>For either of these options, consider using software that encrypts the data. <a href="http://duplicity.nongnu.org/">Duplicity</a> works a bit like rsync, but allows for encryption and incremental backups.</li><li>Use a backup service. These companies usually have their own software and servers. Look for encryption and trustworthiness (I would <strong>never</strong> use a free service for something like backup). It&#8217;s also essential that they offer a Linux client with a command line interface so we can add it to our script. I use <a href="https://spideroak.com/">SpiderOak </a> &#8211; there are plenty other others out there, do your research!</li></ul>



<p>Once you&#8217;ve picked your method, replace the placeholder in our backup script with whatever commands you need to make that run.</p>



<h2 class="wp-block-heading">Run it regularly</h2>



<p>Our backup script is great and all, but unless you run it regularly, it&#8217;s pretty pointless. We&#8217;ll use <code>cron </code>to run this script every night (or whenever you want).</p>



<p>Edit the system crontab:</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text/x-sh&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Shell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;shell&quot;}">sudo crontab -e</pre></div>



<p>Add this line to the bottom, then save and exit the file.</p>



<pre class="wp-block-code"><code>  5 2   *   *   *  /bin/sh /var/backup_staging/scripts/backup.sh</code></pre>



<p>The lines at the top of the crontab script tell you how to use it. The 5 and 2 means the command will run at 5 minutes past 2am (02:05 24 hour clock), and the stars means it won&#8217;t consider the other options (day of week, day or month or month) are ignored. <code>/bin/sh</code> is the program that will run our script.</p>



<h2 class="wp-block-heading">Test!</h2>



<p>This is critical &#8211; after you&#8217;ve done your first backup, take a good look at:</p>



<ul class="wp-block-list"><li>Is everything you expected there?</li><li>Nothing is corrupted</li></ul>



<p>IT professionals regularly do &#8220;recovery drills&#8221;. i.e. practice what they&#8217;d do to get IT systems up and running after a distaster, which helps find problems with the backups. Have a go at using your backups to rebuild you site, maybe in a VM on your client computer.</p>



<p>Good luck!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sandyscott.net/2020/06/server-setup-14-backups/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
