Linux file backup
How to make daily file backups on Linux
Just configure the source the name and the output dir, schedule this script to run daily on crontab and it's done. Remember that retention policy is defined in days, so if you want more or less than what we have on the script, just change it to fit your priorities.
#!/bin/bash SOURCE= NAME= OUTPUT_DIR= WEEKLY_BACKUP_RETENTION=30 #DAYS MONTHLY_BACKUP_RETENTION=365 #DAYS YEARLY_BACKUP_RETENTION=3650 #DAYS mkdir -p $OUTPUT_DIR/ mkdir -p $OUTPUT_DIR/daily mkdir -p $OUTPUT_DIR/weekly mkdir -p $OUTPUT_DIR/monthly mkdir -p $OUTPUT_DIR/yearly echo "STARTING $NAME BACKUP $(date +'%Y-%m-%d-%H-%M-%S')" export WEEK_DAY=`/bin/date +%u` # delete the old file echo "DELETING $OUTPUT_DIR/daily/$NAME-$WEEK_DAY.tar.gz IF EXISTS FILE" rm -f $OUTPUT_DIR/daily/$NAME-$WEEK_DAY.tar.gz export DAY=`/bin/date +%d` export MONTH_DAY=`/bin/date +%d-%m` echo "GENERATING $OUTPUT_DIR/daily/$NAME-$WEEK_DAY.tar.gz FILE" tar czf $OUTPUT_DIR/daily/$NAME-$WEEK_DAY.tar.gz $SOURCE/* DATE=`/bin/date +%Y-%m-%d` ##################### ## WEEK BACKUP ## ##################### if [ "$WEEK_DAY" == "7" ]; then echo "DOING WEEK BACKUP OF $NAME" cp $OUTPUT_DIR/daily/$NAME-$WEEK_DAY.tar.gz $OUTPUT_DIR/weekly/$NAME-$DATE.tar.gz fi ##################### ## MONTH BACKUP ## ##################### if [ "$DAY" == "01" ]; then echo "DOING MONTH BACKUP OF $NAME" cp $OUTPUT_DIR/daily/$NAME-$WEEK_DAY.tar.gz $OUTPUT_DIR/monthly/$NAME-$DATE.tar.gz fi ##################### ## YEAR BACKUP ## ##################### if [ "$MONTH_DAY" == "01-01" ]; then echo "DOING YEAR BACKUP OF $NAME" cp $OUTPUT_DIR/daily/$NAME-$WEEK_DAY.tar.gz $OUTPUT_DIR/yearly/$NAME-$DATE.tar.gz fi ##################### ## RETENTION POLICY## ##################### find $OUTPUT_DIR/weekly -type f -mtime +$WEEKLY_BACKUP_RETENTION | xargs rm -f find $OUTPUT_DIR/monthly -type f -mtime +$MONTHLY_BACKUP_RETENTION | xargs rm -f find $OUTPUT_DIR/yearly -type f -mtime +$YEARLY_BACKUP_RETENTION | xargs rm -f echo "FINISHING $NAME BACKUP $(date +'%Y-%m-%d-%H-%M-%S')"