tiistai 28. huhtikuuta 2015

Backup Wordpress Database

This should get a full backup of your database including users.
mysqldump --routines --flush-privileges --databases DBNAME > /home/USERNAME/dump-$(date +\%d\%m\%y).sql -u root -pPASSWORD

Then just add the database to your new server and remember to get wp-config.php file also. 

Update Wordpress and file permissions under Linux

To update Wordpress without FTP, install plugins and upload media do the following:

Insert to Wordpress config.php file this line: define('FS_METHOD','direct');

Then set under wp-content folder plugins and uploads folder to user www-data

chown www-data plugins
chown www-data uploads

The left one in permissions list is user and on the right group.

TO UPDATE you need to give www-data access for a short time. So do sudo chown www-data wordpress/ -R then update, and after that give access back to your user.

Setting the folder upgrade for www-data should allow upgrading, but unfortunately I couldn't test this. Using chomd 777 -R for the whole wordpress folder allows updating, but that is not a good idea.

Wordpress defaults are 755 for folders, and 644 for files. To set things back use chmod 755 -R and then find:

To recursively give directories read&execute privileges:
find /path/to/base/dir -type d -exec chmod 755 {} +
To recursively give files read privileges:
find /path/to/base/dir -type f -exec chmod 644 {} +

Or, if there are many objects to process:
chmod 755 $(find /path/to/base/dir -type d)
chmod 644 $(find /path/to/base/dir -type f)

Or, to reduce chmod spawning:
find /path/to/base/dir -type d -print0 | xargs -0 chmod 755 
find /path/to/base/dir -type f -print0 | xargs -0 chmod 644

keskiviikko 15. huhtikuuta 2015

Scan all IP addresses in the same local network

Scanning through all IP addresses in same local network is easy task with Linux.

Install arp-scan:
$sudo apt-get install arp-scan

Scan all ip's:
$sudo arp-scan --interface=eth0 --localnet

Where "eth0" is your network device, if you don't know what it is, do ifconfig to check it out:

maanantai 13. huhtikuuta 2015

Linux aliases

You can create aliases to your linux system by editing file:


Update aliases by running:

sudo apt-get update

For example if you install composer:

curl -sS https://getcomposer.org/installer | php


You can then create alias for composer by editing the bash file and typing:

alias composer='/usr/local/bin/composer'

Now you can run composer anywhere just by typing composer


Crontab timed mysql dump and commands

Crontab is timing program included in Linux systems. With crontab you can execute commands automatically on specific times and dates.

To open crontab give command $crontab -e 

You can run this command with root or non root person. If you want to do root things or you need to do command as root use root.

* * * * * command
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ └──── day of the week (0–7) (sunday = 0 or 7)
│ │ │ └────── month (1–12)
│ │ └──────── day (1–31)
│ └────────── hours (0–23)
└──────────── minutes (0–59)

Here are few examples how to use crontab. For example I want to make a tarball from my homepage. This command would do the tarball under /home/shnigi and tarball name would be mysite.tar.gz. The tarball is created from /home/shnigi/public_html the command would run every day 15:12

12 15 * * * tar -czvf /home/shnigi/mysite.tar.gz /home/shnigi/public_html

To backup your MySQL database use command:

30 15 * * * /usr/bin/mysqldump --all-databases > /home/shnigi/dump.sql -u root -pPASSWORD

This would run every day 15:30 and take backup about all databases to /home/shnigi/dump.sql. Note that password is inserted right after the -p without space. The code under this is the same, but it generates date for every mysql dump file.

12 16 * * * /usr/bin/mysqldump --all-databases > /home/dump-$(date +\%d\%m\%y).sql -u root -p

You can try these commands without crontab, just use these:

#SQL dump
32 16 * * * /usr/bin/mysqldump --routines --flush-privileges --all-databases > /home/dump-$(date +\%d\%m\%y).sql -u root -pPASSWORD

#backup all home folders
26 16 * * * tar -czvf /home/homefolders.tar.gz /home


Remove .php extension

To remove .php extension from your site url, for example testsite.com/blog.php to just testsite.com/blog you need to do the following in your system:

$sudo a2enmod rewrite which allows apache to rewrite names. Then create hidden .htaccess file in your website root folder and type:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Edit your navigation page and remove all extensions. Like this: 

You also need to edit your virtualhost files which allows this.