CLI Remove Old Backup File Max 5 Older

We have provided a set of command-line instructions for managing backup files. These commands are designed to remove older backup files based on certain criteria. Let me break down each of the commands for you:

  1. Removing Old Backup Files Using ls, tail, and xargs:

    1
    
    ls -t1 | tail -n +11 | xargs -d '\n' rm

    This command is used to list files in a directory by their modification time in descending order (-t1). Then, it uses tail to exclude the top 10 files (keeping the 11th and onwards), and finally, it uses xargs to remove these older files.

  2. Removing Old Backup Files Using rm and ls:

    1
    2
    3
    
    cd /DataVolume/shares/john/Backups/DockerBackup/example-server
    rm -f `ls -t ??-??-??.tgz.gpg | sed 1,5d`
    cd ~

    This set of commands does the following:

    • It navigates to the specified directory.
    • It lists files matching the pattern ??-??-??.tgz.gpg in descending order of modification time using ls -t.
    • It uses sed 1,5d to remove the first 5 lines (files) from the list.
    • Finally, it removes the remaining files using rm.
  3. Using the ‘drive’ CLI to Push and Delete Backup Files:

    1
    2
    
    drive push --no-prompt -destination backups/example-server/Dockers/ *.gpg
    drive delete --quiet `drive list --files backups/example-server/Dockers/ | grep gpg | cut -c 2- | sed 1,5d`

    These commands involve the “drive” CLI for interacting with Google Drive. Here’s what they do:

    • The first command pushes all *.gpg files from the current directory to the specified destination in Google Drive, without prompting for confirmation.
    • The second command deletes the oldest 5 *.gpg files in the “backups/example-server/Dockers/” directory on Google Drive. It lists the files, filters for those with “gpg” in their names, removes the first 5 files using sed, and then deletes them using drive delete.

Please note that these commands should be used with caution, especially the ones involving file deletion, as there is no confirmation prompt, and data loss can occur if used incorrectly. Always make sure you have a backup or a way to recover files before running such commands in a production environment.

0%