How to add your knowledge

Using Cron to Automate Backups on a Linux System

    Table of contents
    No headers

    Attached are example scripts that can be used to automate the backup of the /usr/discreet/projects /usr/discreet/archives and /usr/discreet/clip directories.

     
    The scripts need to be copied to the /etc/cron.daily directory and permissions changed to 755. (chmod 755 <filename>)
     
    The scripts are each in the form of 
     
    # Script to backup new or changed files from the /usr/discreet/project to /mnt/StorageMedia/backup
    # The script will store files in a directory labeled with the day of the week so keeping a history of up to seven days
    # To have this script ran on a daily basis copy this file to /etc/cron.daily/
    
    mkdir -p /mnt/StorageMedia/backup/backup_`date +%A`
    echo --- DAILY PROJECTS BACKUP - `date` >/mnt/StorageMedia/backup/projects_`date +%A`_backup.log
    rsync -av /usr/discreet/project /mnt/StorageMedia/backup/backup_`date +%A`/ >>/mnt/StorageMedia/backup/projects_`date +%A`_backup.log
     
    Basic Explanation:
     
    `date +%A` will return the current day of the week (ie Friday)  referred to as <dayoftheweek> in the following explanation
     
    mkdir -p will create the directory /mnt/StorageMedia/backup/backup_<dayoftheweek> if possible
     
    the echo and rsync will output any messages to a file called /mnt/StorageMedia/backup/projects_<dayoftheweek>_backup.log these logs will be recycled weekly
     
    rsync -av will copy only changed or new files.  cp -auv could also be used
     
    to backup to a different location or if your storage has a different mount point replace all instances of /mnt/StorageMedia/ with the correct path.