How to backup directory in Linux (using cron)

Very simple script, for regular backup of a specific directory under Linux.

Create fileĀ


SOURCE=/path/of/data #what should we backup
DESTINATION=/home/user/backups #where to store backups
DELETEAFTER=5 #how many days should we store the backups

tar -zcvf latest-backup.tgz $SOURCE
mv latest-backup.tgz $(date +%y%m%d%H)-backup.tgz
find . -name '*-backup.tgz' -mtime +$DELETEAFTER -delete

Mark the file as executable:

chmod a+x

Now, open the crontab editor:

crontab -e

And add this line (with the correct path to your file):

45 3 * * * /home/user/

In this case, it will execute every night at 03:45

Leave a Reply

Your email address will not be published. Required fields are marked *