Question

I am trying to create a cronjob with the following specifics; Export to run weekly and dump files older than 3 weeks to be deleted from path. Please can you help create a sample script.

Was it helpful?

Solution

The script (untested)

#!/bin/bash

# Set path to backup directory
BK_PATH = /path/to/backups/

# If 3 is there remove it
if [ -f "${BK_PATH}3.tgz" ]
then
  rm -rf ${BK_PATH}3.tgz
fi

# If 2 is there, it becomes 3
if [ -f "${BK_PATH}2.tgz" ]
then
  mv ${BK_PATH}2.tgz ${BK_PATH}3.tgz
fi

# If 1 is there, it becomes 2
if [ -f "${BK_PATH}1.tgz ]
then
  mv ${BK_PATH}1.tgz ${BK_PATH}2.tgz
fi

# Create 1.
tar czvf ${BK_PATH}1.tgz $1

Then, in your crontab:

0 0 * * 6 /path/to/the/script /path/the/thing/you/want/backedup
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top