BACK UP RESTORE Help needed please

Status
Not open for further replies.

Andrew Byrd

Member
Feb 16, 2018
309
10
18
54
I use the following script to attempt to back up from server A and restore to server B

Upon completion of running this script from Server B, it says restore complete

I login to server B but the restore never took effect.

I installed Fusionpbx on Server B, then ran the script from SSH.

I log back into Server B Fusionpbx and nothing happened. It is still the blank slate as it was before I ran the script.

Any guidance would be greatly appreciated.

This is the script I ran (obtained from Fusionpbx Docs)
--------------------------------------------------------------------------------------------------------------------

Code:
#!/bin/sh
now=$(date +%Y-%m-%d)
ssh_server=xx.xx.xx.xx
database_host=127.0.0.1
database_port=5432
export PGPASSWORD="actualpasskeyusedhere"

#run the remote backup
ssh -p 22 root@$ssh_server "nice -n -20 /etc/cron.daily/fusionpbx-backup"

#delete freeswitch logs older 7 days
find /var/log/freeswitch/freeswitch.log.* -mtime +7 -exec rm {} \;

#synchronize the backup directory
#rsync -avz -e 'ssh -p 22' root@$ssh_server:/var/backups/fusionpbx /var/backups
rsync -avz -e 'ssh -p 22' root@$ssh_server:/var/backups/fusionpbx/postgresql /var/backups/fusionpbx
rsync -avz -e 'ssh -p 22' root@$ssh_server:/var/www/fusionpbx /var/www
rsync -avz -e 'ssh -p 22' root@$ssh_server:/etc/fusionpbx /etc
find /var/backups/fusionpbx/postgresql -mtime +2 -exec rm {} \;

rsync -avz -e 'ssh -p 22' root@$ssh_server:/etc/freeswitch/ /etc
rsync -avz -e 'ssh -p 22' root@$ssh_server:/var/lib/freeswitch/storage /var/lib/freeswitch
rsync -avz -e 'ssh -p 22' root@$ssh_server:/var/lib/freeswitch/recordings /var/lib/freeswitch
rsync -avz -e 'ssh -p 22' root@$ssh_server:/usr/share/freeswitch/scripts /usr/share/freeswitch
rsync -avz -e 'ssh -p 22' root@$ssh_server:/usr/share/freeswitch/sounds /usr/share/freeswitch

echo "Restoring the Backup"
#extract the backup from the tgz file
#tar -xvpzf /var/backups/fusionpbx/backup_$now.tgz -C /

#remove the old database
psql --host=$database_host --port=$database_port  --username=fusionpbx -c 'drop schema public cascade;'
psql --host=$database_host --port=$database_port  --username=fusionpbx -c 'create schema public;'
#restore the database
pg_restore -v -Fc --host=$database_host --port=$database_port --dbname=fusionpbx --username=fusionpbx /var/backups/fusionpbx/postgresql/fusionpbx_pgsql_$now.sql

#restart freeswitch
service freeswitch restart
echo "Restore Complete";
 

Adrian Fretwell

Well-Known Member
Aug 13, 2017
1,498
413
83
Try breaking the problem down into smaller parts:

What files do you have in /var/backups/fusionpbx/postgresql on server A?
What files do you have in /var/backups/fusionpbx/postgresql on server B?

You could try running each rsync individually for each directory to make sure non of them error. Again check what files are being copied and if they are what you expect.

If, for example, the backup you want has been successfully executed and copied to server B and, for example, it is called:
fusionpbx_pgsql_2020-02-20.sql
Try making a smaller restore script to just restore the DB:
Code:
#!/bin/sh

database_host=127.0.0.1
database_port=5432
export PGPASSWORD="actualpasskeyusedhere"

#remove the old database
psql --host=$database_host --port=$database_port  --username=fusionpbx -c 'drop schema public cascade;'
psql --host=$database_host --port=$database_port  --username=fusionpbx -c 'create schema public;'
#restore the database
pg_restore -v -Fc --host=$database_host --port=$database_port --dbname=fusionpbx --username=fusionpbx /var/backups/fusionpbx/postgresql/fusionpbx_pgsql_2020-02-20.sql

Run it and check output for errors.

If no issues then manually restart Freeswitch:
service freeswitch restart
Code:
service freeswitch restart

Now log in to the GUI on server B and see what you have got.
 

Andrew Byrd

Member
Feb 16, 2018
309
10
18
54
What you say makes perfect sense. I started breaking it down and running one section at a time.

The first section logs into server A and creates the back up
That was a success

The section: Remove the old database is where the problems begin

Code:
root@backupserver:~# #extract the backup from the tgz file
root@backupserver:~# #tar -xvpzf /var/backups/fusionpbx/backup_$now.tgz -C /
root@backupserver:~# #remove the old database
root@backupserver:~# psql --host=$database_host --port=$database_port  --username=fusionpbx -c 'drop schema public cascade;'
psql: FATAL:  password authentication failed for user "fusionpbx"
FATAL:  password authentication failed for user "fusionpbx"
root@backupserver:~# psql --host=$database_host --port=$database_port  --username=fusionpbx -c 'create schema public;'
psql: FATAL:  password authentication failed for user "fusionpbx"
FATAL:  password authentication failed for user "fusionpbx"
root@backupserver:~# #restore the database
root@backupserver:~# pg_restore -v -Fc --host=$database_host --port=$database_port --dbname=fusionpbx --username=fusionpbx /var/backups/fusionpbx/postgresql/fusionpbx_pgsql_$now.sql

I used the password from the config.php file and double checked and made sure it was correct

Maybe it is the quotes or other annotations?

export PGPASSWORD="actualpasskeyusedhere"

I, of course, am using my real password between the quotes
 

Adrian Fretwell

Well-Known Member
Aug 13, 2017
1,498
413
83
If you are doing this line by line at the command prompt, have you set your environment variables?

root@a2estest24:~/fusionpbx/sql# db_host=127.0.0.1 (in your case database_host)
root@a2estest24:~/fusionpbx/sql# db_port=5432 (in your case database_port)
root@a2estest24:~/fusionpbx/sql# export PGPASSWORD="xxxxxxxxxxxxxxxxxxxxxxxxx"

You don't need to set database_host and database_port as variables if you don't use them in the command line. i.e.:

psql --host=127.0.0.1 --port=5432 --username=fusionpbx -c 'drop schema public cascade;'
 

Andrew Byrd

Member
Feb 16, 2018
309
10
18
54
Ok, made a little progress

I was using the dbpasskey from server A in the restore script. I changed the passkey to server B and it let me finish the restore with no errors.

Then I logged into the GUI and tried to login using my same login for server A and got this message

Code:
Fatal error: Uncaught PDOException: SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "v_default_settings" does not exist LINE 1: select * from v_default_settings ^ in /var/www/fusionpbx/resources/classes/domains.php:454 Stack trace: #0 /var/www/fusionpbx/resources/classes/domains.php(454): PDOStatement->execute() #1 /var/www/fusionpbx/core/authentication/resources/classes/authentication.php(172): domains->set() #2 /var/www/fusionpbx/core/authentication/resources/classes/authentication.php(53): authentication->get_domain() #3 /var/www/fusionpbx/resources/check_auth.php(73): authentication->validate() #4 /var/www/fusionpbx/core/user_settings/user_dashboard.php(45): require_once('/var/www/fusion...') #5 {main} thrown in /var/www/fusionpbx/resources/classes/domains.php on line 454
 

Andrew Byrd

Member
Feb 16, 2018
309
10
18
54
So what I found out is when I try to copy / paste the whole script (SSH) it does not execute each and every line I will run a line then stop, requiring manual entry of the next line.

For example:

If I want to run the following:

Code:
rsync -avz -e 'ssh -p 22' root@$ssh_server:/etc/freeswitch/ /etc
rsync -avz -e 'ssh -p 22' root@$ssh_server:/var/lib/freeswitch/storage /var/lib/freeswitch
rsync -avz -e 'ssh -p 22' root@$ssh_server:/var/lib/freeswitch/recordings /var/lib/freeswitch
rsync -avz -e 'ssh -p 22' root@$ssh_server:/usr/share/freeswitch/scripts /usr/share/freeswitch
rsync -avz -e 'ssh -p 22' root@$ssh_server:/usr/share/freeswitch/sounds /usr/share/freeswitch

I would copy paste the entire block to ssh and then I would get his result:

Code:
root@backupserver:~# rsync -avz -e 'ssh -p 22' root@$ssh_server:/etc/freeswitch/ /etc
root@45.76.63.235's password:
receiving incremental file list

sent 112 bytes  received 9,949 bytes  1,829.27 bytes/sec
total size is 1,311,599  speedup is 130.36

But when I run each line one at a time it works.

What do I need to do or insert to make all run sequentially one after the other without me having to manually enter each line one at a time?
 

Adrian Fretwell

Well-Known Member
Aug 13, 2017
1,498
413
83
Andrew, @ad5ou is absolutely correct, the use of keys rather than passwords does make this kind of job easier. Indeed I use ssh keys all the time for this kind of backup script, especially when it is run unattended, from a cron job, for example.

My gut feeling right now is that you are struggling with basic shell commands and I think introducing ssh keys will probably just add to your confusion.

To answer your last question. If you want to execute a number of commands, the simplest thing to do is to put them into a small shell script like this:
Code:
#!/bin/sh

ssh_server=45.76.63.235

rsync -avz -e 'ssh -p 22' root@$ssh_server:/etc/freeswitch/ /etc
rsync -avz -e 'ssh -p 22' root@$ssh_server:/var/lib/freeswitch/storage /var/lib/freeswitch
rsync -avz -e 'ssh -p 22' root@$ssh_server:/var/lib/freeswitch/recordings /var/lib/freeswitch
rsync -avz -e 'ssh -p 22' root@$ssh_server:/usr/share/freeswitch/scripts /usr/share/freeswitch
rsync -avz -e 'ssh -p 22' root@$ssh_server:/usr/share/freeswitch/sounds /usr/share/freeswitch

If, for example, if you called this file fred.sh, and you make sure the file is executable with chmod +x fred.sh you can then execute the file in its current directory with ./fred.sh You will still be asked for the password several times but all the lines will be executed.

If you really wanted to paste this in to a terminal and have all the lines execute you must add <space>;\ to the end of each line (except the last one), for example:
Code:
ssh_server=45.76.63.235 ;\
rsync -avz -e 'ssh -p 22' root@$ssh_server:/etc/freeswitch/ /etc ;\
rsync -avz -e 'ssh -p 22' root@$ssh_server:/var/lib/freeswitch/storage /var/lib/freeswitch ;\
rsync -avz -e 'ssh -p 22' root@$ssh_server:/var/lib/freeswitch/recordings /var/lib/freeswitch ;\
rsync -avz -e 'ssh -p 22' root@$ssh_server:/usr/share/freeswitch/scripts /usr/share/freeswitch ;\
rsync -avz -e 'ssh -p 22' root@$ssh_server:/usr/share/freeswitch/sounds /usr/share/freeswitch

There are limits, albeit large numbers, for how long a single Linux command line can be so it is best not to join too any lines together with the ;\ in to one huge line.

Generally speaking, internal shell commands like echo can be executed line after line without the need for the ;\ must most binary program files can't.

Finally, the ;\ construct is useful to learn because ; does have a meaning in this context and there are other options too. The ; will execute the next command regardless of the success or failure of the current command. If you substitute the ; for && then this will only execute the next command if the current one succeeds. Using || instead will only run the next command if the current command fails. These tools are very useful in the construction of robust shell scripts that will largely look after themselves.

I know there is a lot to learn, its not just Fusion and Freeswitch the the Linux operating system Postgresql, Nginx, etc. and the syntax for a good few dozen commands like rsync. I hope this helps a little.

Adrian.
 
Last edited:

Andrew Byrd

Member
Feb 16, 2018
309
10
18
54
Adrian and ad50u

Thank you very much for your insight, education and direction. You have been a great help. I do admit, my struggles are with basic shell commands. I will self remedy this by spending some time learning the basics

Again - I do thank you
 
Status
Not open for further replies.