0

I'm writing a shell script to automate the restore of a database backup. As part of this I need to know the name of the directory conataining the backup files. However this will change every time.

How can I get the directory name into a variable for reference later on.

example: I have the basic directory:

/var/backups/db/

In here there will be another directory, whose name will change every time (e.g. /var/backups/db/db_backup_109245_abc/)

How can I get the directory name (e.g. db_backup_109245_abc) including the full path into a variable so I can refer to it later on?

There will only ever be one directory in here (an earlier part of the process guarantees that), and the name of that directory will vary every time.

IGGt
  • 2,457
  • I'm not sure I understand. A directory is just a string, you save it in a variable the standard way: $variable="db_backup_109245_abc". What else do you need? – terdon Apr 20 '20 at 16:06
  • 1
    Are you just asking how to give the directory name as an argument when you run the script? – terdon Apr 20 '20 at 16:12
  • I know it will always be in /var/backups/db/. And there will always be one directory in there, but I don’t know the name of that directory. I need to get that directory into a variable e.g. backupdir=“/var/backups/db/???????” – IGGt Apr 20 '20 at 16:17
  • How would you manually figure out what directory to use? What I mean is, how would you know to use /var/backups/db/X instead of /var/backup/db/Y? What's the logic behind what subdirectory you should use? Is it the one with the most recently added file in it, the one that sorts before or after all others, the one with the highest number in its name? – Kusalananda Apr 20 '20 at 16:22
  • Then please [edit] your question and explain this. Tell us there will only be one directory and all you need is to get the name of that directory. – terdon Apr 20 '20 at 16:24

3 Answers3

1

OK, I figured it out:

BackupDir=$(find /var/backups/db/* -maxdepth 0 -type d)
IGGt
  • 2,457
  • 1
    Please note that it is highly disrecommended to parse the output of find or ls. Also, the "backtick" notation for command substitutions is deprecated in bash, and it is recommended to use the $( ... ) notation instead. – AdminBee Apr 21 '20 at 11:40
  • fair enough on the "backtick", but what are the chances realistically of someone creating a filename that doesn't parse correctly in ls? – IGGt Apr 21 '20 at 14:46
  • 1
    If you personally create the files, you can take care of course. But filenames with whitespace already can be subject to word-splitting if you are not careful, leading to strange/undesired behavior. And although admittedly I have not yet encountered it, even newline is a permissible character in filenames. – AdminBee Apr 21 '20 at 14:55
0

Assuming you can rely on hypothesis:

  • the backup prefix is always "db_backup_"
  • last backup directory (the one you are looking for if I understand) will have the later date

then you could have this path using

backup_path=$(ls -t /var/backups/db/ |grep db_backup_ | head -1)
tonioc
  • 2,069
  • 1
    Note that the ls command that you show will list the contents of all db_backup_* directories. – Kusalananda Apr 20 '20 at 16:45
  • @Kusalananda, you're right... changed the solution. – tonioc Apr 20 '20 at 16:53
  • the backup prefix is always "db_backup_" - not necessarily, it could be anything. There will only be one directory in there, so I guess technically it will be the latest one (and also the oldest one). – IGGt Apr 21 '20 at 07:27
0

If there is only one single subdirectory in the /var/backups/db directory, then using

set -- /var/backups/db/*/
dirpath=$1

would assign the pathname of that directory to the variable dirpath.

The slash at the end of the globbing pattern ensures that the pattern will only ever match directories.

You may also want to set the shell option failglob (shopt -s failglob). Doing so would make the shell generate a diagnostic message if the pattern does not match anything. This would additionally allow you to exit the script at that point upon failure:

shopt -s failglob
set -- /var/backups/db/*/ || exit 1
dirpath=$1

After this piece of code, you would also have all directory pathnames in "$@" (if you have more than one).

Kusalananda
  • 333,661