Add backup-with-cron blog article
parent
5545679e08
commit
8e612c0af9
@ -0,0 +1,113 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="description" content="Backups are fantastic, and can save hours upon hours of work if they're done. So we want to make sure they're done, and PCs are good with that, with automation.">
|
||||
<meta name="keywords" content="Blog, articles, template">
|
||||
<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>Automating Backups with Cronjobs</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header>
|
||||
<h1>Automating Backups with Cronjobs</h1>
|
||||
<hr/>
|
||||
<nav>
|
||||
<a href="/">home</a>
|
||||
<a href="/equipment.html">equipment</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>
|
||||
<section>
|
||||
<p class="intro">Backups are wonderful things that save hours upon hours of work, and stress, so long as they're actually made in the first place!</p>
|
||||
<p>Automatically taking backups allows for peace of mind that your work won't be lost forever whilst you go about your normal workflow..</p>
|
||||
|
||||
<h2>Create a backup script</h2>
|
||||
<p>You can just call rsync, etc. in cron, but I recommend making a backup script (or a few) for each specific type of backup you want to make.</p>
|
||||
<p>Create the file where-ever you want to keep them, for the sake of this, it'll be a scripts directory in your home directory</p>
|
||||
<pre><code>vim ~/scripts/backup_script.sh</code></pre>
|
||||
<p>And add whatever your backup scripts wants to do. If you've no idea, check out my <a href="/blog/backup-with-rsync.html" target="_blank">rsync</a>, and <a href="/blog/backup-with-rdiff.html" target="_blank">rdiff</a> articles first.</p>
|
||||
<pre><code>rdiff-backup $DIRECTORY_TO_BACKUP $DIRECTORY_TO_BACKUP_TO
|
||||
rdiff-backup --force --remove-older-than 2W $DIRECTORY_TO_BACKUP_TO</code></pre>
|
||||
<p>The above example will backup a directory, and remove any changes from 2 weeks ago.</p>
|
||||
<p>Now make the script executable</p>
|
||||
<pre><code>chmod +x ~/scripts/backup_script.sh</code></pre>
|
||||
|
||||
<h2>Add a cronjob</h2>
|
||||
<p>Now for the automation part. Using cron we can set this script to run at many time variations. I recommend <a href="https://crontab.guru/" target="_blank" rel="noopener">crontab guru</a> to learn more about the expressions used for cron.</p>
|
||||
<p>Edit the cron table (crontab)</p>
|
||||
<pre><code>crontab -e</code></pre>
|
||||
<p>And add the following</p>
|
||||
<pre><code>* */2 * * * /home/$USERNAME/scripts/backup_script.sh</code></pre>
|
||||
<p>This will run the backup script every 2 hours, every day</p>
|
||||
|
||||
<h2>An advanced backup script</h2>
|
||||
<p>An advantage of using a script for backups, is that it allows for more intricate functionality, you may not need to use this functionality, but it's greatly useful.</p>
|
||||
<p>The script below is something I wrote to backup my home directories for each of my servers. It's used to make hourly backups, and send these backups to a remote server daily at midnight.</p>
|
||||
<pre><code>#!/bin/bash
|
||||
|
||||
# Set locations to backup/backup to from the flags
|
||||
while getopts s:d:b:r:R:n: flag
|
||||
do
|
||||
case "${flag}" in
|
||||
d) DATA=${OPTARG};;
|
||||
b) BACKUPDIR=${OPTARG};;
|
||||
r) REMOTE=${OPTARG};;
|
||||
R) REMOTEBACKUP=${OPTARG};;
|
||||
n) NOW=${OPTARG};;
|
||||
esac
|
||||
done
|
||||
|
||||
# If the backup directory doesn't exist, make it
|
||||
mkdir -p $BACKUPDIR
|
||||
|
||||
# Incremental backup of the directory locally
|
||||
rdiff-backup $DATA $BACKUPDIR
|
||||
# Don't keep changes from over 1W ago
|
||||
rdiff-backup --force --remove-older-than 1W $BACKUPDIR
|
||||
|
||||
# Backup to remote
|
||||
# Get the hour/minute time
|
||||
TIME=$(date +%H%M)
|
||||
|
||||
# If it's a midnight backup, or a manual backup with -n 1 flag set
|
||||
if [ "$TIME" = 0000 ] || [ "$NOW" = 1 ]
|
||||
then
|
||||
# Create the remote directory for backup if it doesn't exist
|
||||
ssh $REMOTE mkdir -p $REMOTEBACKUP
|
||||
|
||||
# Copy the backup to the remote location
|
||||
# -e ssh makes it secure
|
||||
rsync -azh -e ssh \
|
||||
--delete \
|
||||
$BACKUPDIR \
|
||||
$REMOTE:$REMOTEBACKUP
|
||||
fi
|
||||
</code></pre>
|
||||
<p>Which is called in the crontab like so</p>
|
||||
<pre><code># Hourly rdiff-backup of $DIRECTORY_TO_BACKUP
|
||||
0 */1 * * * $SCRIPT_LOCATION -d $DIRECTORY_TO_BACKUP -b $LOCATION_TO_SAVE_BACKUP -r $EXTERNAL_SERVER_SSH -R $EXTERNAL_SERVER_BACKUP_LOCATION</code></pre>
|
||||
|
||||
<p>This script can easily be used for many different directories, on each server without needing to change the script itself. All that's needed is to change the cronjob, and/or add another cronjob changing the values it takes.</p>
|
||||
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
<hr/>
|
||||
<p>Written by <a href="http://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>
|
||||
|
||||
Loading…
Reference in New Issue