计划任务与lvm管理
创建卷组
[root@YL ~]# vgcreate ly /dev/sd{b,c,d}
Volume group "ly" successfully created
创建逻辑卷
[root@YL ~]# lvcreate -L 10G -n liu ly
WARNING: xfs signature detected on /dev/ly/liu at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/ly/liu.
Logical volume "liu" created.
格式化逻辑卷类型为ext4
[root@YL ~]# mkfs.ext4 /dev/ly/liu
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 2621440 4k blocks and 655360 inodes
Filesystem UUID: 44d1b9cd-8bf3-443c-ab6d-d75e50726ba9
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
创建一个挂载点,查看逻辑卷的uuid
[root@YL ~]# mkdir /gzd
[root@YL ~]# blkid |grep liu
/dev/mapper/ly-liu: UUID="44d1b9cd-8bf3-443c-ab6d-d75e50726ba9" BLOCK_SIZE="4096" TYPE="ext4"
挂载逻辑卷
[root@YL ~]# cat /etc/fstab |tail -2
/dev/mapper/cs-swap none swap defaults 0 0
UUID="44d1b9cd-8bf3-443c-ab6d-d75e50726ba9" /gzd ext4 defaults 0 0
重新加载
[root@YL ~]# mount -a
格式化完成,大小为10G
[root@YL ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─cs-root 253:0 0 17G 0 lvm /
└─cs-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
└─ly-liu 253:2 0 10G 0 lvm /gzd
sdc 8:32 0 20G 0 disk
sdd 8:48 0 20G 0 disk
sde 8:64 0 20G 0 disk
sr0 11:0 1 9G 0 rom /mnt
将逻辑卷扩展到14G
[root@YL ~]# lvextend -L 14G /dev/ly/liu
Size of logical volume ly/liu changed from 10.00 GiB (2560 extents) to 14.00 GiB (3584 extents).
Logical volume ly/liu successfully resized.
ext4格式扩容
[root@YL ~]# resize2fs /dev/ly/liu
resize2fs 1.45.6 (20-Mar-2020)
Filesystem at /dev/ly/liu is mounted on /gzd; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/ly/liu is now 3670016 (4k) blocks long.
重新加载,此时内存已经变成了14G
[root@YL ~]# mount -a
[root@YL ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 371M 0 371M 0% /dev
tmpfs tmpfs 391M 0 391M 0% /dev/shm
tmpfs tmpfs 391M 5.6M 385M 2% /run
tmpfs tmpfs 391M 0 391M 0% /sys/fs/cgroup
/dev/mapper/cs-root xfs 17G 2.1G 15G 13% /
/dev/sda1 xfs 1014M 195M 820M 20% /boot
tmpfs tmpfs 79M 0 79M 0% /run/user/0
/dev/sr0 iso9660 9.0G 9.0G 0 100% /mnt
/dev/mapper/ly-liu ext4 14G 41M 13G 1% /gzd
卸载逻辑 卷的挂载点
[root@YL ~]# umount /dev/ly/liu
检查逻辑卷的文件系统
[root@YL ~]# e2fsck -f /dev/ly/liu
e2fsck 1.45.6 (20-Mar-2020)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/ly/liu: 11/917504 files (0.0% non-contiguous), 84228/3670016 blocks
将逻辑卷大小裁剪到3G
[root@YL ~]# resize2fs /dev/ly/liu 3G
resize2fs 1.45.6 (20-Mar-2020)
Resizing the filesystem on /dev/ly/liu to 786432 (4k) blocks.
The filesystem on /dev/ly/liu is now 786432 (4k) blocks long.
[root@YL ~]# lvreduce -L 3G /dev/ly/liu
WARNING: Reducing active logical volume to 3.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce ly/liu? [y/n]: y
Size of logical volume ly/liu changed from 14.00 GiB (3584 extents) to 3.00 GiB (768 extents).
Logical volume ly/liu successfully resized.
查看文件系统
[root@YL ~]# e2fsck -f /dev/ly/liu
e2fsck 1.45.6 (20-Mar-2020)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/ly/liu: 11/196608 files (0.0% non-contiguous), 34882/786432 blocks
重新加载,此时逻辑卷大小已经变成了3G
[root@YL ~]# mount -a
[root@YL ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 371M 0 371M 0% /dev
tmpfs tmpfs 391M 0 391M 0% /dev/shm
tmpfs tmpfs 391M 5.6M 385M 2% /run
tmpfs tmpfs 391M 0 391M 0% /sys/fs/cgroup
/dev/mapper/cs-root xfs 17G 2.1G 15G 13% /
/dev/sda1 xfs 1014M 195M 820M 20% /boot
tmpfs tmpfs 79M 0 79M 0% /run/user/0
/dev/sr0 iso9660 9.0G 9.0G 0 100% /mnt
/dev/mapper/ly-liu ext4 2.9G 25M 2.8G 1% /gzd
将/dev/sdd从ly卷组移除
[root@YL ~]# vgreduce ly /dev/sdd
Removed "/dev/sdd" from volume group "ly"
同一卷组下进行数据迁移
[root@YL ~]# pvmove /dev/sdb
/dev/sdb: Moved: 12.37%
/dev/sdb: Moved: 100.00%
[root@YL ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 cs lvm2 a-- <19.00g 0
/dev/sdb ly lvm2 a-- <20.00g <20.00g
/dev/sdc ly lvm2 a-- <20.00g <17.00g
/dev/sdd lvm2 --- 20.00g 20.00g
卸载挂载点,删除逻辑卷
[root@YL ~]# umount /dev/ly/liu
[root@YL ~]# lvremove /dev/ly/liu
Do you really want to remove active logical volume ly/liu? [y/n]: y
Logical volume "liu" successfully removed
删除卷组
[root@YL ~]# vgremove /dev/ly
Volume group "ly" successfully removed
删除物理卷
[root@YL ~]# pvremove /dev/sd{b,c,d}
Labels on physical volume "/dev/sdb" successfully wiped.
Labels on physical volume "/dev/sdc" successfully wiped.
Labels on physical volume "/dev/sdd" successfully wiped.
*/01 * * * * /bin/echo "liuyang" >> /root/ly.txt
crontab: installing new crontab
[root@YL ~]# ls
anaconda-ks.cfg ly.txt passwd
[root@YL ~]# cat ly.txt
liuyang
liuyang
liuyang
将ly用户放入/etc/cron.deny让其不能使用crontab
[root@YL ~]# cat /etc/cron.deny
ly
[root@YL ~]# su - ly
[ly@YL ~]$ crontab -e
You (ly) are not allowed to use this program (crontab)
See crontab(1) for more information
每隔两分钟都将162113这个内容放入到1.txt中
*/2 * * * * /bin/echo "162113" >> 1.txt
[ly@YL ~]$ ls
1.txt
作业
在linux系统中备份脚本backup.sh需要再每周1-5的每天下午1点和晚上8点执行,下列哪个cron命令可以完成(D)
a. 00 13,20 * 1-5 * backup.sh 每个月的1到5号的下午一点整和晚上八点整备份脚本backup.sh
b. 0 13,20 1,5 * * backup.sh 每个月的一号和五号的下午一点和八点执行备份脚本backup.sh
c. * 13,20 * * 1-5 backup.sh 每周1-5的每天下午1点和晚上8点执行
d. 00 13,20 * * 1-5 backup.sh 每周一到周五的下午一点整和晚上八点整执行备份脚本backup
新建/scripts/httpd.sh文件,并让/scripts/httpd.sh脚本在每天的00:10分执行
10 00 * * * /root/scripts/httpd.sh
新建/backup目录,每周一下午5:50将/backup目录下的所有文件打包成 backup.tar.gz
50 17 * * 1 [root@YL ~]# tar -zcf /root/backup.tar.gz /backup/*
写一个定时任务,每天0点5分把/var/log/nginx下7天前的文件转移到/backup/2018_xx_xx的目录中
05 00 * * * /usr/bin/find /var/log/ly -mmin +2 -exec -type f /usr/bin/mv {} /backup/2022_07_18/ \;
系统脚本/scripts/which.sh,如何定时每隔7分钟执行一次
*/7 * * * * /scripts/which.sh
如何不小心删除了/var/spool/cron/root文件,该如何恢复。
cat /var/log/cron* | grep -i root | grep "CMD" | awk -F '(''{print $3}' | awk -F ')''{print $1}' | sort -u > crontab_tmp