MySQL dump examples

Dump database to file with date in name:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mysqldump -u USERNAME -pSECRETPASSWORD --single-transaction --quick --lock-tables=false DBNAME_HERE > /home/backup/mysql/DBNAME_HERE_`date "+%Y-%m-%d-%H-%M"`.sql
mysqldump -u USERNAME -pSECRETPASSWORD --single-transaction --quick --lock-tables=false DBNAME_HERE > /home/backup/mysql/DBNAME_HERE_`date "+%Y-%m-%d-%H-%M"`.sql
mysqldump -u USERNAME -pSECRETPASSWORD --single-transaction --quick --lock-tables=false DBNAME_HERE > /home/backup/mysql/DBNAME_HERE_`date "+%Y-%m-%d-%H-%M"`.sql

Crontab version: dump every hour at minute 5

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
5 * * * * mysqldump -u USERNAME -pSECRETPASSWORD --single-transaction --quick --lock-tables=false DBNAME_HERE > /home/backup/mysql/DBNAME_HERE_`date "+\%Y-\%m-\%d-\%H-\%M"`.sql
5 * * * * mysqldump -u USERNAME -pSECRETPASSWORD --single-transaction --quick --lock-tables=false DBNAME_HERE > /home/backup/mysql/DBNAME_HERE_`date "+\%Y-\%m-\%d-\%H-\%M"`.sql
5 * * * * mysqldump -u USERNAME -pSECRETPASSWORD --single-transaction --quick --lock-tables=false DBNAME_HERE > /home/backup/mysql/DBNAME_HERE_`date "+\%Y-\%m-\%d-\%H-\%M"`.sql

Keep 2 folders synchronized

Code to copy all changes from one folder to another. I use it to copy changes from my local folder to development server mounted by SSHFS.

In example files are filtered. Only *.cpp and *.h files are copied.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
while inotifywait -e modify,create,delete "/home/jskalski/p/prv/otserv/"; do
rsync -zvur --include="*.cpp" --include="*.h" --exclude="*" "/home/jskalski/p/prv/otserv/" "/mnt/devserver/home/js/otserv/"
done
while inotifywait -e modify,create,delete "/home/jskalski/p/prv/otserv/"; do rsync -zvur --include="*.cpp" --include="*.h" --exclude="*" "/home/jskalski/p/prv/otserv/" "/mnt/devserver/home/js/otserv/" done
while inotifywait -e modify,create,delete "/home/jskalski/p/prv/otserv/"; do
    rsync -zvur --include="*.cpp" --include="*.h" --exclude="*" "/home/jskalski/p/prv/otserv/" "/mnt/devserver/home/js/otserv/"
done

Limit number of connections per IP

Limit number of incoming concurrent connections per IP to 1.

On all ports and IPs of server:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
iptables -t filter -I INPUT -p tcp -j ACCEPT
iptables -t filter -I INPUT -p tcp -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -t filter -I INPUT -p tcp --syn -m connlimit --connlimit-above 1 --connlimit-mask 32 -j DROP
iptables -t filter -I INPUT -p tcp -j ACCEPT iptables -t filter -I INPUT -p tcp -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -t filter -I INPUT -p tcp --syn -m connlimit --connlimit-above 1 --connlimit-mask 32 -j DROP
iptables -t filter -I INPUT -p tcp -j ACCEPT
iptables -t filter -I INPUT -p tcp -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -t filter -I INPUT -p tcp --syn -m connlimit --connlimit-above 1 --connlimit-mask 32 -j DROP

On port 80 of server:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
iptables -t filter -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -t filter -I INPUT -p tcp --dport 80 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -t filter -I INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 1 --connlimit-mask 32 -j DROP
iptables -t filter -I INPUT -p tcp --dport 80 -j ACCEPT iptables -t filter -I INPUT -p tcp --dport 80 -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -t filter -I INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 1 --connlimit-mask 32 -j DROP
iptables -t filter -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -t filter -I INPUT -p tcp --dport 80 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -t filter -I INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 1 --connlimit-mask 32 -j DROP

On port 80 and IP 91.134.189.246 of server (in case when server has more IPs):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
iptables -t filter -I INPUT -p tcp -d 91.134.189.246 --dport 80 -j ACCEPT
iptables -t filter -I INPUT -p tcp -d 91.134.189.246 --dport 80 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -t filter -I INPUT -p tcp --syn -d 91.134.189.246 --dport 80 -m connlimit --connlimit-above 1 --connlimit-mask 32 -j DROP
iptables -t filter -I INPUT -p tcp -d 91.134.189.246 --dport 80 -j ACCEPT iptables -t filter -I INPUT -p tcp -d 91.134.189.246 --dport 80 -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -t filter -I INPUT -p tcp --syn -d 91.134.189.246 --dport 80 -m connlimit --connlimit-above 1 --connlimit-mask 32 -j DROP
iptables -t filter -I INPUT -p tcp -d 91.134.189.246 --dport 80 -j ACCEPT
iptables -t filter -I INPUT -p tcp -d 91.134.189.246 --dport 80 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -t filter -I INPUT -p tcp --syn -d 91.134.189.246 --dport 80 -m connlimit --connlimit-above 1 --connlimit-mask 32 -j DROP

Find newest files in directory (recursively)

List files from oldest to newest, recursively.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
find . -type f -print0 | xargs -0 stat --format '%Y :%y %n' | sort -n | awk '{print strftime("%Y-%m-%d %H:%M:%S", $1), $5}' | tail --lines=50
find . -type f -print0 | xargs -0 stat --format '%Y :%y %n' | sort -n | awk '{print strftime("%Y-%m-%d %H:%M:%S", $1), $5}' | tail --lines=50
find . -type f -print0 | xargs -0 stat --format '%Y :%y %n' | sort -n |  awk '{print strftime("%Y-%m-%d %H:%M:%S", $1), $5}' | tail --lines=50

Result:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
(...)
2019-04-19 12:21:31 ./klientotc/modules/gamelib/ui/uiminimap.lua
2019-04-19 15:04:22 ./klientotc/modules/game_hotkeys/hotkeys_manager.otui
2019-04-19 15:11:36 ./klientotc/modules/game_hotkeys/hotkeys_manager.lua
2019-04-19 16:18:34 ./klientotc/.git/index
2019-04-19 16:18:34 ./klientotc/modules/corelib/ui/uiminiwindowcontainer.lua
2019-04-19 16:33:01 ./klientotc/modules/corelib/ui/uiminiwindow.lua
2019-04-19 17:15:50 ./klientotc.zip
2019-04-23 07:35:34 ./klientotc/Kasteria.log
2019-04-25 07:25:25 ./klientotc/.idea/workspace.xml
2019-04-26 10:26:27 ./source/.idea/workspace.xml
(...) 2019-04-19 12:21:31 ./klientotc/modules/gamelib/ui/uiminimap.lua 2019-04-19 15:04:22 ./klientotc/modules/game_hotkeys/hotkeys_manager.otui 2019-04-19 15:11:36 ./klientotc/modules/game_hotkeys/hotkeys_manager.lua 2019-04-19 16:18:34 ./klientotc/.git/index 2019-04-19 16:18:34 ./klientotc/modules/corelib/ui/uiminiwindowcontainer.lua 2019-04-19 16:33:01 ./klientotc/modules/corelib/ui/uiminiwindow.lua 2019-04-19 17:15:50 ./klientotc.zip 2019-04-23 07:35:34 ./klientotc/Kasteria.log 2019-04-25 07:25:25 ./klientotc/.idea/workspace.xml 2019-04-26 10:26:27 ./source/.idea/workspace.xml
(...)
2019-04-19 12:21:31 ./klientotc/modules/gamelib/ui/uiminimap.lua
2019-04-19 15:04:22 ./klientotc/modules/game_hotkeys/hotkeys_manager.otui
2019-04-19 15:11:36 ./klientotc/modules/game_hotkeys/hotkeys_manager.lua
2019-04-19 16:18:34 ./klientotc/.git/index
2019-04-19 16:18:34 ./klientotc/modules/corelib/ui/uiminiwindowcontainer.lua
2019-04-19 16:33:01 ./klientotc/modules/corelib/ui/uiminiwindow.lua
2019-04-19 17:15:50 ./klientotc.zip
2019-04-23 07:35:34 ./klientotc/Kasteria.log
2019-04-25 07:25:25 ./klientotc/.idea/workspace.xml
2019-04-26 10:26:27 ./source/.idea/workspace.xml

TEST

TEST

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
error_reporting(E_ALL ^ E_NOTICE);
session_start();
header("Content-Type: text/html; charset=utf-8");
require_once('panel/config/config.inc.php');
require_once('panel/config/func.inc.php');
if (FORCE_DOMAIN && $_SERVER['HTTP_HOST'] != FORCE_DOMAIN) {
$protocol = (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') ? 'http' : 'https';
header("HTTP/1.1 301 Moved Permanently");
header("Location: " . $protocol . "://" . FORCE_DOMAIN . "/" . $_SERVER['REQUEST_URI']);
header("Connection: close");
exit();
}
if (FORCE_HTTPS && (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on')) {
$currentDomain = $_SERVER['HTTP_HOST'];
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://" . $currentDomain . "/" . $_SERVER['REQUEST_URI']);
header("Connection: close");
exit();
}
<?php error_reporting(E_ALL ^ E_NOTICE); session_start(); header("Content-Type: text/html; charset=utf-8"); require_once('panel/config/config.inc.php'); require_once('panel/config/func.inc.php'); if (FORCE_DOMAIN && $_SERVER['HTTP_HOST'] != FORCE_DOMAIN) { $protocol = (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') ? 'http' : 'https'; header("HTTP/1.1 301 Moved Permanently"); header("Location: " . $protocol . "://" . FORCE_DOMAIN . "/" . $_SERVER['REQUEST_URI']); header("Connection: close"); exit(); } if (FORCE_HTTPS && (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on')) { $currentDomain = $_SERVER['HTTP_HOST']; header("HTTP/1.1 301 Moved Permanently"); header("Location: https://" . $currentDomain . "/" . $_SERVER['REQUEST_URI']); header("Connection: close"); exit(); }
<?php
  error_reporting(E_ALL ^ E_NOTICE);
  session_start();

  header("Content-Type: text/html; charset=utf-8");
  require_once('panel/config/config.inc.php');
  require_once('panel/config/func.inc.php');

    if (FORCE_DOMAIN && $_SERVER['HTTP_HOST'] != FORCE_DOMAIN) {
        $protocol = (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') ? 'http' : 'https';
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: " . $protocol . "://" . FORCE_DOMAIN . "/" . $_SERVER['REQUEST_URI']);
        header("Connection: close");
        exit();
    }

    if (FORCE_HTTPS && (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on')) {
        $currentDomain = $_SERVER['HTTP_HOST'];
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: https://" . $currentDomain . "/" . $_SERVER['REQUEST_URI']);
        header("Connection: close");
        exit();
    }