Category Archives: Linux

Reduce disk I/O with noatime and nodiratime

Add noatime and nodiratime to the mount options in /etc/fstab: UUID=[…] /webserver ext4 defaults,noexec,nodev,nosuid,noatime,nodiratime 0 2UUID=[…] /webserver ext4 defaults,noexec,nodev,nosuid,noatime,nodiratime 0 2 Remount the modified partitions as follows (use the correct mount point): mount -o remount /webservermount -o remount /webserver You may also apply this to / partition.

Restore MySQL datadir on a new server

You can restore a previously copied\moved MySQL datadir on a new server: 1) Stop the MySQL service service mysql stopservice mysql stop 2) Rename the actual MySQL datadir to something different: mv /var/lib/mysql /var/lib/mysql_originalmv /var/lib/mysql /var/lib/mysql_original 3) Copy the MySQL datadir to restore in the MySQL datadir path: cp -R /backups/mysql /var/libcp -R /backups/mysql /var/lib […]

Backup MySQL datadir without Mysqldump

The fastest way to backup MySQL datadir is: 1) Stop the MySQL service service mysql stopservice mysql stop 2) Copy /var/lib/mysql to another location, i.e: cp -R /var/lib/mysql /root/mysqldatadircp -R /var/lib/mysql /root/mysqldatadir 3) Start the MySQL service service mysql startservice mysql start 4) Compress the new MySQL datadir with tar: tar czf /root/mysqldatadir.tar.gz /root/mysqldatadirtar czf […]

Bash Execute Multiple MySQL Commands from Shell

Here is a Bash script to run multiple MySQL commands: #!/bin/bash   mysql -u root -p’YOUR_MYSQL_PASSWORD_HERE’ dbname << eof ALTER TABLE site DROP INDEX name, ADD UNIQUE KEY email (email); ALTER TABLE site DROP INDEX name_index; alter table site drop column users; alter table site drop column downloads; alter table site drop column invoices; alter […]

Reset (flush) Iptables rules Linux

Bash script to correctly reset (flush) all iptables rules: #!/bin/bash   # Simple script to reset all iptables rules # Without creating issues like with "iptables -F"   IPTABLES="$(which iptables)"   # Set default policies (accept) for all three default chains $IPTABLES -P INPUT ACCEPT $IPTABLES -P FORWARD ACCEPT $IPTABLES -P OUTPUT ACCEPT   # […]

Simple Bash Iptables Firewall Script

Sample firewall script with iptables to allow only trusted traffic: #!/bin/bash   # Simple firewall script to allow only trusted inbound connections # Useful for load balanced servers to allow only inbound traffic on port 80 from the load balancer # Using /etc/crontab you can run the firewall script at every reboot: # @reboot root […]

Bash Get the size of a file

With stat you can get the size of a file: stat –printf="%s" /path/to/filestat –printf="%s" /path/to/file In Bash you can store the file size in a variable: FILESIZE=$(stat -c%s "/path/to/file")FILESIZE=$(stat -c%s "/path/to/file") And you can check if a file is of 0 bytes: if [ "$(stat -c%s "/path/to/file")" -eq 0 ]; then echo "The file is […]

Crontab requires escaping any % signs

Crontab requires escaping any % signs, so this will not work: 0 0 * * * root /root/script.sh "%DATE%"0 0 * * * root /root/script.sh "%DATE%" After escaping any % signs, it will work just fine: 0 0 * * * root /root/script.sh "\%DATE\%"0 0 * * * root /root/script.sh "\%DATE\%"

Bash Check folder size Linux

With du you can check the folder size in human readable format: du -h /path/to/directorydu -h /path/to/directory *** It includes the size of the folder and subfolders *** If you want a simpler output, use this: du -hcs /path/to/directorydu -hcs /path/to/directory