Today I will be improving the basic script used to backup my Raspberry Pi’s so that it compresses the files after they have been copied. This will allow me to store several backups instead of having a single copy stored on my computer. This assumes you have followed the first tutorial to create a Raspberry Pi backup script.
Compressing and archiving our backups
Our previous script copied over the files from the Raspberry Pi using a program called
scp. This created local copies of the files from our Raspberry Pi.
Each time the copy runs the previous files were overwritten which means I only have the current copy and nothing historic. Today I am going to improve the backup script by downloading the files and then creating an archive file. In addition to creating an archive file I am going to compress it so that it takes less space on my computer.
What is the .tar.gz file format?
.tar.gz file, or
.tgz file, is a combination of two operations intended to create a compressed archive. The first step in creating a
.tgz file is creating a
tar archive of the files you want to compress.
tar is used to combine a file and folder structure into a single file that can be easily moved, the name comes from tape archive. This is because originally tar was used to create a single file that could be written to a tape archive.
Once we have created our tar file we then want to compress it so it uses less space on our filesystem. This can be done using the gzip compression algorithm.
To create tar files I can use the unix tool
tar. One of the useful features is that this natively supports a number of compression algorithms, including
Writing our script
The first thing we are going to do is decide what we want to call our backup file. I have decided that I am going to call it after the Raspberry Pi I am backing up (bunker-master2). To archive several backups I am also going to add the current date and time to the end of the archive name.
To create the backup name with the current date and time in it I will use the date function. This will return a date string formatted based on any given date string. Once I have called it I will save it to a variable name.
Here I save the backup name in a variable called
BACKUPNAME. We will use this in the tar command to refer to the backup name. The string after the
date command sets what the date string will look like. We use the special formatting strings such as
%d which the date program will convert to the current date. We use the following date strings in the above command:
%d – Day of month
%m – Month number
%Y – Year number
%H – Hour (in 24 hour clock)
%M – Minute of hour
Now I have the filename I can call tar to create the archive.
tar -czvf "$BACKUPNAME" home var
I use the declared variable name to hold the file I want to save the backup to. Here I am passing
tar four flags each of which are described below:
- c – Create a tar file
- z – Compress the tar file with the gzip algorithm
- v – Use verbose mode to describe what is currently running (can be removed to reduce command line output)
- f – Allows us to pass the filename of the created file as the next parameter.
The third and fourth parameters are the folders that I want to backup. These are the folders that hold the files I have just copied over from the Raspberry Pi. There can be as many of these as required to backup all your folders and files.
tar will create the archive recursively so if you specify a folder all files and folders inside that will be included.
Now I have a backup script that will copy the files from my Raspberry Pi and save them in a compressed archive. This will let me easily keep several backups. The full script is copied below for ease of use:
rm -rf home var
scp -r bunker-master2:/home/* home/
scp -r bunker-master2:/var/www/* /var/www/
tar -czvf "$BACKUPNAME" home var