sh/bash: A Script for Rotating Backups or Files (Backup with Rotation)

Practice



A simple little script for automatic backup rotation.

In this example we create backups of the /var/mydata folder, archive them using gzip and tar, creating a file named mydata-date.tgz (for example, mydata-2011-08-06), and then delete all files older than 31 days.

#!/bin/sh

vDate=`date +"%Y-%m-%d"`

# Backing up
tar -czf /backups/mydata-${vDate}.tgz /var/mydata

# Rotating the logs, delete older than 31 days
find /backups/mydata-* -mtime +31 -exec rm {} \;

Accordingly, instead of /var/mydata - enter the path to the folder you need; instead of +31 - the number of days to keep backups; instead of mydata - the name for the backup files.

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "LINUX operating system"

Terms: LINUX operating system