Ubuntu – Automatic backup of folder

backup

I have one particular folder on my linux box that I would like to have fully backed up and be able to "go back in time" on a file and see all previous revisions.

To add complications, this folder is shared (with the inbuilt sharing tools) and is accessed and written to by a Windows machine. For this reason, I'd like the backup to be written to a place where the windows machine does not have access.

How can I set this up?

Best Answer

Well I use the following script for my backup:

#! /bin/bash

# Gets date of most recent backup.    
newestfile=$(cd /home/<USERNAME>/.Backups && find . -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" ")        
budate=`echo $newestfile| cut -c10-19`

# Gets current date

cdate=$(date --iso)

# If the cureent date is the same as the date of the most recent backup, don't run the backup, just give a notification that says it has already been done today.

if [ $cdate = $budate ]; then
    echo "Backup Complete"
    notify-send -i /home/<USERNAME>/Pictures/Logos/safe.png "Backup Status" "Already started/finished backup for today."

# If the dates are different, start the backup.

else
    echo "Starting backup"
    notify-send -i /home/<USERNAME>/Pictures/Logos/safe.png "Backup Status" "Starting backup for today."
# Compresses the files into .tar.gz format 

    tar -cvpzf /home/<USERNAME>/.Backups/backup-$(date +%Y-%m-%d-%H:%M).tar.gz "/home/<USERNAME>/folder/to/back/up" --exclude=.Backups && notify-send --expire-time=60000 -i /home/tim/Pictures/Home/Logos/safe.png 'Backup Status' 'Finished backup for today.'
fi

This will save a backup file that looks like this:

backup-2014-07-26-13:13.tar.gz

In the hidden folder /home/<USERNAME>/.Backups

The safe.png file that it used for the notifications can be downloaded from here.

  1. Save the script in /home/<USERNAME>/Scripts as backup.sh

  2. Run the following commands:

    chmod +x Scripts/backup.sh
    mkdir .Backups
    touch .Backups/backup-2000-01-01-00:00.tar.gz
  3. Then add the command Scripts/./backup.sh to the start at login applications. Even if you login more than one time in a day, you only get 1 backup.

    OR

    You could also use cron to run the script regularly. Edit it using crontab -e and add this line to the end:

    0 15 * * *    bash /path/to/script/backup.sh
    

My pronouns are He / Him

Related Question