2

---asked already on unix.StackExchange, but closed (too subjective)---

Rewritten for clarity.

  • I want to write scrips for my own use
  • I want to be able to back them up
  • I haven't yet established the backup routine - gonna write a script.
  • it will be my 4th script, and my first serious approach to backing up (so be gentle).

What is the most effective strategy, given that two factors seem to be in tradeoff:

  1. Among several options, /usr/local/bin is widely advocated as most advantageous location for custom scripts, (but not uniquely; see ideas below)

  2. /usr/local/bin is full of other, automatically generated files, files (e.g. Dropbox, etc) that I would not want to take into a backup and hence would have to figure out a way to filter (not a trivial task for my skill level, though I'm willing to learn).

Do I attack this from the "directory end" (e.g. 'don't use that directory, use this one!') or from the "backup script end" (e.g. an effective filter I could use with tar or cp or whatever to extract my script files from the crowd in /usr/local/bin).

I can't find anyone talking about both of these things in the one place (but "backup" and "scripts" swamps results with "backup scripts" so maybe I've missed it.)

Cheers - thanks for the constructive answers so far.

PS - see also an earlier version of this question elsewhere for a couple of helpful replies before shutdown.

Zanna
  • 70,465

1 Answers1

4

You mentioned two things, which I don't believe are good solutions:

"Created by":

Standard Linux file attributes include an owner user and group. I strongly recommend you make sure all files in /usr/local/bin (or actually almost all directories that contain stuff to be accessed by all users) are owned by root as user and group, and also only writable by the owner. This is a security measure that should prevent unprivileged modifications to the file, which might later be executed by someone else or with elevated privileges using sudo.

So as the owner of all those scripts in /usr/local/bin should be root, we can't use that to distinguish the scripts you want to back up.

"Subdirectory":

Executables inside subdirectories of /usr/local/bin would not be accepted by the system as commands which you can run directly without a path. The reason is that for this, an executable needs to be directly inside a directory listed in the $PATH environment variable. Subdirectories are not automatically included here, each location needs to be explicitly specified - again, as a security improvement to lower the risk of accidentally executing untrusted code.

Although it would be possible to add that subdirectory in all necessary locations, I'd refrain from changing all users' $PATH (and not to forget, the /etc/sudoers configuration's secure_path must include the new directory too, if you want it accessible when using sudo).


Now what could you do instead? Here are a few ideas:

User-specific bin directory:

If you don't need those scripts system-wide for all users, but only for your own account, simply put them into ~/bin (~ being your home directory). This location is only on your own user's $PATH, not of anyone else. For your personal scripts, this should be the simplest way.

Keep in mind that you need to restart your current shell once (close and reopen the terminal window, type source ~/.profile or log out and back in) if you just created the directory during your current session. This is because the directory is added only if it exists when the shell initializes itself. You don't ever have to do this again later on.

Keep a list of your scripts:

Simply have a text file somewhere listing the paths of all of your scripts/files which you want to back up. Then you can simply process this list with another script/command like the one below to copy them all to your backup directory:

#!/bin/bash
backupdir=~/backup/scripts
listfile=~/backup/script-list.txt
while read line ; do
    cp -- "$line" "$backupdir"
done < "$listfile"

You just have to keep the list file (here ~/backup/script-list.txt) updated and well formatted. In this simple version, you must make sure that each line contains exactly one absolute path to a file you want to back up, with no leading or trailing spaces and no empty lines. Of course the script could be improved to be able to handle such cases, but this is just a quick prototype.

Add a "signature" to your own scripts, and only back up those containing it:

Just think of a unique string you add to all of your own scripts, e.g. in a comment. That could look like *** CREATED BY BYTECOMMANDER *** or so, maybe even more unique. Then you can make a backup script that scans /usr/local/bin for files containing your signature line and only copies those, leaving others alone:

#!/bin/bash
backupdir=~/backup/scripts
signature="*** CREATED BY BYTECOMMANDER ***"
for scriptfile in /usr/local/bin/* ; do
    if grep -qF "$signature" "$scriptfile" ; then
        cp -- "$scriptfile" "$backupdir"
    fi
done
Byte Commander
  • 107,489