0
  • After I opened the terminal, I typed this command:
moabdelaziz@pop-os:\~$ crontab -r

  • And scheduled the task:
* * * * * /bin/bash /home/moabdelaziz/Backups/backup.sh >> /home/moabdelaziz/output.txt
  • backup.sh

#!/bin/bash

TargetDir=/home/mohamed/Date
TargetBackup=/home/mohamed/Backup
EncryptionKey="PASS"
days=12

source ../backup_restore_lib.sh

validate_backup_params ${TargetDir} ${TargetBackup} ${EncryptionKey} ${days}
backup ${TargetDir} ${TargetBackup}

  • backup_restore_lib.sh contains those functions

validate_backup_params {...}
Encryption {...}
Decryption {...}
remote_server {...}
backup {...}
validate_restore_params {...}
restore {...}

Unfortunately, it does not work :(.

Can anyone point me to anything I'm doing wrong? Thanks in advance!

  • 1
    The path `../backup_restore_lib.sh` will not generally be resolved based on the directory the script is in, but wherever working directory the cron daemon happened to use when it ran that job. Either use a full path, or use something like the process in ["How do I get the directory where a Bash script is located from within the script itself?"](https://stackoverflow.com/questions/59895/how-do-i-get-the-directory-where-a-bash-script-is-located-from-within-the-script) to get the script's directory and resolve based on that. BTW, add `2>&1` to the cron entry to see errors in the log. – Gordon Davisson Jul 18 '23 at 01:50
  • When you run a command from `cron`, the working directory will be the user's home directory. So you can use a path relative to that. Or just use the absolute path, as @GordonDavisson recommends. – Barmar Jul 18 '23 at 02:24
  • Is `bash_script.sh` supposed to be `backup.sh`? – Barmar Jul 18 '23 at 02:24
  • Did you check the error logs? Probably there is an error that `backup_restore_lib.sh` or `backup`won't be found. In any case, I suggest either an absolute path there, or do a `cd` in your script so that you end up in the directory you want the script run in. Also, you invoke `backup` without specifying an absolute path, but you never set the variable `PATH` in your script. – user1934428 Jul 18 '23 at 05:19

1 Answers1

1

In general, crontab scripts are executed in the home directory of the user. That means that your

source ../backup_restore_lib.sh

will look for a file /home/backup_restore_lib.sh to source.

It is better to put in the absolute path to the file that you want to source.

Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31