Can't create backups from cron (Ubuntu)


#1

Hello,

Trying to setup daily backups of Buddy, but cron does not want to run the commands inside the backup.sh file.
Content of backup.sh:

buddy --yes stop
buddy --yes backup
buddy --yes start

(as stated here: https://buddy.works/knowledge/standalone/standalone-backup)

If I run this file from command line, it runs fine - stops Buddy, creates a backup, then starts Buddy.
If I run it from crontab, the output is this:

Command stop not found
____ __ __ ______ __ _
/ __ )__ / // / __ / / / / _________ _____()___
/ __ / / / / __ / __ / / / / / / / __ / / _ / ___/ __ / _/ / / _
/ /
/ / /
/ / /
/ / /
/ / /
/ / / /
/ / / / // __/ / / // / / / (
_ ) /
/
/_,/_,/_,/_, / /__// //_/_// / .// ///___/
/
/ /_/

Buddy Enterprise On-Premises Installation

Command backup not found
____ __ __ ______ __ _
/ __ )__ / // / __ / / / / _________ _____()___
/ __ / / / / __ / __ / / / / / / / __ / / _ / ___/ __ / _/ / / _
/ /
/ / /
/ / /
/ / /
/ / /
/ / / /
/ / / / // __/ / / // / / / (
_ ) /
/
/_,/_,/_,/_, / /__// //_/_// / .// ///___/
/
/ /_/

Buddy Enterprise On-Premises Installation

Command start not found
____ __ __ ______ __ _
/ __ )__ / // / __ / / / / _________ _____()___
/ __ / / / / __ / __ / / / / / / / __ / / _ / ___/ __ / _/ / / _
/ /
/ / /
/ / /
/ / /
/ / /
/ / / /
/ / / / // __/ / / // / / / (
_ ) /
/
/_,/_,/_,/_, / /__// //_/_// / .// ///___/
/
/ /_/

Buddy Enterprise On-Premises Installation

Any clues? File is executable. Tried placing it in root cron and user cron - same thing, doesn’t run.

Tried creating a bash and sh scripts (by placing #!/bin/bash and #!/bin/sh at the top and specifying the environment) - same output.

Wondering what can be wrong now.
Thank you in advance.


#2

Hi @Stan,

Which user do you use to run it in cron? You’d need to run it with a proper user, just like you do it in the terminal.
You can find our files in the home directory of this user. These files are not available if you do that with a different user.


#3

Thanks for your reply. I have initially installed Buddy as a user “convuser”, so “.buddy” directory is in /home/convuser/ (/home/convuser/.buddy/)

I have created backup.sh file inside /home/convuser/.buddy/app/backups/ directory. The file’s owner is “convuser:convuser”
I chmoded this file to a 777 permission.

  • Running “~/.buddy/app/backups/backup.sh” as “convuser” produces “You must run this command as root or with sudo”

  • Inserting a record in “convuser”'s crontab doesn’t do anything (because “buddy stop” has to be run by root)

  • Running “sudo ~/.buddy/app/backups/backup.sh” performs stop, backup, start - OK

  • Inserting a record in root crontab (“sudo crontab -e”) doesn’t do anything, no output

  • if I place the backup.sh file outside “convuser”'s home directory, and place it in root crontab, I get “Command stop not found”

How can I run it as “convuser” if “buddy stop” required to be run as root? What am I missing? I can’t place “sudo” inside user’s crontab…


#4

Stan, it might be a little bit problematic, however, that should do the trick:

  • run crontab for root, that is: sudo crontab -e
  • in the middle at the top, change the HOME variable for the convuser user directory: export HOME=/home/convuser
  • then run the following commands: buddy stop, buddy backup and buddy start

Theoretically, it should work. The user would have root permissions and it’ll work in the user’s HOME directory at the same time.


#5

Hello,

Finally figured it out :slight_smile: For some reason, changing HOME variable to our user directory didn’t work.
Here is a workaround for anyone having a similar issue. This solution is not the prettiest, but at least we are not storing root password anywhere on the server.

Login to your server as your user (non-root), enter into root mode (“sudo -s”) and edit “sudoers” file:

visudo

At the end of the file enter:

convuser ALL = (ALL) NOPASSWD: /usr/local/bin/buddy, /bin/mv

Save the file. What this does is it lets you execute “buddy” and “mv” commands with “sudo” (when you are logged in as a non-root user) without asking for root password. Again, not the prettiest solution, but Im open for other suggestions.
In this line, “convuser” is the user under which Buddy was installed. We add “mv” to the list, because we are moving the backup file to an external drive after the backup procedure is complete.

Exit the root mode, go to your home directory and create a backup script that we will run daily:

vi ~/backup.sh

The content of the file is below:

date=$(date +"%Y-%m-%d")
echo “====================”
echo “STARTING BUDDY BACKUP PROCESS $date”

cd /home/convuser
sudo buddy --yes stop
sudo buddy --yes backup
sudo buddy --yes start

cd /home/convuser/.buddy/app/backups/
sudo mv *.tar.gz /mnt/md0/backups/buddy/buddy-$date.tar.gz

Then go into your user’s crontab (not root!):

crontab -e

And enter the following to generate a backup every day at midnight:

0 0 * * * ~/backup.sh >> ~/backup.log 2>&1

This will also populate a log file for every run.
Hope this helps someone :slight_smile: