Index of /andrejciho/Linux/Nightly File Backup

Name Last modified Comments Description

Back Parent Directory

Nightly File Backup

In a fashion similar to how I described nightly mysql backup, you can also do nightly file backup. In this particular example, I used tar with gzip compression because I like to keep the last 7 backups of a website. If I was looking for a script that just backs up one location to another, using rsync with publick key authentication would have been more appropriate and I might talk about that in another post.

As with majority of my posts, I offer nothing new or unique. This is just a script that has been useful to me and if you find it useful, my time and effort in writing this up have been validated.

#!/bin/sh

#Define your variables here:
SOURCE_DIR=     # e.g. /home/andrej/documents
TARGET_DIR=     # e.g. /mnt/hda7/docbackups
BACKUP_FILENAME=     # e.g. andrejdocs

#  Make sure output directory exists.
if [ ! -d $TARGET_DIR ]; then
    mkdir -p $TARGET_DIR
fi

#  Rotate backups
for j in  6 5 4 3 2 1 0; do
    for i in $TARGET_DIR/$BACKUP_FILENAME.tgz.$j; do
        if [ -e $i ]; then
            mv $i ${i/.$j/}.$(( $j + 1 ));
        fi
    done
done

cd $SOURCE_DIR
tar -cvzf $TARGET_DIR/$BACKUP_FILENAME.tgz.0 *

chmod 600 $TARGET_DIR/$BACKUP_FILENAME.tgz.0

In the Nightly MySQL Backup I briefly talk about how to use cron in scheduling scripts to run periodically.


Leave a Comment
*Required
*Required (Never published)
 

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image