Bash Script: Easily Backup your Magento2 Code Files + Database

October 2, 2017  |  No Comments  |  by Raj (MagePsycho)  |  Linux, Mac OS-X, Magento 2

We had created a similar Backup Script for Magento1 in the past. Now we are back with the much-improvised version for Magento2.
You must be wondering why we needed a bash script for Magento2 backup even it comes up with inbuilt console command:

php /path/to/magento2/bin/magento setup:backup -v --code --media --db

Some of the reasons are:

  • Linux native command is always faster than PHP.
  • There is no enabling and disabling of maintenance mode while running the backup script. So there is no any downtime.
  • To have more Freedom. Magento2 console script is limited. What if you want to backup in Amazon S3, Google Drive etc.? If you have independent script you can play as you like without worrying about breaking the Magento2 system.

You can find the full script below:

Please visit our GitHub project page for more info: MagePsycho/magento2-db-code-backup-bash-script


You can simply download the script file and give the executable permission.
In Linux system this can be simply done as:

cd /path/to/your/preferred/download/folder
curl -O
chmod +x

If you want to make this command system-wide then

sudo mv /usr/local/bin/


To display help

./ --help


To backup database only

./ --type=db --src-dir=/path/to/magento2/root --dest-dir=/path/to/destination

To backup code only

./ --type=code --skip-media=1 --src-dir=/path/to/magento2/root --dest-dir=/path/to/destination

To backup code + database

./ --type=all --skip-media=1 --src-dir=/path/to/magento2/root --dest-dir=/path/to/destination

To schedule backup via Cron
You can run this command manually (on demand) or via cron.
If you want to schedule via Cron, just add the following line in your Crontab entry (crontab -e)

0 0 * * * /path/to/ --type=all --skip-media=1 --src-dir=/path/to/magento2/root --dest-dir=/path/to/destination > /dev/null 2>&1

0 0 * * * expression means the command will run run at every midnight. You can refer to to know more about the cron-expression.

Hope you will find this script useful in your day-to-day operation. Any issues found can be raised at our GitHub project – Bash Script: Backup Magento2 Code + Database

Let us know your experience/feedback on this script.

Best Resources to Help You Learn Magento

August 29, 2017  |  No Comments  |  by Raj (MagePsycho)  |  Magento, Magento 2

Learning Magento may seem like a laborious task at first, but when you know the right
resources and the right places to head to, it may not be that difficult to master the platform.
Magento is one of the biggest e-commerce platforms and it powers some of the biggest websites
on the internet. Even small e-commerce stores prefer to base their online stores on Magento and
it’s an understatement to say that it’s just popular. Hence, if you are looking forward to starting
learning the platform and start a development career, you are on the right path!
Now let’s look into the best Magento resources on the internet that will help you in your
endeavor to master Magento and step into the world of e-commerce with confidence.

Official Magento Documentations

While we are on the subject of learning Magento, the Official Magento Documentation needs no introduction. Since it comes directly from the Magento labs, it’s the surest way to learn and know your way around Magento without losing focus. While there are several other resources that can help you up your development game, the official document takes the lead by default!

Head over to the official documentation if you are looking for a direct source of Magento knowledge!

Alan Storm

If there is one person who stands out and makes a significant impact in the Magento Community, it’s Alan Storm. Alan is a programmer who makes things easier for anyone who has a hard time understanding code! His professional web blog of the same name is a trusted source of how-to’s and if you are looking forward to learning from a professional, look no further than Alan Storm.

The blog is not limited to just Magento, though, and there’s a lot you can look forward to if you are new to the coding business!

Inchoo’s Blog

Inchoo is one of the leading Magento-based software developers. Founded in 2008, Inchoo is well-known for its blog, which is a great source for insight into Magento development. Among all the blogs based on Magento, Inchoo is among the ones that maintain quality and aims to teach and educate the readers than just producing articles for the sake of it.

If you are here to learn Magento and improve your understanding, head over to Inchoo’s blog to get some serious knowledge.


Magenticians, as suggested by the name, is a dedicated Magento blog and one of the fastest growing blogs in the industry. With a tagline that says ‘Magento Made Easy’, Magenticians is a great pool of resource to help ease your Magento journey with regular how-tos, tutorials, e-commerce articles, and interviews with Magento Influencers etc., to keep you up to date with all things Magento.

From all the resources available, Magenticians is the most regular in posting quality content and Muneeb Ul Hasan’s tutorials are on-point and worth looking into!

MagePsycho’s Magento Blog

The MagePsycho’s blog is another useful resource with a roster of several experts and Magento developers. Not only does the blog give you great tutorials, but you can stay up to date with all the latest industry news and happenings!

Don’t forget to check out Raj KB’s tutorials and guides, as they are always insightful and bring something new to the table!

Note: More list will be added soon.


There are hundreds of other blogs in the blogosphere that publish Magento related content, but there are factors that allow some to lead the way and are considered the best among the rest. And that is why we go through the necessary research and sieve out the best ones for you. We’ve listed the ones we think are necessary for anyone jumping into the Magento world, but if you feel that there is some blog that deserves a mention in this list, let us know in the comments below and we’ll check it out!

Magento2: Fastest Way to Import / Update Product Prices in Bulk

March 19, 2017  |  No Comments  |  by Raj (MagePsycho)  |  Magento 2


As you know our Magento1 version of the script “Updating product prices in Magento in an easier & faster way” was a massive hit. And we are back with the similar script for Magento 2 which helps you to update the product prices in bulk – probably the easiest and fastest way possible.

Let’s take a look at the standalone PHP script & sample CSV file below:

How to run the script?

1. Prepare the Script

Copy/Paste the script from above to the file: [magento2-root]/pub/m2-import-prices-index.php

2. Prepare the CSV Data

Download the sample CSV file as “prices.csv” and update values for SKU & Price columns. Then upload the CSV file to the [magento2-root]/var/import/ directory.

3. Run the Script

You can run script either from browser or from CLI.

3.1 Executing the price bulk update from Browser
Go to the browser and hit the URL:

Which will out as below on successful operation:
Magento2 Bulk Price Update via Web

Note: If you are using Nginx web server and want to change the script filename other than *-index.php, you may need to edit the nginx.conf.sample file of Magento2 as

# PHP entry point for main application
location ~ (index|get|static|report|404|503|AddYourNewFileNameHere)\.php$ {

And of course need to reload the nginx configuration thereafter:

# Ubuntu
sudo service nginx reload

3.2 Executing the price bulk update script from CLI
Simply run the following command(s) from the Console of your server:

cd /path/to/magento2-root
php -f pub/m2-import-prices-index.php

Which will output as:
Magento2 Bulk Price Update via CLI

Note: This is recommended approach if you want to bulk update CSV data is big.

Hope Magento2 Developers & Store Owners will find this script useful.
Please do show your love towards the script by sharing & commenting below.

Bash Script: Create MySQL Database & User with Optional Password

January 3, 2017  |  No Comments  |  by Raj (MagePsycho)  |  Linux, Mysql

I have been using DigitalOcean for my personal projects. And one repeated things that I had to do is the creation of MySQL database & user. I know I could have used some CPanel like GUIs and even some of them came to my mind like VestaCP, Ajenti, ServerPilot etc. But all comes with sort of things and not so developer friendly.

So I decided to create a simple bash script that can simply the database & user.

Bash Script Code

You can find the script at Gist:

How to use?

1# Download
Download the script from Gist – Bash Script: Create MySQL Database & User

2# Set Permission
You need to set the executable permission in order to execute the shell script:

chmod +x

3# Usage
Use the script as:

./ [--host="<host-name>"] --database="<db-name>" [--user="<db-user>"] [--pass="<user-password>"]

So the only required parameter is database name. In the case of empty values for other parameters:

  • ‘host’ becomes localhost
  • ‘user’ takes value from database name
  • ‘password’ is randomly generated

For the following command:

./ --database=bash_db2

The output looks like:

Bash Script: Create MySQL Database & User with Optional Password

Bash Script Console: Create MySQL Database & User with Optional Password

Hope you find this script useful.
Please do let us know any feedback in the comments below.

How to set up multiple Magento 2 websites / stores with Nginx?

September 21, 2016  |  6 Comments  |  by Raj (MagePsycho)  |  Magento 2, Nginx

Magento is so flexible that one instance of it can have multiple websites/stores with different languages, domain names, categories etc.
You can configure the websites, stores, and store views in the Magento Admin. You use the MAGE_RUN_TYPE and MAGE_RUN_CODE variables in entry point scripts(index.php), .htaccess or Nginx configuration files(depending upon the web server you are using) to start the Magento application using these websites or store views.


The code (i.e. MAGE_RUN_CODE and MAGE_RUN_TYPE) checks two environmental variables


and use them to start Magento with the right website/store which can be defined in Manage Stores section in Admin.

The value of MAGE_RUN_TYPE determines if MAGE_RUN_CODE should be considered the code of a website or a store.

  • If MAGE_RUN_TYPE = ‘website’ is used, MAGE_RUN_CODE should be the code of the website and default store for this website will be loaded in the frontend.
  • If MAGE_RUN_TYPE = ‘store’ is used, MAGE_RUN_CODE should be the code of any store view and that particular store will be loaded in the frontend.

Nginx Configuration Settings

As mentioned before, there are many ways to configure the MAGE_RUN_TYPE and MAGE_RUN_CODE environment variables. But setting environment variables using web servers would be the best way as it doesn’t involve any core code edits.
Since I am using MEMP Stack, I will be sharing on how to configure Nginx for multi-website/store environment.

Case 1: One Website, Multiple Store Views

For example, we have

  • (store code: mystore_en)
  • (store code: mystore_de)
  • (store code: mystore_es)

Step 1. Edit your Nginx virtual host configuration file as

File: Usually located under /etc/nginx/sites-available/ or /usr/local/etc/nginx/sites-available/ or other depending upon the OS type.

map $http_host $MAGE_RUN_CODE { mystore_en; mystore_de; mystore_es;
server {
    listen 80;
    set $MAGE_ROOT /path/to/your/magento2;
    set $MAGE_MODE default;
    include /path/to/your/magento2/nginx.conf.sample;

Here you can see how the Nginx map block is used to set MAGE_RUN_CODE as per host. And server_name directive includes all the available domains.

Step 2. Send MAGE_RUN_CODE and MAGE_RUN_TYPE variables to the php-fpm server
File: include path from above, example: path/to/your/magento2/nginx.conf.sample

# PHP entry point for main application
location ~ (index|get|static|report|404|503)\.php$ {
    try_files $uri =404;
    fastcgi_pass   fastcgi_backend;
    fastcgi_buffers 1024 4k;

    fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
    fastcgi_param  PHP_VALUE "memory_limit=768M \n max_execution_time=600";
    fastcgi_read_timeout 600s;
    fastcgi_connect_timeout 600s;
    #add here - start
    fastcgi_param  MAGE_RUN_TYPE store;
    fastcgi_param  MAGE_RUN_CODE $MAGE_RUN_CODE;

    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;

Here Nginx fastcgi_param‘s MAGE_RUN_TYPE & MAGE_RUN_CODE will create the enviroment varilables for PHP, so that the script can access the values via


Case 2: Multiple Website, Multiple Store views

For example, we have

  • (website code: mystore1)
    • (store code: mystore1_en)
    • (store code: mystore1_de)
    • (store code: mystore1_es)
  • (website code: mystore2)
    • (store code: mystore2_en)
    • (store code: mystore2_de)
    • (store code: mystore2_es)

In order to configure for this case, you can use the similar settings as for Case 1. The only difference will be the store code values for Nginx map block and domain names for server_name directive.


If you are unsure about which value(website or store) should you use for MAGE_RUN_TYPE, go for the store value and use the corresponding store code for MAGE_RUN_CODE

Hope this helps you in configuring the Nginx for multi-store/website. In the case of any issues with the multi-store setup, please do comment below.