Tag Archives: linux

Linux / Windows : Automatically backup mysql (on linux) and tranfer backup file to windows (via FTP)

Automatically backup mysql (on linux) and tranfer backup file to windows (via FTP).
This task will involve 2 types of environment (OS), Windows and Linux.

[Windows Section]
1. Setup FTP server on your windows environment using windows own software or third party software.
2. Create an account and allow connection only from the server(Linux) IP address.

[Linux Section]
1. Create an account on mysql with backup privileges ONLY (normally all read privileges).
2. Create an folder in your home directory. Example : /home/anas/mysql_backup_scheduler
3. Create an empty shell file inside /home/anas/mysql_backup_scheduler. Example: backup_and_ftp.sh
4. Inside backup_and_ftp.sh , put the content below and replace accordingly : –

---- BOF -----

#!/bin/bash

DATEC="/bin/date"
DATE=`${DATEC} +%Y%m%d_%H%M`
DATEM=`${DATEC} +%Y-%m-%d`
DBNAME=""
DBBACKUP="$DBNAME"-${DATE}.sql
HOST=''
USER=''
PASSWD=''

cd /home/anas/mysql_backup_scheduler/
/usr/bin/mysqldump -u -p --databases "$DBNAME" > ${DBBACKUP}

echo "Done Backup ..." ${DATE}

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put ${DBBACKUP}
quit
END_SCRIPT

echo "Done Upload ..." ${DATE}

#rm ${DBBACKUP} # uncomment only when you want to delete old files
exit 0

---- EOF ----

Finally, add this script to your cron.

Advertisements

Linux : Scheduling using “cron” and “at”

Basic scheduling commonly use in linux OS.

Cron (best use for repetition task only)

Format: (split each with space)
# min(0-59) hour(0-23) dom(1-31) mon(1-12) dow(0-6 0=sun) command #

Example:
1. Run task every 2 hours
* */2 * * * /path/to/task/file/command

2. Run task every day at 11:30 PM
30 23 * * * /path/to/task/file/command

3. Run task every Sunday at 10.00 pm
00 22 * * 1 /path/to/task/file/command

At (best use for one time task)

from at manual page :

at and batch read commands from standard input or a specified file which are to be executed at a later time, using /bin/sh.

at executes commands at a specified time.

atq lists the user’s pending jobs, unless the user is the superuser; in that case, everybody’s jobs are listed. The format of the output lines
(one for each job) is: Job number, date, hour, queue, and username.

atrm deletes jobs, identified by their job number.

batch executes commands when system load levels permit; in other words, when the load average drops below 0.8, or the value specified in the invoca tion of atd.

Example:

1. Run task on 1:00 PM once
# at 1:00 PM
at > /path/to/task/command
at > ^d

Ctrl + d to exit
Also can use 23:00 for 1:00 PM

2. Run task 2 days from today
# at now + 2 days
at > /path/to/task/command
at > ^d

3. List all task
# atq

4. View detail ( at -c number of task)
# at -c 3

5. Remove task (atrm number of task)
# atrm 3

Reference taken from : http://www.computerhope.com/unix/uat.htm

the expression: would translate to:
noon ---> 12:00 PM October 18 2014
midnight ---> 12:00 AM October 19 2014
teatime ---> 4:00 PM October 18 2014
tomorrow---> 10:00 AM October 19 2014
noon tomorrow ---> 12:00 PM October 19 2014
next week ---> 10:00 AM October 25 2014
next monday ---> 10:00 AM October 24 2014
fri ---> 10:00 AM October 21 2014
NOV---> 10:00 AM November 18 2014
9:00 AM ---> 9:00 AM October 19 2014
2:30 PM ---> 2:30 PM October 18 2014
1430 ---> 2:30 PM October 18 2014
2:30 PM tomorrow ---> 2:30 PM October 19 2014
2:30 PM next month ---> 2:30 PM November 18 2014
2:30 PM Fri ---> 2:30 PM October 21 2014
2:30 PM 10/21 ---> 2:30 PM October 21 2014
2:30 PM Oct 21---> 2:30 PM October 21 2014
2:30 PM 10/21/2014 ---> 2:30 PM October 21 2014
2:30 PM 21.10.14 ---> 2:30 PM October 21 2014
now + 30 minutes ---> 10:30 AM October 18 2014
now + 1 hour ---> 11:00 AM October 18 2014
now + 2 days ---> 10:00 AM October 20 2014
4 PM + 2 days ---> 4:00 PM October 20 2014
now + 3 weeks ---> 10:00 AM November 8 2014
now + 4 months ---> 10:00 AM February 18 2015
now + 5 years ---> 10:00 AM October 18 2019

Remember to check your server time first : # date

Credits to :
https://debian-handbook.info/browse/stable/sect.task-scheduling-cron-atd.html
http://www.computerhope.com/unix/uat.htm
Linux Manual Page

Tested on Centos 6/7

Linux : Network Time Protocol (ntp) Configuration & Timezone


+------------------------------+
| Timezone |
+------------------------------+

First, get the current timezone of your server :
# timedatectl

You should get something like this :-

Local time: Mon 2017-05-08 09:34:14 +08
Universal time: Mon 2017-05-08 01:34:14 UTC
RTC time: Mon 2017-05-08 01:34:14
Time zone: Asia/Kuala_Lumpur (+08, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a

If it is not your timezone, change it via this command:-
# timedatectl set-timezone Asia/Kuala_Lumpur

You can get the list of time zone from “/usr/share/zoneinfo” or # timedatectl list-timezones
The other way to know your current timezone is to
# ls -ld /etc/localtime
and the output similar to this
lrwxrwxrwx. 1 root root 39 Aug 3 2015 /etc/localtime -> ../usr/share/zoneinfo/Asia/Kuala_Lumpur

Also you can make symbolic link like this …

# ln -s /usr/share/zoneinfo/Asia/Kuala_Lumpur /etc/localtime

if not work, also try change this file content

# vim /etc/sysconfig/clock
change to
ZONE="Asia/Kuala_Lumpur"


+---------------------------------------+
| Network Time Protocol (NTP) |
+---------------------------------------+

Firstly, install the ntp :-

# yum install ntp nptd ntpdate

After that, sync the time :-
[Optional]
# ntpdate -s asia.pool.ntp.org

Then,

# vi /etc/ntp.conf

and replace or change setting similar ( Not compulsory, you can just leave it )
[Optional]

server 0.asia.pool.ntp.org iburst
server 1.asia.pool.ntp.org iburst
server 2.asia.pool.ntp.org iburst
server 3.asia.pool.ntp.org iburst

restart the service

# service ntpd restart
or
# systemctl restart ntpd.service

other command related to NTP :-

ntpd - Network Time Protocol (NTP) daemon
ntpdate - set the date and time via NTP
ntpdc - special NTP query program
ntpq - standard NTP query program
ntpstat - show network time synchronisation status
ntptime - read and set kernel time variables

p/s:
please get nearest ntp server list from http://www.pool.ntp.org/zone/my.
Tested on Centos 7 and Centos 6.

references :
https://www.certdepot.net/rhel7-set-ntp-service/
https://www.cyberciti.biz/faq/centos-linux-6-7-changing-timezone-command-line/
https://www.tecmint.com/install-ntp-server-in-centos/

Linux : Vi and Vim Short Notes

Writing to file when your forgot to run Vi/Vim as root privileges.
:w !sudo tee %

Searching in Vi/Vim.
:%s///g

Copy and paste line in Vi/Vim.
[Esc] yy -
p - paste after cursor
P - paste before cursor

Reference:-
http://vim.wikia.com/wiki/
http://stackoverflow.com/

Linux : Add new startup script using Systemd

I has been working with current CentOS 7 using Systemd feature. Here is the script I use to start one of my program.

Create an empty file with name <what ever name>.service

Filename: runbind.service

Content of the file :

BOF >>>
[Unit]
Description=Runbind Reload
After=syslog.target
After=network.target


[Service]
Type=simple
User=root
Group=root
ExecStart=/bin/sh /opt/runbind.sh
Restart=always


# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300


[Install]
WantedBy=multi-user.target

<<<EOF

Save this file in /usr/lib/systemd/system/

Note: You will want to change this parameters :-

Please exclude BOF and EOF lines
User -> which user account you need to run this script
Group -> user’s group
ExecStart-> full path to your program or script
Description-> description of the script

Tested on CentOS Linux release 7.2.1511 (Core)

 

Credit to : 
https://www.redmine.org/boards/1/topics/32763?r=43267
https://www.digitalocean.com/community/tutorials/how-to-configure-a-linux-service-to-start-automatically-after-a-crash-or-reboot-part-1-practical-examples

 

Linux : Loading new kernel without “really” rebooting the server – CentOS

This is VERY interesting article I found on the internet. 
Thanks to KoRoVaMiLK#

You have that server you can’t reboot but need to patch the kernel NOW (eg. #kernel flaw found#).. well, you can try this out.

First of all, we print out running kernel version:

# uname -r
2.6.32-71.29.1.el6.i686

Ok, we have to patch:

# yum update kernel*

Grab the kexec tools:

# yum install kexec-tools

Now we get last installed kernel version release and put it on a var:

# latestkernel=`ls -t /boot/vmlinuz-* | sed "s/\/boot\/vmlinuz-//g" | head -n1`

# echo $latestkernel
2.6.32-220.4.1.el6.i686

Now we need to load the new kernel version in memory:

# kexec -l /boot/vmlinuz-${latestkernel} --initrd=/boot/initramfs-${latestkernel}.img --append="`cat /proc/cmdline`"

Finally, we can issue a reset:

# kexec -e

..and.. wow, we lost the system! ..Well, not exactly.

The system will “restart without restarting”..something like a fast reboot, without performing BIOS checks (and you know how long can a full system restart last).

# uname -r
2.6.32-220.4.1.el6.i686

It worked!

* Be aware that kernel reset will perform a connection reset as well, together with resetting your uptime, so if you’re searching for something to grant your uptime record while security patching, well, this is not for you.

 

Credit to :

http://blog.agate.io/post/16460518079/running-new-linux-kernel-without-rebooting

Linux : how to read output parse from “free” command

for example :

[myapit@linuxserver ~]$ free -m
             total  used  free  shared  buffers  cached
Mem:         7872   7732  139   0       1478     4346
-/+ buffers/cache:  1907  5964
Swap:        4095   45    4050

from the output above, we can get :-

First Line :

  • 7872 – total physical RAM size
  • 7732 – total RAM used include buffers and cached
  • 139 – total free RAM to be used by new process
  • 0 – shared memory, obsolete (not use anymore)
  • 1478 – total RAM buffered by applications
  • 4346 – total RAM used as cached by applications

Second Line:

-/+ buffers/cache:  1907  5964

What is the difference between buffers and Cache?
A buffer is a temporary location to store data for a particular application and this data is not used by any other application. This is similar to bandwidth concept. When you try to send burst of data through network, if your network card is capable of sending less data, it will keep these huge amounts of data in buffer so that it can send data constantly in lesser speeds. In other hand Cache is a memory location to store frequently used data for faster access. Other difference between a buffer and a cache is that cache can be used multiple times where as buffer is used single time. And both are temporary store for your data processing.

 

Third Line: -linux swap
Swap:        4095   45    4050

:: calculation to get the actual used and free ram
 Used RAM = 7732 - from first line
 Used Buffer = 1478 - from first line
 Used Cached = 4346 - from first line
Actual total used RAM = 7732 - (1478+4346) = 1908
Total available RAM = 7872 - 1908 = 5964

based on calculation above, in my opinion, we can use information from Second Line to get the actual used and free ram as the value after calculation seems to be the same

 


Reference:


http://www.linuxnix.com/2013/05/find-ram-size-in-linuxunix.html
http://unix.stackexchange.com/questions/152299/how-to-get-memory-usedram-used-using-linux-command