Creating Scheduled Automatic Daily, Weekly, and Monthly Files Backup using CRON and TAR

Posted by Ray on January 25, 2024

A guide for creating backup scripts and scheduling them to run daily, weekly, and monthly.

Creating Backup Scripts

  1. Create a backup directory to store the backup scripts and files, and navigate into this directory.
    1
    
     $ mkdir backups && cd backups
    
  2. Create daily, weekly, and monthly directories to store backups.
    1
    
     $ mkdir daily weekly monthly
    
  3. Create a backup scripts to run.
    1
    2
    
     # nano <name>-backup.sh; example:
     $ nano daily-backup.sh
    

setup

  1. Type the following code:
    1
    2
    3
    4
    5
    6
    7
    
     # LINUX:
     tar -zcf <store_backup_dir>/<backup_file_name>-$(date +%Y%m%d).tar.gz -C <file_or_folder_dir> <file_or_folder_to_backup>
    
     # BSD:
     tar -zcf <store_backup_dir>/<backup_file_name>-`date +%Y%m%d`.tar.gz -C <file_or_folder_dir> <file_or_folder_to_backup>    
    
     find <store_backup_dir>/* -mtime +<num_of_days> -delete
    

    The first line has the commands:

    • tar compresses files to a tar format
    • -zcf means gzip compress into a file
    • <store_backup_dir> implies the full path to the directory where you want to store the backup file
    • <backup_file_name> implies the name of the backup file
    • $(date +%Y%m%d) appends the current date of when the backup file is generated
    • .tar.gz is the backup file extension
    • -C means change directory to where the file to be backed up is located
    • <file_or_folder_dir>implies the full path to the directory where the file of folder to back up is located
    • <file_or_folder_to_backup> is the name of the file or folder to be backed up

    The second line has the commands:

    • find searches files
    • <store_backup_dir>/* refers to all backup files located in the backup folder
    • -mtime + to filter files created ago
    • <num_of_days> specifies the number of days to filter
    • -delete command that deletes old backup files that were filtered

    example using daily backup:

    1
    2
    3
    
     # Daily backup for last 7 days
     tar -zcf /Users/ray/Desktop/backups/daily/backup-$(date +%Y%m%d).tar.gz -C /Users/ray/Desktop/ assets/
     find /Users/ray/Desktop/backups/daily/* -mtime +7 -delete
    

daily_backup

  1. Run script to check if it works.
    1
    2
    3
    4
    
     $ sh daily-backup.sh
     $ cd daily && ls # backup file should be created
     $ tar -xzvf backup-<date>.tar.gz
     $ ls # backup file should be extracted
    

script_working

  1. Repeat steps 3 to 5 for weekly and monthly scripts. Input your desired number of days to filter in <num_of_days> parameter. Weekly backup for the last 5 weeks would be +31, and monthly backup for the last 5 months would be +100.

    Example for weekly:

    1
    2
    3
    
     # Weekly backup for last 5 weeks
     tar -zcf /Users/ray/Desktop/backups/weekly/backup-$(date +%Y%m%d).tar.gz -C /Users/ray/Desktop/ assets/
     find /Users/ray/Desktop/backups/weekly/* -mtime +100 -delete
    

    Example for monthly:

    1
    2
    3
    
     # Monthly backup for last 4 months
     tar -zcf /Users/ray/Desktop/backups/monthly/backup-$(date +%Y%m%d).tar.gz -C /Users/ray/Desktop/ assets/
     find /Users/ray/Desktop/backups/monthly/* -mtime +100 -delete
    

Schedule Script Execution Using Cron

  1. Type crontab -e in the terminal
  2. Schedule your script to run at specific times. Use https://crontab.cronhub.io/ for easier cron creation Example:
    1
    2
    3
    
     15 0 1 * * sh <path_to_script>/backup-monthly.sh
     15 0 * * * sh <path_to_script>/backup-daily.sh
     15 0 * * 1 sh <path_to_script>/backup-weekly.sh
    
  3. Save and exit file
  4. Check backup files after a few days to verify if worked.