Export docker volume from one host to another

usage: ./script.sh sentry-postgres
#!/bin/bash

# Environment check

SOURCE_HOST_ADDRESS=localhost
SOURCE_HOST_USER=$USER
TARGET_HOST_ADDRESS=192.168.126.168
TARGET_HOST_USER=targetuser

# If migrating from localhost, set this to localhost
if [[ ! $SOURCE_HOST_ADDRESS ]]
then
  echo "Please set the current host address in SOURCE_HOST_ADDRESS"
  exit 1
fi

# If migrating from localhost, set this to $USER
if [[ ! $SOURCE_HOST_USER ]]
then
  echo "Please set the current host user in SOURCE_HOST"
  exit 1
fi

# If migrating to localhost, set this to localhost
if [[ ! $TARGET_HOST_ADDRESS ]]
then
  echo "Please set the new host address in TARGET_HOST_ADDRESS"
  exit 1
fi

# If migrating to localhost, set this to $USER
if [[ ! $TARGET_HOST_USER ]]
then
  echo "Please set the new host user in TARGET_HOST_USER"
  exit 1
fi

# Argument check

if [[ ! $1 ]]
then
  echo "Please supply a docker volume name, as displayed by the command 'docker volume ls'"
  exit 1
fi
volume_name=$1

# Export the volume from the current instance

echo "Exporting volume $volume_name on $SOURCE_HOST_ADDRESS"
ssh "$SOURCE_HOST_USER"@"$SOURCE_HOST_ADDRESS" "\
mkdir -p \$HOME/docker-volume-backup
docker run \
  --rm \
  -v $volume_name:/volume-backup-source \
  -v \$HOME/docker-volume-backup:/volume-backup-target \
  busybox \
  sh -c 'cd /volume-backup-source && tar cf /volume-backup-target/backup.tar .' \
"


# Transfer the exported volume to the new address

echo "Transferring exported volume $volume_name from $SOURCE_HOST_ADDRESS to $TARGET_HOST_ADDRESS"
ssh "$TARGET_HOST_USER"@"$TARGET_HOST_ADDRESS" "mkdir -p \$HOME/docker-volume-backup"
scp -3 "$SOURCE_HOST_USER"@"$SOURCE_HOST_ADDRESS":./docker-volume-backup/backup.tar \
  "$TARGET_HOST_USER"@"$TARGET_HOST_ADDRESS":./docker-volume-backup/backup.tar


# Restore the backup

echo "Creating volume $volume_name on $TARGET_HOST_ADDRESS"
ssh "$TARGET_HOST_USER"@"$TARGET_HOST_ADDRESS" "\
docker volume create $volume_name \
&& docker run \
  --rm \
  -v $volume_name:/volume-backup-target \
  -v \$HOME/docker-volume-backup/:/volume-backup-source \
  busybox \
  sh -c 'cd /volume-backup-target && tar xf /volume-backup-source/backup.tar .' \
"

# Clean up residual files

echo "Cleaning up unnecessary files"
ssh "$SOURCE_HOST_USER"@"$SOURCE_HOST_ADDRESS" "rm -rf \$HOME/docker-volume-backup"
ssh "$TARGET_HOST_USER"@"$TARGET_HOST_ADDRESS" "rm -rf \$HOME/docker-volume-backup"

echo "Successfully migrated docker volume $volume_name from $SOURCE_HOST_ADDRESS to $TARGET_HOST_ADDRESS"
29 октября 2024, 11:04

Add proxy to ssh connection

~/.ssh/config
Host address.com
    Hostname address.com
    ServerAliveInterval 55
    ForwardAgent yes
    ProxyCommand nc -x 127.0.0.1:9999 %h %p
26 октября 2024, 23:34

Worpress in docker: wrong file permission

In wp-config.pgp
define('FS_CHMOD_DIR', 0777);
8 августа 2024, 14:01

Postgres show enum values

select enum_range(null::your_type_name);
26 июля 2024, 16:26

Install postgres on ubuntu

sudo apt install postgresql postgresql-contrib
su postgres
psql
ALTER USER postgres PASSWORD '***';
5 июля 2024, 11:06

Gitlab crear docker cache

/usr/share/gitlab-runner/clear-docker-cache
2 июля 2024, 13:55

Install docker on ubuntu 24

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
23 мая 2024, 13:35

Get first 10 row and total rows from table

SELECT stuff,
       count(*) OVER() AS total_count
FROM table
WHERE ....
ORDER BY ... OFFSET 0 LIMIT 10
17 мая 2024, 22:34

docker.errors.DockerException: Error while fetching server API version: HTTPConnection.request() got an unexpected keyword argument 'chunked'

If docker-compose doesn't work use
docker compose up -d
17 мая 2024, 22:06

Knexjs upsert with update

import Knex from "knex";

const knex = Knex({
  client: 'pg',
  connection: process.env.DATABASE_URL,
});

const records = [
  { id: 2, login: "admin" },
];

async function upsert() {
  const result = await knex.raw(
      `? ON CONFLICT (id)
              DO UPDATE SET
              login = EXCLUDED.login,
              updated_at = CURRENT_TIMESTAMP
            RETURNING *;`,
      [knex("users").insert(records)],
    );
}
27 марта 2024, 09:55

Kill process by pattern

pkill -9 -f pattern
24 марта 2024, 10:31

PhpStorm licence key

???
5 марта 2024, 14:58

Fix postgres sequence after insert

Run the output of this sql command
SELECT 
    'SELECT SETVAL(' ||
       quote_literal(quote_ident(sequence_namespace.nspname) || '.' || quote_ident(class_sequence.relname)) ||
       ', COALESCE(MAX(' ||quote_ident(pg_attribute.attname)|| '), 1) ) FROM ' ||
       quote_ident(table_namespace.nspname)|| '.'||quote_ident(class_table.relname)|| ';'
FROM pg_depend 
    INNER JOIN pg_class AS class_sequence
        ON class_sequence.oid = pg_depend.objid 
            AND class_sequence.relkind = 'S'
    INNER JOIN pg_class AS class_table
        ON class_table.oid = pg_depend.refobjid
    INNER JOIN pg_attribute 
        ON pg_attribute.attrelid = class_table.oid
            AND pg_depend.refobjsubid = pg_attribute.attnum
    INNER JOIN pg_namespace as table_namespace
        ON table_namespace.oid = class_table.relnamespace
    INNER JOIN pg_namespace AS sequence_namespace
        ON sequence_namespace.oid = class_sequence.relnamespace
ORDER BY sequence_namespace.nspname, class_sequence.relname;
Alternatives List of all sequence
SELECT c.relname FROM pg_class c WHERE c.relkind = 'S' order BY c.relname;
Fix sequence (if it's named id)
SELECT 
concat('SELECT SETVAL(''public.', c.relname,''', COALESCE(MAX(id), 1) ) FROM public.', replace(c.relname, '_id_seq', ''), ';') FROM pg_class c WHERE c.relkind = 'S' order BY c.relname;
4 марта 2024, 17:26

Install nodejs 20 on ubuntu

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
9 февраля 2024, 12:52

Copy files from/to docker containers

docker cp <source_path>  <container_id>:<destination_path>
docker cp <container_id>:<source_path> <destination_path>
8 февраля 2024, 21:23

Generate random string with openssl

 openssl rand -base64 32
5 февраля 2024, 20:29

Proxy and cache external resource with nginx

proxy_cache_path /home/nginx levels=1:2 keys_zone=CACHE:50m max_size=12G inactive=30d;

server {
	server_name example.com;

	location / {
		proxy_cache_key "$uri";
		proxy_pass https://$path$is_args$args;
		resolver 8.8.8.8;
		proxy_cache            CACHE;
		access_log      off;
		expires max;
		add_header X-Proxy-Cache $upstream_cache_status;
		proxy_set_header Referer "";
		proxy_cache_valid 200 30d;
		proxy_cache_valid 403 10m;
		proxy_cache_valid 404 10m;
		
		proxy_ssl_server_name on;
                proxy_ssl_name $proxy_host;
	}

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
15 декабря 2023, 12:59

Create .htpasswd file

sudo sh -c "echo -n 'sammy:' >> /etc/nginx/.htpasswd"
sudo sh -c "openssl passwd -apr1 >> /etc/nginx/.htpasswd"
20 июня 2023, 01:02

List all services on ubuntu

systemctl list-units --type service
4 апреля 2023, 08:42

Docker install on ubuntu 20.04

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get --assume-yes install docker-ce docker-ce-cli containerd.io
16 января 2023, 11:15