SSH: Rotate backup shell script to remove directories (old backup files)

Most time you have a limited space on the remote SFTP/ SSH backup server. Here is the script that periodically cleanup old backup files from the server i.e it will remove old directories.

Requirements

Script will automatically calculate date from today’s date. By default it will keep only last 7 days backup on server. You can easily increase / decrease this limit. In order to run script you must meet the following criteria:

  • Remote SSH server with rm command execution permission
  • SSH Keys for password less login (see how to setup RSA / DSA keys for password less login)
  • Accurate date and time on local system (see how to synchronize clock using ntpdate ntp client)
  • Remote backup directory must be in dd-mm-yyyy or mm-dd-yyyy format. For example daily mysql backup should be stored in /mysql/mm-dd-yyyy format.

Sample Script Usage

Run the script as follows:
./rot.backup.sh 7 /mysql "rm -rf"
Where,

  • 7 : Remove last 7 days files
  • /mysql : Base directory to clean up. If todays date is 9/Oct/2007, it will remove last 7 days directory /mysql/02-10-2007, /mysql/01-10-2007, …. /mysql/26-09-2007, /mysql/25-09-2007. It means script will only keep last 7 days backup on remote sftp / ssh server.
  • rm -rf : Command to run on directory structure

Sample Shell Script

Install following script:

#!/bin/bash
if [ "$#" == "0" ];then
  echo "$0 upper-limit path {command}"
  exit 1
fi
### SSH Server setup ###
SSH_USER="vivek"
SSH_SERVER="nas.nixcraft.in"
START=7
DIR_FORMAT="%d-%m-%Y" # DD-MM-YYYY format
#DIR_FORMAT="%m-%d-%Y" #MM-DD-YYYY format
## do not edit below ##
LIMIT=$( expr $START + $1 )

## default CMD ##
CMD="ls"
SSH_PATH="."

[ "$3" != "" ] && CMD="$3" || :
[ "$2" != "" ] && SSH_PATH="$2" || :

DAYS=$(for d in $(seq $START $LIMIT);do date --date="$d days ago" +"${DIR_FORMAT}"; done)
for d in $DAYS
do
  ssh ${SSH_USER}@${SSH_SERVER} ${CMD} ${SSH_PATH}/$d
done

Run above script via cron tab (cronjob):
@daily /path/to/rot.ssh.script 7 "/html" "rm -rf"
@daily /path/to/rot.ssh.script 7 "/mysql" "rm -rf"

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
6 comments… add one
  • Srinivasan Aug 13, 2011 @ 23:20

    I need to remove 1 month older files from a sftp server username@servername, where
    i don’t have access for ssh in the sftp server.

    Please advise

  • Al Jun 22, 2008 @ 13:21

    Watch out of that pipe comes back empty – rm -f will then act on whatever the contents of the current directory are!

  • Reetika Oct 22, 2007 @ 4:57

    Hi
    Pls suggest any one liner or script for my probelm.
    I need to tremove the files which are 14 days old but need to skip those folder which are having any single file with latest date.
    Need to skip whole folder which contain any single latest file with all the old files also which that particular folder contain.

    my script is like this
    find . -mtime +14 -exec rm -f {} ;
    find . -type d -mtime +14 -exec rmdir {} ;
    perl -MFile::Find -e”finddepth(sub{rmdir},’.’)”
    #find . -depth -type d -empty -exec rmdir {} ;

    Thanks
    Reetika Gupta

  • Yannick Oct 11, 2007 @ 10:14

    Ohhhh..yes..’find’ will probably don’t work in most of ssh jails..

    BTW… Thank you very much Vivek for all your very useful tips.. 🙂

  • 🐧 nixCraft Oct 11, 2007 @ 4:50

    Yannick,

    Thanks for quick one liner 🙂

    Just a quick note many backup services only allows limited set of commands in chrooted ssh jail. Many time find and other advanced commands are not available so you need to craft something like this

  • Yannick Oct 10, 2007 @ 23:54

    I usualy use someting like this:

    find /var/backup/ -name ‘backup*’ -mtime +7 | xargs rm -f

Leave a Reply

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

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.