Performing backups
Now that I have a more-or-less functional website I think it is time to do some backups.
My favorite command for backups is rsync.
My preferred parameters are:
- -a because it includes several useful options in one (like recursive, keep permissions, timestamps, etc.)
- -v It’s always useful to add the verbose option, this way you see what is actually happening.
- -z As I’m doing the backup on my computer and the source is an external server it makes sense to compress the data.
- --delete I want an exact copy, if I delete a file on the server I don’t want to keep it forever in the backup.
- --exclude ‘x’ To exclude a file or folder that I don’t want to back up.
- -e As the backup comes from a server we should connect to it via ssh, so we should add -e “ssh -p $PORT”.
I call this command with a script with several variables, but the line that calls rsync is something like this:
rsync -avz --delete --exclude 'downloads' -e "ssh -p $PORT" "$USR@$SERVER:$SRC" "$DST"
Where:
- $PORT The port where the ssh has to connect (default is 22).
- $USR My user at that server.
- $SERVER You guessed it, it’s misello.net.
- $SRC The source or the folder I want to synchronize, for example, my whole home.
- $DST Where I want to save the backup.
As I mentioned before, I like to use the –delete parameter. This is risky, because if you delete a file by mistake and you realize after the backup this file is gone for good. This is why I use one folder for each day of the week.
This is easy to perform by creating a variable
DAY=$(date +%u%A)
And adding this variable to the path
DST="/Destination/path/$DAY/my_home_server/"
As I perform the backup on a USB drive I make sure it has the ext4 filesystem and not FAT in order to be compatible with UNIX permissions.
The last thing I had to deal with was the log system. It’s always good to have a log for what happened. To do so I save the exit status of rsync:
STATUS=$?
And all the echoes that go to the screen are also copied to a log file using tee -a as well as the messages and errors of rsync (adding » log_file 2>&1 at the end of the command).
I then thought of using the logger command. But I somewhat dislike it.
Finally I decided not to have logs as we know them because, at the end of the day, I will not read them. I only wanted a simple system that could tell me when the last backup was and if it was successful.
For this method I used a sentinel file. This is an empty file that, just by its name and presence, tells me what happened and when.
I will have one of these files in my Documents folder and one in the USB drive. The one in the Documents folder tells me I did a backup, and the one in the USB drive tells me it was successful.
At the start of the script I delete all sentinel files (both the one on the Documents folder and the one in the USB drive). Then I set the name of the file depending on the date and time and create it:
NAME=`date +%a`_`date +%Y%m%d`_`date +%H%M%S`.last_server.bk
touch "/path/to/my/Documents/folder/"$NAME
Later in the script, after saving the exit status of the rsync:
if [ $STATUS -eq 0 ]; then
echo "Exit message"
SENTINEL="$(ls /path/to/my/Documents/folder/*.last_server.bk)"
cp "$SENTINEL" "/path/to/my/USB/drive/home_$(basename "$SENTINEL")"
else
echo "Failure message. Exit status: $STATUS"
fi
I can also repeat this method with different names on the USB drive if the backup consists of different rsync commands.