Creating a Raspberry Pi backup script with scp and Bash on Windows

This post describes a simple way to keep backups of a Raspberry Pi or any other Linux computer.

Why do I need backups of my Raspberry Pi?

Some of the nodes in my cluster are going to be compute only, meaning that they will just run what is asked of them. Others will hold important information such as a database or files that the other nodes are processing. An important feature of having a cluster is ensuring that any information is always available.

There are a couple ways of doing this but the most simple one is ensuring that your important data is always backed up. Then if something were to happen to an important node it can be replaced and the data can be restored.

In the future I will look into more advanced ways of ensuring the data is spread across the cluster but for now a low tech solution (having a script to save important files) is simple and easy to use.

Testing what scp can do for us

To run and backup the script I am going to use the Linux tool scp which stands for Secure Copy. Originally this would have been only accessible to those running Linux or some kind of emulation tools. However recently Windows released “Windows Subsystem for Linux” which allows you to run the core of Ubuntu on Windows.

This guide assumes you have installed Windows Subsystem for Linux or are running a Linux based OS.

The program scp can be used to transfer files to and from remote Linux operating systems over SSH.

A basic command to copy a file from your raspberry pi (or other Linux systems) is:

scp bunker-master2:/home/pi/file.txt file.txt

Here we are copying a file called file.txt in the /home/pi  directory from a computer called bunker-master2 and saving it as file.txt.

This is helpful but only lets us copy a single file at a time. We can improve this command by replacing the second filename with a dot. This tells scp to put the file in the current directory

scp bunker-master2:/home/pi/file.txt .

This means we dont need to specify the filename of the file we are copying. It will always use the name of the file as it exists on the remote host. In addition to using dot to mean the current directory, we can use the asterisk character * to use as a wildcard. By changing the above command and replacing file.txt with an asterisk we can make it download the entire directory.

scp bunker-master2:/home/pi/* .

Creating the backup script

Now the improved scp command copies nearly every file (note, it wont copy dot files) from the pi home directory. Using this we can change what we copy to copy everything of importance to me off the Raspberry Pi. Below is now my backup file which I can use to backup my Pi.

rm -rf home var
scp -r bunker-master2:/home/* home/
scp -r bunker-master2:/var/www/* var/www/

The first command we run is to delete the old copies we downloaded from the Raspberry Pi. This means any files that were deleted on the Raspberry Pi will not be stored on our computer.

Depending on what is important to you, you might want to consider backing up /etc for your configuration files.

I have added these commands to a file called backup.sh and I can run the file by running ./backup.sh in the bash terminal.

In the future, I am going to improve this backup method as there are a couple of downsides to using it. One of them is that even if a file hasn’t changed from your local archive, it will still copy it across. In addition, this won’t backup files such as a MySQL database which to properly backup require a different type of backup.

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.