Backup

Backup

UPDATE: it occurred to me that I’m using rsync incorrectly (or at least not taking advantage of its transport speedup). However, I want to store the backup in a compressed form. A solution I came up with is to decompress the files on the server before upload and compress them again, but I can’t be bothered to do this now.

Backup is crucial everywhere, particularily on servers where a failure would wipe out hours upon hours of work without any backup solution.

In this tutorial, we will be using rsync since it helps reduce the time spent transferring data.

First, note down what you have to backup. Then create backup-service:

#!/bin/bash

rsync -Aavx /folder/to/backup /tmp/backup-folder
rsync -Aavx /folder/file /tmp/other-file
tar -czvf /tmp/service-backup_`date +"%Y%m%d"`.tar.gz /tmp/backup-folder/ /tmp/other-file
ssh user@backup.server "cp service-backup_$(date -d 'yesterday' +'%Y%m%d').tar.gz service-backup_$(date +'%Y%m%d').tar.gz"
rsync -Aavx /tmp/service-backup_$(date +'%Y%m%d').tar.gz user@backup.server:/path/to/backup/service-backup_$(date +'%Y%m%d').tar.gz
rm -r /tmp/backup-folder /tmp/other-file

Now, you’ll have to configure an ssh key and make it unprotected so that the script can connect without user input. To backup automatically, add it to your crontab.

After a while, past backups are going to pile up on your backup server, I recommend writing a script that automatically deletes backups older than 3 days if there are backups from the past 3 days.

This is a script that you can use:

#!/bin/bash

# Define the directory where the files are located
directory="/var/backup/"

# Find files matching the pattern "backup-" followed by a date (YYYYMMDD)
# Older than 3 days and count the number of files with the same base name newer than 3 days
files_to_remove=$(find "$directory" -name 'backup*' -mtime +3 -exec basename {} \; | sort)

# Iterate through the files to remove and delete them
for file in $files_to_remove; do
        # Count the number of files with the same base name newer than 3 days
        count=$(find "$directory" -name "backup*" -mtime -3 | wc -l)
        if [ "$count" -ge 3 ]; then
                echo "Removing files older than 3 days for $file"
                find "$directory" -name "$file" -mtime +3 -exec rm {} \;
        else
                echo "Not enough files found for $file"
        fi
done