{"id":948,"date":"2018-12-10T09:58:53","date_gmt":"2018-12-10T05:58:53","guid":{"rendered":"https:\/\/bukleev.ru\/?page_id=948"},"modified":"2018-12-10T09:58:53","modified_gmt":"2018-12-10T05:58:53","slug":"backing-up-restoring-fusionpbx-with-postgresql","status":"publish","type":"page","link":"https:\/\/bukleev.ru\/?page_id=948","title":{"rendered":"Backing Up &#038; Restoring FusionPBX with PostgreSQL"},"content":{"rendered":"<p>\u041d\u0430 \u0432\u0441\u044f\u043a\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 &#8212; \u043a\u043e\u043f\u0438\u044f \u0441\u0442\u0430\u0442\u044c\u0438<\/p>\n<p><a href=\"https:\/\/inside-out.xyz\/technology\/backing-up-restoring-fusionpbx-with-postgresql.html\">https:\/\/inside-out.xyz\/technology\/backing-up-restoring-fusionpbx-with-postgresql.html<\/a><\/p>\n<h1>Backing Up &amp; Restoring FusionPBX with PostgreSQL<\/h1>\n<p>Not a\u00a0fan of PostgreSQL, but lately many people have come to me regardless I have published I work with MySQL\/MariaDB. Because of this, I want to share with you a set of scripts to backup and restore your deployment. I am not the author of these scripts, I am just doing little modifications to clean them up and to do some more professional and advanced ones that will work not only with PostgreSQL but with MySQL and MariaDB.<\/p>\n<p>These scripts are thought to work with two stand-alone servers. No clustering or load-balancing is involved.<\/p>\n<h2>Back Up your FusionPBX<\/h2>\n<p>First, the script that does the backup. You can put it in a crontab if you wish. Modify it to fit your paths and needs.<\/p>\n<pre><code>#!\/bin\/sh\u00a0\r\nnow=$(date +%Y-%m-%d)\u00a0\r\ndatabase_host=127.0.0.1\u00a0\r\ndatabase_port=5432\u00a0\r\nexport PGPASSWORD=\"YOUR_SUPER_SECRET_PASSWORD\"\u00a0\r\n\r\necho \"Server Maintenance\"\u00a0\r\nmkdir -p \/var\/backups\/fusionpbx\/postgresql\u00a0\r\n#delete backups older 3 days\u00a0\r\nfind \/var\/backups\/fusionpbx\/postgresql\/fusionpbx_pgsql* -mtime +2 -exec rm {} \\;\r\nfind \/var\/backups\/fusionpbx\/*.tgz -mtime +2 -exec rm {} \\;\r\n\r\n#delete postgres logs older than 7 days\u00a0\r\nfind \/var\/log\/postgresql\/postgresql-9.4-main* -mtime +7 -exec rm {} \\;\u00a0\r\n\r\n#delete freeswitch logs older 3 days\u00a0\r\nfind \/var\/log\/freeswitch\/freeswitch.log.* -mtime +2 -exec rm {} \\;\u00a0\r\n\r\n#delete fax older than 90 days\u00a0\r\n#find \/var\/lib\/freeswitch\/storage\/fax\/* \u00a0-name '*.tif' -mtime +90 -exec rm {} \\;\u00a0\r\n#find \/var\/lib\/freeswitch\/storage\/fax\/* \u00a0-name '*.pdf' -mtime +90 -exec rm {} \\;\u00a0\r\n#find \/var\/lib\/freeswitch\/recordings\/*\/archive\/* \u00a0-name '*.wav' -mtime +7 -exec rm {} \\;\u00a0\r\n#find \/var\/lib\/freeswitch\/recordings\/*\/archive\/* \u00a0-name '*.mp3' -mtime +7 -exec rm {} \\;\u00a0\r\n#psql --host=$database_host --port=$database_port --username=fusionpbx -c \"delete from v_fax_files WHERE fax_date &lt;\u00a0\r\nNOW() - INTERVAL '90 days'\"\r\n\r\n#delete voicemail older than 90 days\u00a0\r\n#find \/usr\/local\/freeswitch\/storage\/voicemail\/* \u00a0-name 'msg_*.wav' -mtime +90 -exec rm {} \\;\u00a0\r\n#psql --host=$database_host --port=$database_port --username=fusionpbx -c \"delete from v_voicemail_messages WHERE to\r\n_timestamp(created_epoch) &lt; NOW() - INTERVAL '90 days'\"\u00a0\r\n\r\n#delete call detail records older 90 days\u00a0\r\n#psql --host=$database_host --port=$database_port --username=fusionpbx -c \"delete from v_xml_cdr WHERE start_stamp &lt;\r\nNOW() - INTERVAL '90 days'\"\u00a0\r\n\r\necho \"Starting Backup\"\u00a0\r\n#backup the database\u00a0\r\npg_dump --verbose -Fc --host=$database_host --port=$database_port -U fusionpbx fusionpbx --schema=public -f \/var\/bac\r\nkups\/fusionpbx\/postgresql\/fusionpbx_pgsql_$now.sql\r\n\r\n#backup the files and directories\u00a0\r\n#tar -zvcf \/var\/backups\/fusionpbx\/backup_$now.tgz \/var\/backups\/fusionpbx\/postgresql\/fusionpbx_pgsql_$now.sql \/var\/ww\r\nw\/fusionpbx<\/code><\/pre>\n<h2>Restore your FusionPBX<\/h2>\n<p>This script should be run on the backup server.<\/p>\n<pre><code>#!\/bin\/sh\r\nnow=$(date +%Y-%m-%d)\r\nssh_server=PRIMARY_SERVER_IP\r\ndatabase_host=127.0.0.1\r\ndatabase_port=5432\r\nexport PGPASSWORD=\"YOUR_SUPER_SECRET_PASSWORD\"\r\n\r\n#run the remote backup\r\nssh -p 22 root@$ssh_server \"nice -n -20 \/usr\/loca\/sbin\/fusionpbx-backup.sh\"\r\n\r\n#delete freeswitch logs older 7 days\r\nfind \/var\/log\/freeswitch\/freeswitch.log.* -mtime +7 -exec rm {} \\;\u00a0\r\n\r\n#synchronize the backup directory\r\n#rsync -avz -e 'ssh -p 22' root@$ssh_server:\/var\/backups\/fusionpbx \/var\/backups\u00a0\r\nrsync -avz -e 'ssh -p 22' root@$ssh_server:\/var\/backups\/fusionpbx\/postgresql\/fusionpbx_pgsql_$now.sql \/var\/backups\/fusionpbx\/postgresql\u00a0\r\nrsync -avz -e 'ssh -p 22' root@$ssh_server:\/var\/www\/fusionpbx \/var\/www\u00a0\r\nrsync -avz -e 'ssh -p 22' root@$ssh_server:\/etc\/fusionpbx \/etc\u00a0\r\nfind \/var\/backups\/fusionpbx\/postgresql -mtime +2 -exec rm {} \\;\u00a0\r\n\r\nrsync -avz -e 'ssh -p 22' root@$ssh_server:\/etc\/freeswitch\/ \/etc\r\nrsync -avz -e 'ssh -p 22' root@$ssh_server:\/var\/lib\/freeswitch\/storage \/var\/lib\/freeswitch\/\u00a0\r\nrsync -avz -e 'ssh -p 22' root@$ssh_server:\/var\/lib\/freeswitch\/scripts \/var\/lib\/freeswitch\/\u00a0\r\nrsync -avz -e 'ssh -p 22' root@$ssh_server:\/var\/lib\/freeswitch\/sounds \/var\/lib\/freeswitch\/\u00a0\r\nrsync -avz -e 'ssh -p 22' root@$ssh_server:\/var\/lib\/freeswitch\/recordings \/var\/lib\/freeswitch\/\u00a0\r\n\r\necho \"Restoring the Backup\"\u00a0\r\n#extract the backup from the tgz file\u00a0\r\n#tar -xvpzf \/var\/backups\/fusionpbx\/backup_$now.tgz -C \/\u00a0\r\n\r\n#remove the old database\u00a0\r\npsql --host=$database_host --port=$database_port \u00a0--username=fusionpbx -c 'drop schema public cascade;'\u00a0\r\npsql --host=$database_host --port=$database_port \u00a0--username=fusionpbx -c 'create schema public;'\u00a0\r\n#restore the database\u00a0\r\nPGPASSWORD=\"YOUR_SUPER_SECRET_PASSWORD\" pg_restore -v -Fc --host=$database_host --port=$database_port --dbname=fusionpbx --username=fusionpbx \/var\/backups\/fusionpbx\/postgresql\/fusionpbx_pgsq\r\nl_$now.sql\u00a0\r\n\r\n#restart freeswitch\u00a0\r\nservice freeswitch restart\u00a0\r\necho \"Restore Complete\";<\/code><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041d\u0430 \u0432\u0441\u044f\u043a\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 &#8212; \u043a\u043e\u043f\u0438\u044f \u0441\u0442\u0430\u0442\u044c\u0438 https:\/\/inside-out.xyz\/technology\/backing-up-restoring-fusionpbx-with-postgresql.html Backing Up &amp; Restoring FusionPBX with PostgreSQL Not a\u00a0fan of PostgreSQL, but lately many people have come to me regardless I have published I work with MySQL\/MariaDB. Because of this, I want to<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-948","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/bukleev.ru\/index.php?rest_route=\/wp\/v2\/pages\/948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bukleev.ru\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bukleev.ru\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bukleev.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bukleev.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=948"}],"version-history":[{"count":1,"href":"https:\/\/bukleev.ru\/index.php?rest_route=\/wp\/v2\/pages\/948\/revisions"}],"predecessor-version":[{"id":949,"href":"https:\/\/bukleev.ru\/index.php?rest_route=\/wp\/v2\/pages\/948\/revisions\/949"}],"wp:attachment":[{"href":"https:\/\/bukleev.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}