Skip to content

Instantly share code, notes, and snippets.

@crh0831
Created April 25, 2023 00:57
Show Gist options
  • Save crh0831/07e07b93f84e23504a94262d1ee40fff to your computer and use it in GitHub Desktop.
Save crh0831/07e07b93f84e23504a94262d1ee40fff to your computer and use it in GitHub Desktop.
Generic Directory Backup Shell Script

Generic Directory Backup Script

I regularly have directories of files I want to back up in case of disaster. Usually these are files that change often, and I only want to keep recent versions in case I want to revert or recover changes. (Git? git, who?)

I have used this script over and over as a simple way to archive a directory to a location with a date-stamped filename. It also cleans up after itself by deleting files older than X days. I stick it in CRON and let it run on a schedule and I always have an archive of the last X days of the files in my directory.

#!/usr/bin/env bash
#===============================================================================
#
#          FILE: wiki-backup.sh
#        AUTHOR: C Hawley
#       CREATED: 2022-11-30
#      REVISION: 2022-11-30
#
#===============================================================================
 
set -o nounset                               # Treat unset variables as an error
 
# Backup Source
bsource=/mnt/data/wiki-data
 
# Backup Destination
bdest=/mnt/backups
 
# Backup Filename (no extension)
bfilename=wiki-data-backup 
 
# Get today's date
bdate=$(date +"%Y-%m-%d")
 
# Archive directory to the destination
tar czf $bdest/$bfilename-$bdate.tgz $bsource
 
# Prune backups older than 7 days
find $bdest -maxdepth 1 -type f -iname "$bfilename*.tgz" -mtime +7 -delete

Here's one with a twist - backing up certain things on certain days of the week:

#!/usr/bin/env bash
#===================================================================
#
#          FILE: dockerdata-backup.sh
#         USAGE:  
#   DESCRIPTION: 
#       OPTIONS: 
#  REQUIREMENTS: 
#         NOTES: 
#        AUTHOR: C Hawley
#       CREATED: 2021-04-01
#      REVISION: 2022-02-02
#
#===================================================================
 
set -o nounset                               # Treat unset variables as an error
 
# Get today's date
bdate=$(date +"%Y-%m-%d")
day=$(date +%u)
 
# Backup Source
bsource=/mnt/docker
 
# Backup Destination
bdest=/mnt/nfs/derry
 
# Backup Every Day
for dir in gitea npm; do
    echo "Archiving $bsource/$dir -> $bdest/$dir-$bdate.tgz"
    tar czf $bdest/$dir-$bdate.tgz $bsource/$dir
done
 
# Backup only on certain day of week
case $day in
    3) #Wednesday 
      for dir in filerun; do
        echo "Archiving $bsource/$dir -> $bdest/$dir-$bdate.tgz"
        tar czf $bdest/$dir-$bdate.tgz $bsource/$dir
      done
      ;;
esac
 
# Prune backups older than 3 days
find $bdest -type f -iname "*.tgz" -mtime +3 -delete

Change the Backup source, destination and filename variables for your case and the -mtime number to change the retention days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment