Home

My name is Ahmed Khaled a computer hobbyist and a DevOps Engineer. This is my resources list that I collect regulary to help me in my daily work and personal hobbies and projects.

For my blog visit: akhal3d.github.io

SSH

SSH Keys

Create a passwordless key pair

ssh-keygen -t ed25519 -C "email@example.com" -f ./path/to/key/id_ed25519 -N ""

Tunneling

Local port forwarding

It makes remote server appears as local on a certain local port. You use as many ports as you can by using multiple -L with every port.

ssh -N -L local_port:127.0.0.1:remote_port user@host

Remote port forwarding

Sometimes called reverse tunneling. It redirects incoming traffic from the server to the client using the -R flag.

ssh -N -R remote_port:127.0.0.1:local_port user@host

Dynamic port forwarding

Creates a SOCKS5 proxy using the -D flag.

ssh -N -D 127.0.0.1:local_port user@<remote_server>

Optional flags

FlagUsage
-NDon't create an interactive session
-fRun it in the background

Resources

sed

Delete line containing string

sed -i '/string/d' file.txt

SQL Server

Common commands

List all the databases

SELECT name FROM master.dbo.sysdatabases;

Kubernetes

kubectl run --restart=Never sqltools --image=mcr.microsoft.com/mssql-tools --command sleep 99d

kubectl exec -it sqltools -- /opt/mssql-tools/bin/sqlcmd -S <HOSTNAME> -U <USERNAME> -P <PASSWORD>

Notes:

  1. Add -Q to excute query without running into the interactive console.
  2. -P is optional.

AWS RDS

Create a backup to S3

exec msdb.dbo.rds_backup_database
	@source_db_name='database_name',
	@s3_arn_to_backup_to='arn:aws:s3:::bucket_name/file_name.extension',
	[@kms_master_key_arn='arn:aws:kms:region:account-id:key/key-id'],	
	[@overwrite_s3_backup_file=0|1],
	[@type='DIFFERENTIAL|FULL'],
	[@number_of_files=n];

Check backup task status

exec msdb.dbo.rds_task_status @task_id=<TASK_ID>;

References

  1. Importing and exporting SQL Server databases using native backup and restore

PostgreSQL

You can use PGPASSWORD variable to pass the password to your database.

Backup and Restoration

Backup

pg_dump -h DB_HOST -U DB_USER DB_NAME | gzip -9 > DB_BACKUP_NAME_$(date +%F).sql.gz

Restore

gunzip -c DB_BACKUP_NAME.sql.gz | psql -h DB_HOST -U DB_USER DB_NAME

VIM

Write with sudo

Save a file opened with non-root user

:w !sudo tee %

Vagrant

Create cluster of multiple nodes


Vagrant.configure("2") do |config|
  
  # Specif the number of nodes
  num_of_nodes = 3
  
  (0..(num_of_nodes - 1)).each do |node|

	# Append a number to the node name
    node_name = "vm-#{node + 1}"

    config.vm.define node_name do |node|
		
		# VM Settings such as network and disks

      node.vm.provider "virtualbox" do |vb|
        # Vendor specific settings such as VirtualBox GUI 
      end
    end
  end
end

References

Vagrant Documentation

nc

Copy text between two sessions on the same server

First session:

nc -l -p <PORT>

Second session:

nc 127.0.0.1 <PORT> # Same port in the first session

certbot

In case of custom web server configuration I find DNS challenges more convenient and easier to work with.

certbot certonly --manual --preferred-challenges=dns --email email@example.com  -d subdomain.example.com

Miscellaneous

  • Invert image colors gm convert <input> -negate <output>

  • Copy directory recursively cp -r /source/ /dest/

  • curl basic auth: curl -u username:password https://example.com

  • Trigger sentry cleanup docker exec sentry_onpremise_sentry-cleanup_1 gosu sentry sentry cleanup --days <number of days>

  • Check TLS version of a website openssl s_client -connect example.com:443 -tls1_2

  • Fill memory cat /dev/zero | head -c 3G | tail