Crmgo Blog

bash

← Blog / Programming

bash

Posts: 22

Deleting files older than X days

Deleting files older than X days

· bash

find /home/ -type f -mtime +7 -exec rm '{}' \; delete files smaller than 5KB find ./ -size -5k -delete delete files larger than 10MB find ./ -size +10m -delete

if...then...else... syntax

if...then...else... syntax

· bash

? if [ condition ]; then if the condition is true, execute all commands until else or fi else if the condition is not true, execute all commands until fi fi It is very important to remember that your construct must always end with the word fi! Expressions...

Copying yesterday's backup in 4 threads

Copying yesterday's backup in 4 threads

· bash

#!/bin/bash # Script for automating file downloads. LogFile=/var/log/backup_db.log NameDay=$((10#`date +%d`-1)) if [ ${#NameDay}=1 ]; then NameDay=0$NameDay fi temp_name=$(date +%m.%Y) NameDate=$NameDay.$temp_name FulName=$NameDate/BACKUP_SQL-$NameDate'_21.00.7z' #echo $FulName echo $(date) - start copy >> $LogFile aria2c "ftp://lomix.com.ua/$FulName" \ --ftp-user=ftpbackup --ftp-passwd=XXXXX \ --dir='/backup/db' --log='/var/log/aria.log' \ --max-concurrent-downloads=4…

Mounting a network drive

Mounting a network drive

· bash

#!/bin/sh # Mount a network drive from the seo-studio server sudo apt-get install smbfs mkdir /media/sharedisk cp "/etc/fstab" "/etc/fstab.default" echo "//192.168.4.224/sharedisk /media/sharedisk cifs credentials=/root/.smbcredentials,iocharset=utf8,file_mode=0777,dir_mode=0777,_netdev 0 0" >> /etc/fstab echo "username=sharedisk" > /root/.smbcredentials echo "password=xxxxxxx" >> /root/.smbcredentials cd /etc/rc0.d sudo mv S31umountnfs.sh S19umountnfs.sh

copying files to FTP

copying files to FTP

· bash

Server=192.168.xxx.xxx Username=clientftp Password=xxxxxx Directory=test LDirectory="/test" Filename="index.php" ftp -n $Server <<End-Of-Session user $Username $Password binary bell cd $Directory lcd $LDirectory put $Filename bye End-Of-Session # If you need to copy multiple files, you can use the mput *.php template

Bash script for creating a network drive

Bash script for creating a network drive

· bash

#!/bin/sh # Uncomment for your system to install the samba component #sudo apt-get install smbfs #(for Ubuntu) #sudo zypper install smbfs # (for openSUSE) #sudo yum install smbfs # (for CentOS) mkdir /media/sharedisk cp "/etc/fstab" "/etc/fstab.default" echo "//192.168.4.224/sharedisk /media/sharedisk cifs credentials=/root/.smbcredentials,iocharset=utf8,file_mode=0777,dir_mode=0777,_netdev 0…

How to Create Multiple Network Interfaces in CentOS

How to Create Multiple Network Interfaces in CentOS

· bash

#!bin/bash i=0 diapazon=19 while [ $i -lt 255 ] do i=$[$i+1] K_file2="ifcfg-eth1:"$diapazon$i echo 'DEVICE=eth1:'$diapazon$i>$K_file2 echo 'IPADDR=192.168.'$diapazon'.'$i>>$K_file2 echo 'HWADDR=xx:xx:xx:xx:xx:xx'>>$K_file2 echo 'ONBOOT=yes'>>$K_file2 echo $K_file ' created' done

MySQL replication monitoring script

MySQL replication monitoring script

· bash

#!/bin/bash # replication monitoring script # and sending me an email in case of an issue # Script written by Dmitry Lomaka, Kyiv 2012 # required: yum install mailx # set up the master server's crontab to run once per hour # crontab -e ## 00 *…

Examples of If Then Else

Examples of If Then Else

· bash

Nested if Statements Inside an if statement, you can use another if statement. You can use as many levels of nesting as you can handle. In this example, leap years are checked:

Loops.

Loops.

· bash

The for-in Loop The for-in statement is designed to sequentially access values listed in a list. Each value in the list is assigned to a variable in turn. The syntax is as follows: Let's consider a small example: After executing the example, a line with its number will appear in the first 5 virtual consoles (terminals). In...