Save documents from OpenShift Mongodb - javascript

I have a scalable app in OpenShift with MongoDb2.2 ad NodeJs0.10,
Since i can't use Cartridge rockmongo-1.1 because it cannot be embedded in scalable app,
How can i get my db'documents and do a Backup and Restore with ssh ?

Make sure you use the RHC command line tools.
You have two ways to get to your data:
1) You can ssh into your gear (as a shortcut you can do rhc ssh {app-name}). Then you can do the dump and restore at the console. Remember, you only have write permissions to the ~/app-root/data and /tmp directory so make sure to put your output there. Then you can scp your files back and forth.
2) You can you*rhc port forward* to port forward you MongoDB terminal and commands to you local machine. You will still need the MongoDB username and password we assigned to your instance on creation.

Related

How to dump postgres database and sync with other postgres database using nodejs

I have 2 database servers. One for dev and one for production, what I want to do is to create a dumb of production DB and sync it with dev DB at a specific time using nodejs.
Postgres provides pg_dump (single DB) and pg_dumpall (all DBs) utilities out of the box. The simplest way would probably be a cron job to automate the backup of your source DB and scp it over to a destination server, then restore the DB on the destination server by a cron job:
On the source server:
crontab -e
0 0 * * 0 pg_dumpall -U postgres > ~/backups/backup.bak
# scp to your destination server
If you’re working in one of the major cloud environments they will have their own tools that can help, eg AWS you can automate snapshots, then restore from a snapshot (or have a lambda perform the restore from the snapshot as you suggest in nodeJs)
Or (super-cool way) is to use AWS DMS (Data Migration Services) CDC - Change Data Capture and you can replicate a source DB instantaneously with one or many target replicas (avoiding the need for dumps and restores)
I asume you meant dump, and googled that for you:
https://www.joseverissimo.com/blog/automatically-export-a-database-and-import-it-to-another-using-node-js
Now you just should make sure it runs as a cronjob.

How to stop a NodeJs file on Ubuntu server

I paid someone to install a Js script on my server
but when i tried making some changes on scripts/files i just couldnt see the changes on browser
while browsing and googling for like 24 hours i figured out that js scripts should be restarted in order to show the result on browser
and i figured out that in order to start a node js file i should type in terminal
node /dir/example.js
but can someone help me how to refresh/restart it
Iam stuck here like 24 hours
please help
I just want to let you know too that iam using Putty software in order to connect to the ubuntu server, ubuntu version is 20.04
EDIT: you will need an ssh connection to your server with access to a terminal..
Your node.js scripts, if they are on a server, should typically be ran using a process manager such as pm2.
Try to type pm2 list in a terminal. If you see some information you have hit the spot. Just after that run pm2 restart id. id being the number of your process and your changes should be visible.
You can just try this or try your luck with other process managers that run on your server providers. Just read what your server provider recommends for default and usually your previous developer would have used it.

Node.JS: Why is my connection to localhost:3000 refused?

I'm a student going into back-end development for the first time and are trying to learn Node.JS. I downloaded a pdf book about Node.JS from sitepoint called: "Jumpstart Node.JS". In following the instructions to set up the server on the command line, install the dependencies, and navigate to localhost:3000, i got nothing except the following message: "Connection refused: localhost:3000", Can somebody please tell me what might have went wrong and how to fix it?
Edit1:
The instructions i followed is about setting up a node.js server using the Node command line, thus no code, simply cmd commands, however, here is a quick summary of the process i followed:
Created an account on MongoLabs and then a database using the free pricing plan.
Installed express using the command: npm install -g express#.2.5.8.
Created an applications with default options using this command: express authentication.
modified the package.json file in system32
installed the dependencies by typing cd authentication, hitting enter, and then typing the command: npm install
Typed node app and hit enter.
According to the instructions i should have seen a message: "Welcome to express" but instead got the error message.
In following the instructions to set up the server on the command line, install the dependencies, and navigate to localhost:3000
It seems that you didn't start the server.
Somewhere between installing the dependencies and navigating to the URL you need to actually start the server if you want it to serve the request.
Check that there is no copy of the server running in the background, or that another app is using the port currently.
(Your firewall show allow you to see which app has been allocated to that port)
Because nodejs requires it to be the only app on that port running on your computer.
Also try a different port maybe?

Point mongoos.connect at localhost mongodb

I am studying the MEAN stack by using this tutorial. But the tutorial connects to a remote mongodb installation. I have MongoDB running in the CentOS7 localhost.
How do I alter the mongoose connect line in server.js from the tutorial link above to connect to a localhost database instead of a remote database?
Here is the current line from server.js that needs to be changed to point to the localhost mongodb:
mongoose.connect('mongodb://node:nodeuser#mongo.onmodulus.net:27017/uwO3mypu');
A specific mongodb database has not been created yet. Do I need to create that also?
I'm fairly new at Mongo to, but I know how to connect to a local db. Basically I had to do the following:
Download the latest version of mongodb from https://www.mongodb.com/download-center?jmp=nav#community (according to your settings)
Once installed, I've created a folder that will be containing my DB.
Use a command line instance to start mongo like this:
mongod --dbpath [YOUR_DB_PATH]
Created a DB use mydb
With that you should have already a mongodb db instance looking for connections on default port. So you can change that line for this:
mongoose.connect('mongodb://localhost:27017/mydb');
Again, this is really basic and it is creating a mongo DB connection with all default options. So this will keep you rolling, but you may need to dig a bit more for custom options.
Hope this helps

mail to myself a user's submitted form - hybrid app

I am trying to build a hybrid app, I am using jquery mobile. The app is supposed to be installed on the mobile and not a web-app. I tried doing some research but still couldn't understand how to accomplish what I want.
I want to insert a form for the user to fill (such as: mail, name, telephone, text) and I want that when submitted the fields will be mailed to myself.
I understand that it isn't possible to insert php to jquery mobile because it doesn't support it or something like that. However, I saw that it's possible to call a php script through AJAX. But I don't get how it's done because PHP is server side it has to be hosted somewhere right?
So I need to host on the web a php file and call it through the app by it's URL?
I have found a php script that does exactly what I want but I don't understand if it's useable through hybrid app.
I also read that javascript can't mail something straight to my email address, it only has the mailto function which opens a mail form.
I couldn't find an answer to that, and I would really appreciate if someone could explain to me how it works and if it's even possible.
Its very easy, but for a beginner it may be a daunting task to set up so ill write Plenty of Notes and Installation tasks to get you started.
Prerequisites for a Home Setup with PHP and EMAIL Capabilities. For Remote Hosting Setup I guess everything is Installed for you but may require some Cost, so for Home use and doing it by yourself the bonus is that its Free.
1) A Spare Computer with at least 1gig of ram and 10gigs of hard disk. if you don't have a spare one or cant afford one at he moment you may be able to Virtualize one on top of your current computer using something like Virtual box which is free. If you want 100% live service the computer must be on all the time and connected to the Internet.
2) A Server OS preferably Linux one. In this example will use UBUNTU Server. Why? Because its Very easy to setup and install extra utilities, frameworks and programs, easy to manage and has all the features you will ever need and its Fast.
3) A Mail Account. GMAIL as an example for sending emails.
4) an Internet Connection.
5) A network Cable to connect the computer to your Router. Even if you choose Wireless over a Network Cable to connect you may need to download additional drivers to get your Wireless card Working and Connecting Most likely you will need a Net Cable first. It is advisable not to use Wireless for Such Setup for a number of Reasons such as Security, Loss of Connection (Weak Signal) etc
That's it. Total Installation time About 35 minutes. 25 mins for installing the OS and 10 mins for the rest of setup so you can send emails Via PHP.
The Setup
1) Install UBUNTU Server Visit Ubuntu to Download the Latest Version.
Server not the Desktop Version as its a bit slower than the Server version. However the downside to using the Server version is that you need to learn some basic commands on how to edit, copy, move, delete, change permissions of files around the server via command line. But its not rocket science.
On the good side hosting websites on Linux Server which may hold 100's of files of scripts, CSS, and Web pages, what you can do is install SAMBA server which enables you to share your web folder(s) or any folder for that matter over your Local Network or from Outside so on another PC using Lets say Windows can connect to that folder and will enable you to use all your favorite software on windows for editing and Saving those files on your Linux Server which is easier rather than using editing utilities directly on the server. takes about a minute to install and setup a Samba server. I will add that to the bottom of this Guide.
once that's done
2) Install LAMP Server
This will Install
Apache Web Server, Mysql Database, and PHP
Pretty much all you need to start hosting websites (Apache), have databases (Mysql) for web uses and a server side language to handle all the work (PHP).
command
(sudo is required in ubuntu in front of commands so you can run them in admin mode which is more Secure. It will ask you for your account password, before running the rest of the command)
sudo tasksel install lamp-server
Let it run, It will Auto download the required files so make sure you are connected to the internet. The only question you will be asked is type in a password for the root access of Mysql database.
Apache, Mysql, PHP can be further configured so there is some basic learning curve for the future.
I never had to configure Mysql, for Apache configuration i usually change the Path to where my website is and some Security settings. The default directory where your web related files go into is (/var/www/html). This html directory is where all the PHP, HTML files go into. However, there's no Reason why you cant rename this folder and reconfigure apache to directly use it as your root web folder. Ill explain how to do this near the end of this guide. For PHP configuration, you may need at some point to install extra extensions such as php5-imagick which is a library for editing images using PHP. There are many extensions that can enable PHP to do many things.
Halfway there
Email Setup using Postfix which is a way to send emails
command
sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
After a few moments it will Present You with Some Options In setting Up Mail.
You want (Internet Site) i think its the 3rd option Down. Choose this
You will then see your hostname. No action Required here, Continue
That's the Mail Done, but we need to configure some bits to it so it knows about the mail configuration
open your postfix config file
command
sudo nano /etc/postfix/main.cf
add the following in the file, you can put it add the end of the file
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes
you may see (relayhost =) already in the file. what you can do is delete that line and add all the above there.
nano is a text editor, so freely move with the cursor keys up down, delete, type etc. When you finish editing. do CNTRL X and press Y (Yes) to write and exit the file. If you made a mistake in the file press N (No) to just exit without writing to the file.
Almost there
edit the Postfix email Account Information file and insert (In our example Gmail account) logging information.
command
sudo nano /etc/postfix/sasl_passwd
should be a blank file, so insert
[smtp.gmail.com]:587 USERNAME#gmail.com:PASSWORD
change USERNAME#gmail.com to your email account and PASSWORD to the emails password. It needs this info so Postfix can connect to Gmail and send mails
I suggest creating a new fresh email account with your App's Name.
my_app_name#gmail.com so you have a distinguished account name for sending emails through your app.
CNTRL X and Y to save and exit the file
Now we fix some permissions to the files
Fix permission and update postfix config to use sasl_passwd file:
commands
sudo chmod 400 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd
Next, validate certificates. You say what??? Just run the following command, Do you see a bunch of Random characters about 200 of them? If so that's your servers Certificate for sending Mails.
command (no further action is required as cat is the command to just list a file on screen)
cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem
ALL DONE You can send mails now, However we made some changes to the files above so lets reload Postfix first to take in the changes.
command
sudo /etc/init.d/postfix reload
Do a test
command to send an email from the server
echo "This is my first test mail from my server" | mail -s "From Me" myemail#gmail.com
Check did you get the mail?
If yes then you are able to send mails directly from the server
Ok now that you can send emails from your web server you need to use Ajax Post to post the form data in your java script files over to a PHP file on your web server and compose the email and send it.
Before you consider buying a domain name (mywebsitename.com) you can start testing using the WAN address of your home router.
If you don't know the current address open a browser and type (What is my WAN address)
eg.
http://wanip.info/
this will show your WAN address. xxx.xxx.xxx.xx
this is the number you put in to your AJAX requests
example
$.ajax({
method: "POST",
url: "http://xxx.xxx.xxx.xxx/some.php",
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
More info about Ajax here
http://api.jquery.com/jquery.ajax/
But first we finish off completing some minor tasks
You router needs to know your webeservers IP address so it can send HTTP requests and Data to it.
How to find the Webserver IP and Routers IP addresses using Linux
command
ifconfig
you will something like this
eth0 Link encap:Ethernet HWaddr 08:00:27:87:35:25
inet addr:192.168.2.15 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe87:3525/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:861 errors:0 dropped:0 overruns:0 frame:0
TX packets:549 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:125639 (125.6 KB) TX bytes:63563 (63.5 KB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:56 errors:0 dropped:0 overruns:0 frame:0
TX packets:56 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3360 (3.3 KB) TX bytes:3360 (3.3 KB)
You are Interested in the Ethernet Network Card eth0 (if you have installed more than one network card you may also see (eth1, eth2 etc) and this inet addr:192.168.2.15 is your servers address. Not Static Yet
command
route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.2.1 0.0.0.0 UG 0 0 0 eth0
192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
this 192.168.2.1 (under the Gateway next to 0.0.0.0) is the Routers Local Address
Please note, yours may be different
open a browser and type that in
http://192.168.2.1
you will be presented with your routers home page. Log in and find Virtual Servers settings. Should be in the General Firewall Options.
You should see a Table like this
Depending on your routers model you should see something similar. Use the drop down menu and choose WEB HTTP. Click add. The 80 80 .. that's the ports the firewall will open for WEB communication. Enter the last 2 digits of your web servers address and click Save. Your router may reboot to save the changes.
Lets install SAMBA server so you can share your web folder to your Local Network so you can access it from a local PC. In this example we use Windows.
command
sudo apt-get install samba
After it has finished Installing edit the samba configuration file so we can add the folder we want to share in our case /var/www/
command
sudo nano /etc/samba/smb.conf
go to the bottom of the file and add
[share]
comment = Ubuntu File Server Share
path = /var/www/
browsable = yes
guest ok = yes
read only = no
create mask = 0777
CNTRL X and Y to save and exit the file
i suggest checking the net for samba share configuration as the above setup does not have much security in mind. You can do much more like request a password to connect, disable guests, etc.
we change the Permissions of the web Folders to full. Although not recommended, as you have a blank web folder you should inherit normal user permissions as you create files and when you learn about changing permissions of files and directories on Linux in the future you change them.
command
sudo chmod -R 777 /var/www/
basically the 777 is spread into 3 levels of permissions and it represents binary 111/ 111/111 -- binary number (111) is in the real world 7 so READ(1), WRITE(1), EXECUTE(1) are all turned on for those 3 levels which its not very secure. I suggest you read more about the chmod command and file permissions to get a better understanding about its use.
It's time to give the server a Reboot
sudo shutdown -r now
Now The Reboot is done, Lets connect to that shared Web Folder
Assuming you have a PC running windows connected to the same Local Network as the web server (Mac should be something similar but check on how to connect to a shared drive)
Open Computer, choose Map Network Drive type in the web servers IP address e.g \\192.168.2.23\ - click Browse You should see the folder we shared in the SAMBA configuration file. Connect to it.
Treat that drive like any drive to save your files only this one is the root folder of your web server website(s) folder.
However on the webserver if you don't want the default html folder eg you want a custom name for your website, on windows right click on the folder and rename it.
If you do rename it you need to let Apache know by doing the following
command
sudo nano /etc/apache2/sites-enabled/000-default.conf
change DocumentRoot /var/www/html to DocumentRoot /var/www/thenewname
CNTRL X and Y to exit nano
restart apache webserver to take in the change
sudo /etc/init.d/apache2 restart
do a test page
write a simple index.html webpage or index.php php page and save it in the web folder
open a browser and type in your WAN adress
here is a link on how to compose and send an email with PHP
https://css-tricks.com/sending-nice-html-email-with-php/
Ok last note
You have everything running, but Home Routers Have Reserve IP address that give each local PC automatically when they connect to the network. Not a big issue, but its best to give the web server a static IP address
Log in to your router again and go to your Lan Setup specifically we want to check the DHCP Server IP Pool Settings. Dhcp is responsible for giving out IP address automatically to each PC in a local network so it reserves around 100 of these out of possible 253 address on a home router. We need to check the range and choose one above that range.
You should see something like this. From 192.168.2.2 to 192.168.2.100 are reserved by the Dhcp service. So logic has it we can choose 192.168.2.101 as an address for the server as this is not used by the Dhcp service. In fact 192.168.2.101 to 192.168.2.254 are free as the complete range of a routers IP address are from 2 to 254 the 0, 1 and 255 are used by the router.
edit the network file on your webserver
command
sudo nano /etc/network/interfaces
Copy what you see below, (without the //remarks) and delete the existing setup in the configuration file for eth0 which is the Ethernet card 0.
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.2.101 //the chosen IP address for the webserver
gateway 192.168.2.1 // the routers address
netmask 255.255.255.0 // leave this as is
network 192.168.2.0 // change x.x.2.x if you are on the 1 network. ie 192.168.1.0 some routers are on the 1 network
broadcast 192.168.2.255 // same as above
dns-nameservers 8.8.8.8 //check below
CNTRL X and Y to exit nano
Just to clarify dns-nameservers 8.8.8.8 uses a Google Public DNS server to translate web address to IP addresses. Its a general use one. You can change that to any Public DNS Address you know, but it is required because we now have manual settings for the webserver.
Reboot the server again to load in the changes to the network and lastly log in to your router and change the IP number for your Virtual server to send HTTP Requests as shown in the above Step we did earlier.
I hope this guide helps you

Categories

Resources