Linux Tips & Tricks

A list for me to remember how to do stuff

mysqldump over ssh

Backing up mysql/mariadb over ssh with multithreaded compression - note that this requires pbzip2 for multithreaded performance - highly recommended. Alternatively you can replace pbzip2 with gzip -9 (or a lower number for less cpu usage)

mysqldump -uUSERNAME -p DATABASE | pbzip2 | ssh USERNAME@EXAMPLE.COM 'cat > /path/to/DATABASE.sql.gz'

Local restore of pbzip2 would be:

pbzip2 -cdk DATABASE.sql.gz | mysql -u root -p DATABASE

Save color output of ansible-playbook run

unbuffer ansible-playbook -i hostsfile -l limitgroup fancyplaybook.yml > out.txt

Tar and del files individually

ls | while read filename; do tar -czvf "$filename".tar.gz "$filename" && rm "$filename"; done

dd to device without buffered write

dd if=image.img of=/dev/sdb bs=4M status=progress iflag=nocache oflag=nocache,dsync

Setup a reverse ssh automatic connection -

SSHPORT=port the remote host will use on localhost
sleep 100 
while true
        /usr/bin/autossh -M 10994 -N -R $SSHPORT:localhost:22 $SSHUSER@$SSHHOST -o "ServerAliveInterval 45" -o "ServerAliveCountMax 2"
        sleep 5


* Set script to executable
* Make sure the autossh package is installed
* ssh-copy-id to the target host (run ssh-keygen first if needed)
* _Test the connection with the appropriate user_
* Set the script to autostart, for example in /etc/crontab like: @reboot username /home/username/ 

Stress a 4 core cpu (like an RPi)

for i in 1 2 3 4 ; do nice -n 20 openssl speed >/dev/null 2>&1 & done 

Measure cpu temperature of a Raspberry Pi

vcgencmd measure_temp