0

Folders are created every time I (re)boot.

They have the format below: (the time will always vary)

.mozilla_2019_01_08_08:02
.mozilla_2019_01_09_20:16
.mozilla_2019_01_10_01:16
.mozilla_2019_01_10_18:12

Here, I want to be able to delete the folder .mozilla_2019_01_10_18:12 when it is created as an earlier one already exists on ..._2019_01_10. Better would be to stop it being created, i.e. if one has already been created that day.

I can't use cron @daily or specific times as I never know how often or when the computer will be (re)booted.

How can I do this in an existing script please?

Here is my script:

##!/bin/bash
##
# COPY .mozilla FOLDER AND LEAVE LAST 10 COPIES (1 per day?)
#                                                                                                                
sleep 30
#  
if [ -e /home/david/nas-backups/mozilla/ ]
  then
    cp -a /home/david/.mozilla /home/david/nas-backups/mozilla/
    mv /home/david/nas-backups/mozilla/.mozilla /home/david/nas-backups
    /mozilla/backups/.mozilla_$(date +%Y_%m_%d_%H:%M)

# Show result of copy message
    export DISPLAY=:0 && zenity --info --title "Backup Information Box"
    --text=".mozilla Backup Finished

    Backed up to:
    /nas-backups/mozilla/backups/" --width=600 --height=200

## Working on how to keep only first copy per day ##

# Limit to last 10 alphabetical copies (courtesy of @Vijay)
    cd ~/nas-backups/mozilla/backups
    total=$(ls -1A|wc -l); remove=$((total - 10)); rm -rv $(ls -1vA|head
   -n $remove)


# Show folder so I can manually delete extra daily copies
    nautilus /home/david/nas-backups/mozilla/backups

else

    export DISPLAY=:0 && zenity --info --title "Backup Information Box"
    --text=".mozilla backup FAILED

    - nas-backups either not mounted or not availeble" --width=450
    --height=150
fi
  • 1
    Maybe posting the entire contents of the "existing script" could help people help you. – DK Bose Jan 10 at 2:46
  • Who creates those folders? I don't have anything like that on my system. – AlexP Jan 10 at 3:41
  • @user68186 I have tried mv -n and --no-clobber but the first backup (say .mozilla_2019_01_11 ends up with another .mozilla (no date) inside it. – David Jan 11 at 0:13
0

One backup per day: Don't create more backups if one exists for today

Add a condition that checks if a backup folder for today does not exist. If there is no folder for today then only the cp and mv will be executed.

Note the * at the end of the second folder check. This allows for the time stamp at the end of the folder name, as you have done.

Here is the code fragment:

##!/bin/bash
if [ -d /home/david/nas-backups/mozilla/ ]
  then
  if [ ! -d /home/david/nas-backups/mozilla/backups/.mozilla_"$(date +%Y_%m_%d)"* ]
    then
      echo "No backups found for today: Making one"
      cp -a /home/david/.mozilla ~/nas-backups/mozilla/
      mv /home/david/nas-backups/mozilla/.mozilla ~/nas-backups/mozilla/backups/.mozilla_$(date +%Y_%m_%d_%H:%M)
    else
      echo "Backup folder for today exists. No backups will be made today"
  fi
fi

Hope this helps

  • 1
    Many thanks. I was looking at it from the wrong end - parsing existing filenames. Your solution is great (and logically obvious) - thank you. – David Jan 11 at 23:46

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.