A Simple Bash Backup Script

 Sat, 12 Nov 2022 10:51 UTC

A Simple Bash Backup Script
Image: CC BY 4.0 by cybrkyd


Many Linux users tend to backup their entire /home directory. I am of the view that I only need to backup essential files. My thinking is: if I lose everything, which files can I not live without?

Identifying essential files which can be restored quickly is the purpose here. This enables a return to a production-ready state as efficiently as possible. I prefer to use a Bash script rather than rely on a backup application to save my important files. The script can then be set to run at pre-determined times via Cron.

Bash backup script example

The first thing is to remove old backups. In my case, any backups older than 60-days can be deleted.

cd /media/cybrkyd/x-drive/backup
find -type f -mtime +60 -exec rm -f {} \;

Next, create the empty backup working directory:

mkdir /media/cybrkyd/x-drive/backup/backup-archive

Backup all the essential files and directories using the cp command:

echo "backup Docs..."
  cp -a ~/Documents /media/cybrkyd/x-drive/backup/backup-archive
  cp -a ~/Work /media/cybrkyd/x-drive/backup/backup-archive
echo "backup Photos..."
  cp -a ~/Pictures /media/cybrkyd/x-drive/backup/backup-archive
echo "backup SSH & GPG..."
  cp -a ~/.ssh /media/cybrkyd/x-drive/backup/backup-archive
  cp -a ~/.gnupg /media/cybrkyd/x-drive/backup/backup-archive
  cp -a ~/.password-store /media/cybrkyd/x-drive/backup/backup-archive

My backups are tiny - less than 1GiB - so I add it to a tarball without compression.

tar -cpf backup-archive.tar backup-archive/

Next, the tar is timestamped and the backup working directory is deleted:

mv backup-archive.tar `date '+%d-%b-%Y-%H%M'`.tar
rm -rf backup-archive/

Here is the full script:

#!/bin/sh

#remove backups older than 60 days
cd /media/cybrkyd/x-drive/backup
find -type f -mtime +60 -exec rm -f {} \;

mkdir /media/cybrkyd/x-drive/backup/backup-archive
echo "backup Docs..."
cp -a ~/Documents /media/cybrkyd/x-drive/backup/backup-archive
cp -a ~/Work /media/cybrkyd/x-drive/backup/backup-archive
echo "backup Photos..."
cp -a ~/Pictures /media/cybrkyd/x-drive/backup/backup-archive
echo "backup SSH & GPG..."
cp -a ~/.ssh /media/cybrkyd/x-drive/backup/backup-archive
cp -a ~/.gnupg /media/cybrkyd/x-drive/backup/backup-archive
cp -a ~/.password-store /media/cybrkyd/x-drive/backup/backup-archive
echo "Adding to tarball..."
tar cpf backup-archive.tar backup-archive/

mv backup-archive.tar `date '+%d-%b-%Y-%H%M'`.tar
rm -rf backup-archive/

echo "Done!"

That’s all there is to it. 30 seconds of piece of mind in less than 30 lines.

Notice that my script writes to an external drive /media/cybrkyd/x-drive. It is good practice to have backups air-gapped somehow from their source. Saving backups on the same drive or even the same machine is asking for trouble.

Ideally, more than one copy of the same backup is recommended. For example, save a backup to an external, removable drive and have a second copy on a remote drive like Google Drive, Dropbox, etc. Of course, don’t dump unencrypted backups to the cloud! Encrypt them.