Computer
Back Ups
Page

Revised
March 07, 2015

Daily
Backup Script

To use this script:

1: Create empty file , such as /usr/local/bin/BU_Daily.sh /opt/local/bin/BU_Daily.sh or to place the script into.

2: Set permissions.   Make sure that it is executable.   Mine is set to 770 / rwxrwx---

3: Open the empty file in text editor.   Copy script text below, then paste into file.

4: Configure script under the "CONFIGURE SECTION".

5: Create the directory that stores the backup, such as /mnt/backups/Running/Daily.

6: Run the script in a terminal to check for errors.

    # cd /usr/local/bin

    # BU_Daily.sh

7: Set CRON to run script as needed.

8: Set up the Clean Up script to remove old backups.


#! /bin/bash
# See http://www.jperkins.us/computer/home_network/
# Author: Jerry Perkins, Nashville, Tennessee
# Last revision September 23, 2006
#
# Purpose: To make a daily backups on a Linux / UNIX machine.
#
# --------- USER NOTES ----------
#
# Example:
# /usr/local/bin/BU_Daily.sh
# Run every night
# at 2 minutes after Midnight.
#
# --------- CONFIGURE SECTION ----------
#
# Set path to where to backups will be stored.
# Example: STORAGE="/mnt/backups/Running/Daily"
STORAGE="/mnt/backups/Running/Daily"
#
# List the directories that are to be backed up.
# Example: PATHLIST="/etc /home /srv /usr/local"
# You can also backup remote directories that are mounted.
# Example shows a backup to a Microsoft directory.
# WARNING, do not use directory names that contain a space,
# such as "My Documents".
# But you can use "My*". (Warning, I have not tested the
# "*" since major changes to script)
# There also can be security issues with sharing Microsoft files.
# PATHLIST="/mnt/c-drive/WINDOWS/Profiles /mnt/c-drive/WINDOWS/My*"
# PATHLIST="/etc /home /root /srv /usr/local /var"
PATHLIST="/etc /home /root /srv /opt /usr/local /var"
#
# Set the period of days between backups.
# Example: PERIOD="-1"
PERIOD="-1"
#
# Set User Name. This changes the backup files Owner.
# Example: FILEOWNER="joew"
FILEOWNER="joew"
#
# Set Group Name. This changes the backup files Group.
# Example: FILEGROUP="wilson"
FILEGROUP="wilson"
#
# Set permissions. Suggest either 400 for owner read, or
# 440 for owner and group read.
# Example: PERM="400"
PERM="440"
#
# --------- NO EDITING BELOW THIS LINE ----------
#
DAYNAME=`date +%Y-%m-%d-%H`
FILEPOST=".tar.gz"
DASH="_"
if [ -e $STORAGE ]; then
  cd $STORAGE
  for LIST in $PATHLIST; do
     LISTROOT=`echo "$LIST" | colrm 1 1`
     DIRNAME=`echo "$LISTROOT" | sed "s/\//_/g"`
     FILENAME=$DIRNAME$DASH$DAYNAME$FILEPOST
     if [ -e $LIST ]; then
        find $LIST -mtime $PERIOD -type f | tar zcfP $FILENAME -T-
     fi
  done
  CHGALL="*"$DAYNAME$FILEPOST
  chown $FILEOWNER $CHGALL
  chgrp $FILEGROUP $CHGALL
  chmod $PERM $CHGALL
# Removes empty backups.
  find $STORAGE -size 45c -type f -exec rm {} \;
  find $STORAGE -size 0c -type f -exec rm {} \;
fi # END


If you have any comments, problems, questions or suggestions, please send me an e-mail at

Updated;   This page was last updated on Mar 07 , 2015 .

Any Browser        Gates Free        Valid XHTML 1.0! Valid CSS!

Linux Logo