SSH Backup job not transmitting to remote server

I’m running FreePBX and attempting to run a shecduled backup using ssh to copy the file to a remote server. I’ve setup the backup schedule to run a job and the local tar.gz file is created in /var/lib/asterisk/backups/ but the transfer via ssh to the remote server is not working.

I created the .ssh directory on my remote server and ran ssh-keygen from the FreePBX server and copied the file to my backup servers .ssh/authorized_keys file. I’m able to ssh from the FreePBX server without a password as well as scp the tar.gz files from the command line without supplying a password. This makes me think the ssh keys are working correctly but for some reason FreePBX’s backup job only creates the tar.gz file and fails to transfer to the remote server.

Any ideas?

Are you testing and scheduling the transfer under the same user ?
i think you are testing under your user account the cron job runs under root.

Also when using cron be sure that the cron shell enviroment is correct like path settings.


I assume you are running this as the asterisk user?

You can test it out by running the ampbackup.php script from the commandline.
The job runs as the asterisk user normally so best to test it that way.

This text is from the php script itself and describes how to run it.

[code] The program is called from the backup.php script and implemented immediately as such:

ampbackup.php <Backup_Name> <Backup_Voicemail_(yes/no)>
<Backup_Recordings_(yes/no)> <Backup_Configuration_files(yes/no)>
<Backup_CDR_(yes/no)> <Backup_FOP_(yes/no)

example: ampbackup.php “My_Nightly_Backup” yes yes no no yes

I tried running the ampbackup.php script from the cmd line as you suggested and I get the following output. I suspect a user/permissions issue but I’m not sure how this needs to be setup.

[[email protected] bin]# php ampbackup.php “My-Full-Backup” yes yes yes yes yes
PHP Notice: Undefined index: sudo in /var/www/html/admin/modules/backup/bin/ampbackup.php on line 72
PHP Notice: Undefined index: remotesshhost in /var/www/html/admin/modules/backup/bin/ampbackup.php on line 77
PHP Notice: Undefined index: admin in /var/www/html/admin/modules/backup/bin/ampbackup.php on line 132
PHP Notice: Undefined index: include in /var/www/html/admin/modules/backup/bin/ampbackup.php on line 136
PHP Notice: Undefined index: overwritebackup in /var/www/html/admin/modules/backup/bin/ampbackup.php on line 149
PHP Notice: Undefined index: ftpuser in /var/www/html/admin/modules/backup/bin/ampbackup.php on line 171
PHP Notice: Undefined index: sshkey in /var/www/html/admin/modules/backup/bin/ampbackup.php on line 185
PHP Notice: Undefined index: emailaddr in /var/www/html/admin/modules/backup/bin/ampbackup.php on line 194

After running the script I checked the backup directory and can see the backup file is created. I noticed that when running from the web interface the backup file owner is asterisk and from the command line as root the owner is root.

[[email protected] My-Full-Backup]# ls -l
total 32596
-rw-rw-r-- 1 asterisk asterisk 32052904 Jun 27 20:08 20110627.20.08.20.tar.gz
-rw-r–r-- 1 root root 1280627 Jun 27 20:16 20110627.20.16.55.tar.gz

While this is running I’m monitoring my target server for ssh connections from this asterisk server and never see it attempt to connect, yet I can run a manual scp command to move the file over with no problems.

[[email protected] My-Full-Backup]# scp 20110627.20.16.55.tar.gz [email protected]:/home/asterisk
20110627.20.16.55.tar.gz 100% 1251KB 1.2MB/s 00:00

I suspect some kind of issue with the script or how I’ve setup the parameters in the backup interface. My ssh setting look like the following on the system backup page, where ‘mytarget’ is a private ip address.

SSH Username: asterisk
SSH Key: /root/.ssh/id_rsa
SSH Hostname: mytarget
SSH Directory: /home/asterisk

Any help would be much appreciated.

The backup runs as the asterisk user. This user will not have access to /root/.ssh so the job will fail.

You need to follow the instructions here:

And look for the section starting at:

This thread might also help.

After looking at the directions I ran the following from my FreePBX server and was able to get everything working correctly. What was screwing me up was that I was logged in as root on the cmd line and had to remember to run all of the commands as the user asterisk.

Generated the /var/lib/asterisk/.ssh/id_rsa and files:
sudo -u asterisk ssh-keygen

Copied the file to my target backup server and renamed /home/asterisk/.ssh/authorized_keys:
sudo -u asterisk ssh-copy-id -i [email protected]

Successful test of the SSH session:
sudo -u asterisk ssh [email protected]

Thanks for the help.

1 Like