linux_wiki:rsync_backup_script

Rsync Backup Script

General Information

This script backs up a home folder to a backup folder. It also creates a backup log, named after the timestamp that it runs. Lastly, it removes log files that were last modified more than 7 days ago.

Checklist

  • Distro(s): Any
  • Other: List of source folders to backup (/home/bill/ in this example)
  • Other: Storage mounted somewhere to backup to (/backups/ in this example)

backuphome.sh
#!/bin/bash
 
# Get Current Date/Time for syncLog's name
CURRENTDATE=`date +%m%d%y_%T`
 
# Backup Home folder (Exclude Backups,NAS,Storage,USB)
rsync -ahv --exclude-from=/home/bill/excludes /home/bill /backups/ >> /home/bill/bin/rsync/syncLog_$CURRENTDATE 2>&1
 
# Look in ~/bin/rsync/ and find log files from rsync backups that are
#  older than 7 days.  Delete them. -ctime ignores fractional parts,
#  so +7 equates to files at least 8 days ago
 
find /home/bill/bin/rsync -name "syncLog_*" -type f -mtime +7 -delete

1) Edit your crontab:

crontab -e

2) Schedule the script to be run every night at 11:00pm (for example)

# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# m h  dom mon dow   command
 
0 23 * * * /home/bill/scripts/backuphome.sh
  • linux_wiki/rsync_backup_script.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)