Linux Shell Scripting summary of common

I. Introduction
This paper will summarize some of the common shell script used to facilitate future work.

Two, shell scripts
[a] regular backup mysql database, to be combined with cronb timed task scheduling implementation.

#! / bin / the bash
# outset some custom variables

# database backup path
DB_BAK_PATH = / Data / DB
# current time
CURRENTTIME = $ (DATE +% the Y% m% D_% H% M% S)
# backup database name
DB_NAME test_db_bak =
# username
db_username = root
# password
DB_PASSWORD = 0905
# host name
DB_HOSTNAME = localhost

echo "======= start [backup database backup path: $ DB_BAK_PATH / $ CURRENTTIME.tar.gz] ===== === "


# create a path for backup, if the directory does not exist, create a backup directory
IF [-d!" "; the then $ DB_BAK_PATH / $ CURRENTTIME]
echo" backup directory does not exist, ready to create the backup directory "
# create a backup directory
mkdir -p "$ DB_BAK_PATH / $ CURRENTTIME"
the else
echo "backup directory already exists"
fi

# another relatively simple wording (in front of the command is true, just behind the execution && command)
# [! -d "$ DB_BAK_PATH / $ CURRENTTIME "] &&mkdir -p "$DB_BAK_PATH/$CURRENTTIME"

# Using mysqldump backup mysql database, and gzip compression
mysqldump -u db_username $ {}} -p --host = $ {$ DB_HOSTNAME DB_PASSWORD the DB_NAME $ | gzip> $ DB_BAK_PATH / CURRENTTIME $ / $ CURRENTTIME.sql.gz

#tar compression
$ DB_BAK_PATH cd
tar -zcvf $ CURRENTTIME.tar.gz $ CURRENTTIME

# delete temporary directory
RM -rf $ DB_BAK_PATH / $ CURRENTTIME

# delete month before the database backup file
find $ DB_BAK_PATH -mtime +30 -name "* .tar.gz "-exec RM -rf {} \;

# database backup is complete
echo" ======== database backup success [backup path: $ DB_BAK_PATH / $ CURRENTTIME.tar.gz] ========= == "
[b] batch create Linux users, and belong to the same user group wsh, it requires a consistent user name and password.

! # / bin / bash
# batch create Linux users (user1 - user5), and belong to the same user group wsh, it requires a user name and password are the same

for the I in $ (seq 1 5)
do
# useradd-user user name -g group name
useradd the I WSH $ -g the User
# Change password
echo the User the I $ | $ passwd the User --stdin the I
DONE


[C] the files larger than 10k under / test directory deletion (commonly used for garbage collection)

! # / bin / bash
# files larger than 10k in the / test is deleted
for in $ tmp (LS / test)
do
IF [-f $ tmp]; the then
IF [$ ($ tmp LS the -l | awk '{Print . 5} $ ') 10000 -gt]; the then
# delete the file
RM -rf $ tmp
Fi
Fi
DONE


Time [d] N days Clear batch files (task scheduling the timing needed to achieve binding cronb)

! # / bin / the bash

# periodically cleaned suffix in / data / directory 30 days before a .txt text:! wq i member
find / data / -mtime +30 -name " * .txt" -exec rm -rf } {\;


# / the Data: ready to clean up the directory; 
# -mtime: +30 standard statement written: Find files older than 30 days;
#. "TXT" means the search for all files with the extension .txt;
# -exec : fixed wording;
#rm -rf: force delete files, including directories;
# {} \: find the results into the inside;
 

 

 
---------------------

Guess you like

Origin www.cnblogs.com/hyhy904/p/11089236.html