1

I have a working bash script to create backups and upload them as a tar archive to a remote sftp server.

After the upload, the script should remove all but the latest 20 backup files. I can't use any, pipe, grep, whatever on the sftp. Also I don't get the file-listing result handled in my bash-script.

export SSHPASS=$(cat /etc/backup/pw)
SFTPCONNECTION=$(cat /etc/backup/sftp-connection)

sshpass -e sftp $SFTPCONNECTION  - << SOMEDELIMITER 
 ls -lt backup-*.tar
 quit 
SOMEDELIMITER 

There is this nice oneliner, but I did not figure out how the use it in my case (sftp).

Daniel
  • 102
  • 10

2 Answers2

1

This script deletes all tar files in the given directory except the last 20 ones. The -t flag sorts by time & date. The <<< redirect expands $RESULT feed's it into the stdin of the while loop. I'm not entirely pleased with it as it has to create multiple connections, but with sftp I don't believe there is another way.

RESULT=`echo "ls -t path/to/old_backups/" | sftp -i ~/.ssh/your_ssh_key user@server.com | grep tar`

i=0
max=20
while read -r line; do
    (( i++ ))
    if (( i > max )); then
        echo "DELETE $i...$line"
        echo "rm $line" | sftp -i ~/.ssh/your_ssh_key user@server.com
    fi
done <<< "$RESULT"
codelitt
  • 366
  • 2
  • 13
0

Thanks to codelitt I went with this solution:

export SSHPASS=$(cat /etc/backup/pw)
SFTPCONNECTION="username@host"

RESULT=`echo "ls -tl backup*.tar" | sshpass -e sftp $SFTPCONNECTION |  grep -oP "backup.*\.tar" `

i=0
max=24
while read -r line; do
#  echo "$line "
  (( i++ ))
    if (( i > max )); then
        echo "DELETE $i...$line"
        echo "rm $line" |  sshpass -e sftp $SFTPCONNECTION 
    fi  
done <<< "$RESULT"

It's a slight modification of his version:

  • it counts/removes only files named backup*.tar
  • it uses ls -l (for line based listings)
  • I had to use sshpass instead of a certificate-based authentication. The sftp password is inside /etc/backup/pw
Daniel
  • 102
  • 10