I want to write a script that will back up one directory if a certain application launches and then backs up another directory if that same application is closed down.
NFI where to start. It seems like cron isn't the tool for this because that is time based. I'm thinking I need something that is constantly running in the background looking for the process to begin/end, or, something in the applications own directory that is triggered when it runs/closes.
Any prod in the right direction greatly appreciated, also, ELI5.
For something like this, we really need to know what operating system and shell you're using. (And, it helps to know what operating system and shell you're using for almost every question posted to the Shell Programming and Scripting forum. To save you and us time, please get into the habit of providing this information whenever you start a new thread in this forum.)
Do you have write access to the application (so the backup can be built into it), or could you create a wrapper that, when run, does the backup and then lauches the application?
---------- Post updated at 10:53 ---------- Previous update was at 10:52 ----------
Not sure what write access to an application is, not skilled enough to create a wrapper. The application is invoked by either typing its name at the console (ardour4) or by double clicking an icon in the GUI.
Create (if not exists yet): $HOME/bin
Save the following as $HOME/bin/myardour4
Obvously you should change the DIR_* variables to match your needs.
#!/bin/bash
#
# Variables
#
DIR_BASE=~/tmp # Directory holding the content to save
DIR_BKUP=./tester # Directory to be saved
DIR_TRGT=~/backups # Directory to store the tarball
NOW=$(date +'%F_%T'|sed s,":",".",g) # Prepare string: YYYY-MM-DD_HH.MM.SS
#
# Functions
#
backup_dir() { # LABEL
# Creates a tarball using with leading data-time and closing 'LABEL'
# For label, should provide either 'beginn' or 'end'
[ -z "$1" ] && return 1
pushd "$DIR_BASE"
tar -acf "$DIR_TRGT/${DIR_BKUP##*/}-$NOW-$1.tar.gz" "$DIR_BKUP"
popd
}
#
# Action
#
backup_dir beginn
ardour4
backup_dir end
From #5 you say it is manually launched either by the GUI or Command Line at any random time(s) and shut down also at any random time(s).
So you need to detect when the program has actually started running to automatically do a backup to '/full/path/to/some/folder1' and when it is exited/quitted to save another backup to '/full/path/to/some/folder2'?
So you wish this script to run 24/7 in the background?
Do you want the copy all files into '.....folder1' and similarly copy all other files into '.....folder2' or do _zipped_ versions of the same?