Backup mongo indexes

There is really short and briliant script for create backup of indexes queries. This code iterate over all collections and create backup of createIndex() queries.

print(`// Backup indexes of : ${db.getName()} : database`);
print(`use ${db.getName()};`);

db.getCollectionNames().forEach(function (collection) {
    indexes = db.getCollection(collection).getIndexes().forEach(function (index) {
        if (index.name === '_id_') return; // skip defalut _id indexes
        const keys = tojsononeline(index.key);
        delete index.id; delete index.key; delete index.v; delete index.ns;
        print(`db.${collection}.createIndex(${keys}, ${tojsononeline(index)});`);
    });
});

You can save this backup code to file and run int directly with mongoshell command:

mongo --quiet mongodb://localhost:27017/mydb ./backup.indexes.js > myindexes.js

Example output:

db.users.createIndex({"settings" : 1}, {"name" : "settingsIndex", "background" : true});
db.users.createIndex({"name" : 1}, {"name" : "nameIndex", "background" : true});
db.users.createIndex({"email" : 1}, {"name" : "emailIndex", "background" : true});

#mongo#2020

Install mongo on macOS

First you need tap official mongo brew:

brew tap mongodb/brew

Then just install mongo with:

 brew install mongodb-community

There is one tool which people often want to install on its own, and that’s the MongoDB shell.

brew install mongodb-community-shell

You can also install only database tools for managing mongo database:

brew install mongodb-database-tools

Starting mongo

brew services start mongodb-community

or stop

brew services stop mongodb-community

#brew#macOS#mongo#2020

Usefull traits

Get class_basename trait

trait ClassBasename {
  public static function class_basename() {
        return substr(strrchr('\\' . static::class, '\\'), 1);
  }
}

This trait returns class basename. If you have e.g. class something\\class\\path\\MyClass it will returns only MyClass.

Get class_namespace trait

trait ClassNamespace {
  public static function class_namespace() {
        return trim(substr(static::class, 0, strrpos('\\' . static::class, '\\')), '\\');
  }
}

This trait returns class namespace. If you have e.g. class something\\class\\path\\MyClass it will returns only something\\class\\path.

#PHP#2020

Convert video for iMovie with ffmpeg

You need to include the argument -pix_fmt yuv420p to generate H.264 content for Apple software/devices, and a bunch of other decoders that don't handle yuv444p.

ffmpeg -i input.avi -pix_fmt yuv420p output.mp4

Apple added ProRes support sometime in late 2014 since iMovie 10 you can use follow code:

ffmpeg -i input.avi -c:v prores -c:a pcm_s16le output.mov

Formats compatible with macOS Catalina:

Video formats:

  • Apple Animation Codec
  • Apple Intermediate Codec
  • Apple ProRes
  • AVCHD (including AVCCAM, AVCHD Lite, and NXCAM)
  • DV (including DVCAM, DVCPRO, and DVCPRO50)
  • H.264
  • HDV
  • HEVC
  • iFrame
  • Motion JPEG (OpenDML only)
  • MPEG-4 SP
  • Photo JPEG
  • XAVC-S

Container formats

  • 3GP
  • AVI
  • M4V
  • MOV (QuickTime)
  • MP4

Audio formats

  • AAC
  • AIFF
  • BWF
  • CAF
  • MP3
  • MP4
  • RF64
  • WAV

More information about incompatible media in iMovie for macOS

#bash#ffmpeg#2020

Add copy to clipboard button to all preformat code in pure Javascript

document.onreadystatechange = function () {
  if (document.readyState == 'complete') {

    // for all pre > code
    document.querySelectorAll('pre > code').forEach(function (codeBlock) {

      // create anchor element
      let copy = document.createElement('a');
      copy.className = 'btn-copy';
      copy.innerText = 'Copy';
      copy.title = 'Copy to Clipboard!';
      copy.href = '#';

      // add click event
      copy.addEventListener('click', function (e) {
        copy.innerText = 'Copied!';

        // copy innerText of preformat
        navigator.clipboard.writeText(codeBlock.innerText).then(function () {
          setTimeout(function () {
            copy.innerText = 'Copy';
          }, 500);
        }, function (err) {
          copy.innerText = 'ERROR';
        });

        // do nothing else...
        e.preventDefault();
        e.stopPropagation();

      }, false);

      // insert before codeBlock <pre><a><code>
      codeBlock.parentNode.insertBefore(copy, codeBlock);
    });
  }
};
a.btn-copy,
a.btn-copy:hover {
  color: black;
  user-select: none;
  position: absolute;
  padding: 8px;
  font-size: smaller;
  right: 0;
  top: 0;
}

As with many new APIs, navigator.clipboard is only supported for pages served over HTTPS. To help prevent abuse, clipboard access is only allowed when a page is the active tab. Pages in active tabs can write to the clipboard without requesting permission, but reading from the clipboard always requires permission.

https://developers.google.com/web/updates/2018/03/clipboardapi

#javascript#2020

Docker cleanup guide

Docker doesn't remove unused objects such as containers, images, volumes, and networks unless you explicitly tell it to do so. How To Remove them?

Removing All Unused Objects

Remove stopped containers, all dangling images, and all unused networks:

docker system prune
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - all dangling build cache

Are you sure you want to continue? [y/N] 

You can also include all unused volumes, then add --volumes:

docker system prune --volumes

Docker Containers

docker container ls -a

Remove selected container

docker container rm [CONTAINER ID]

Remove all stopped containers

To remove all stopped containers use:

docker container prune

You can also list what will be removed:

docker container ls -a --filter status=exited --filter status=created 

Stop and remove all containers

docker container stop $(docker container ls -aq)
docker container rm $(docker container ls -aq)

Docker Images

You can list them:

docker image ls

Remove selected image

docker image rm [IMAGE ID]

Remove dangling images

A dangling image is an image that is not tagged and is not used by any container. You can remove them by:

docker image prune

Remove all unused images

To remove all images which are not referenced by any existing container, not just the dangling ones, use the prune command with the -a flag:

docker image prune -a

Docker Networks

You can list them with:

docker network ls

Remove selected network

docker network rm [NETWORK ID]

Remove all unused networks

Use the docker network prune command to remove all unused networks. Remove all networks that are created more than 12 hours ago:

docker network prune -a --filter "until=12h"

Docker Volumes

docker volume ls

Remove selected Volume

docker volume rm [VOLUME NAME]

Remove all unused volumes

docker volume prune

#Docker#2020