Skip to content

Commit

Permalink
grep, swap, mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsiryk committed Aug 2, 2019
1 parent 94258e9 commit 4acd59d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 13 deletions.
23 changes: 12 additions & 11 deletions SWAP.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Linux SWAP recommended size
---------------------------

.. note::

It's not a rule. It's only recommendation for general configurations.

===== ====
Expand All @@ -24,17 +24,19 @@ SWAP in file

sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo mkswap /swapfile


2. Disable all SWAP::
2. Disable SWAP::

sudo swapoff -a
sudo swapoff /swapfile
# or disable all swap
sudo swapoff -a


3. Connect new SWAP file::

sudo swapon /swapfile
sudo swapon /swapfile


4. Add new SWAP to fstab (auto mount when system start)::
Expand All @@ -46,15 +48,15 @@ SWAP in file

sudo sysctl -w vm.swappiness=30



Automated swapfile creation
^^^^^^^^^^^^^^^^^^^^^^^^^^^

::

#!/bin/bash

set -e

# SWAP file size in MB
Expand All @@ -71,8 +73,8 @@ Automated swapfile creation
sudo chmod 600 $SWAP_PATH
sudo mkswap $SWAP_PATH

# Disable all SWAP
sudo swapoff -a
# Disable SWAP
sudo swapoff /swapfile

# Connect new SWAP file
sudo swapon $SWAP_PATH
Expand All @@ -82,5 +84,4 @@ Automated swapfile creation

# Tweak system swap usage value
sudo sysctl -w vm.swappiness=$VM_SWAPPINESS



4 changes: 2 additions & 2 deletions linux_commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ grep::


Word replacement::

grep -iRl -E "(SOURCE)" | xargs sed -i "s/SOURCE/DEST/g"
grep -Rl -E "(SOURCE)" ./* | xargs sed -i "s/SOURCE/DEST/g"

sed -i "s@SOURCE@DEST@g" /path/to/file

Expand Down
67 changes: 67 additions & 0 deletions mongodb.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
=======
MongoDB
=======


Execute JS code::

mongo [db_name] --quiet --eval "<JS_code>"


Print a list of all databases on the server::

show dbs
# or
db.getMongo().getDBNames()
# or
printjson(db.adminCommand('listDatabases'))


Switch current database to <db>. The mongo shell variable db is set to the current database::

use <db>


Backup/restore MongoDB data (docker)::

# dump
docker exec <container> sh -c 'mongodump --archive' > mongodb.dump

# restore
docker exec -i <container> sh -c 'mongorestore --archive' < mongodb.dump


Print a list of all collections for current database::

show collections
# or
db.getCollectionNames()


Print documents of collection::

db.<collection>.find().pretty()


Get all documents of all collections of current DB::

var c = db.getCollectionNames();
for (var i = 0; i < c.length; i++) {
print('[' + c[i] + ']');
db.getCollection(c[i]).find().forEach(printjson);
}


Get DBs, collections and docs count::

db.getMongo().getDBNames().forEach((d) => {
print('[' + d + ']');
db = db.getSiblingDB(d);
db.getCollectionNames().forEach((c) => {
print('\t' + c + ': ' + db.getCollection(c).count());
});
});

# single line
db.getMongo().getDBNames().forEach((d)=>{print('['+d+']');db=db.getSiblingDB(d);db.getCollectionNames().forEach((c)=>{print('\t'+c+': '+db.getCollection(c).count())})})

0 comments on commit 4acd59d

Please sign in to comment.