Simple Backup Shell Script Linux


This video covers a topic that not only saves you a bunch of manual chore work, but also improves your working practices:

Automatically backing up important stuff

This is the picture:

You are on Mac or Linux.

You have something important you'd like to back up on a daily basis.

You'd like to keep a rolling number of copies so that your disk isn't needlessly consumed.

You could do this by hand, but you don't want to.

Hello /bin/bash

We're going to automated such chore work to our humble servant: the computer.

The good news:

If you've already taken a manual backup then Good News, Everyone!

You already know exactly how you will do this.

If not, well, that bit is different for every circumstance.

My example will cover running a command: rancher export.

This command dumps out a bunch of important configs which can be used to very quickly reproduce a working Dockerised environment from yml files.

I use Rancher a lot, and as such I want to make sure my configs are backed up on the regular.

Here's the process:

1. Create a root directory to save everything in

For me I have this:

mkdir -p /home/$(whoami)/Development/docker/rancher

I'd like something like:

mkdir -p /home/$(whoami)/Development/docker/rancher/{todays_date_here_plz}

# or

mkdir -p /home/$(whoami)/Development/docker/rancher/20171127-1527

Computers do a better job of this sort of thing than me.

A better way of writing this would be:

echo `date +"%Y%m%d-%H%M"`
20171127-1543

From which we can deduce:

mkdir -p /home/$(whoami)/Development/docker/rancher/`date +"%Y%m%d-%H%M"`

ls -la
total 104
drwxrwxr-x 22 www-data www-data  4096 Nov 27 15:45 .
drwxrwxr-x 98 chris    chris     4096 Nov 27 15:14 ..
drwxrwxr-x 19 chris    chris     4096 Nov 27 15:27 20171127-1527

# ... etc

Bosh.

2. Change into that dir and run the backup

Part one of this is simple enough.

Imagining for one moment that we're hand-cranking this, we would do:

cd /home/$(whoami)/Development/docker/rancher/20171127-1527

And part two, as mentioned, differs depending on what it is you're trying to do.

If it were running a mysqldump for example, the way you would do this would be different to running a pg_dump, and so on.

In my case I want to run a command: rancher export

3. Only Keep 7 Days Of Config

My system wide backup is kept for longer. This should be more than sufficient.

rm -rf $(ls -1t | tail -n +8)

That's it, we're done.

Automating It

There's two parts to automating this.

We need to combine all of this stuff into a shell script.

And we need to add an entry to our crontab to run that shell script as some determined time.

We've already done all the hard work.

cd /home/$(whoami)/Development/docker/rancher
touch backup.sh
chmod +x backup.sh
vim backup.sh

Of course you don't need to use vim. It's a sadists sport.

#!/bin/bash

cd /home/$(whoami)/Development/docker/rancher

CURRENTDATE=`date +"%Y%m%d-%H%M"`

mkdir $CURRENTDATE

cd $CURRENTDATE

rancher export

rm -rf $(ls -1t | tail -n +8)

Awww yeah.

Basically what we whacked into the command line, just with added variables because we are hardcore nerds.

crontab -e

This brings up your crontab.

Adding in an entry is the same syntax as vim (or it is for me).

0 16 * * * ~/Development/docker/rancher/backup.sh >/dev/null 2>&1

Every 4pm run my script and whack the output into /dev/null, aka nowhere.

Curious about the end of that command? This explains it better than I could.

And that's it.

A manual chore no more.

Episodes

# Title Duration
1 Simple Backup Shell Script Linux 09:36
2 Run PHPUnit Tests Whenever Your Files Change 09:25
3 Use Your Command Line History Like a Boss 03:17