linux下获取文件的创建时间与实战教程

您所在的位置:网站首页 获取文件的创建时间 linux下获取文件的创建时间与实战教程

linux下获取文件的创建时间与实战教程

#linux下获取文件的创建时间与实战教程| 来源: 网络整理| 查看: 265

背景

有时候我们需要获取文件的创建时间。

例如:

我在研究 《xtrabackup 原理图》的时候,想通过观察确认 xtrabackup_log 是最早创建 并且是 最晚保存的文件。我们就需要知道 xtrabackup_logfile 这个文件的创建时间戳和修改时间戳。

复习: Linux关于文件的三个时间戳

Linux 的文件系统保存有三个时间戳,利用 stat 指令查看文件信息可以获取。他们分别是 ATime、MTime 和 CTime

[root@192-168-199-198 backups]# stat 2.txt File: ‘2.txt' Size: 16 Blocks: 8 IO Block: 4096 regular file Device: 821h/2081d Inode: 15 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2019-07-23 12:12:14.276981038 +0800 Modify: 2019-07-23 12:12:41.415980158 +0800 Change: 2019-07-23 12:12:41.415980158 +0800 Birth: -

ATime ——文件的最近访问时间

只要读取文件,ATime 就会更新,对应的是 stat 命令获取的 Access 的值。

[root@192-168-199-198 backups]# cat 2.txt #=0;i--)) do unset array[$i] SUBPWD=`echo " "${array[@]} | sed 's@ @/@g'` DISK=`df -h |grep ${SUBPWD}$ |awk '{print $1}'` [[ -n $DISK ]] && break done #不是ext4就退出 [[ "`mount |grep ${DISK} |awk '{print $5}'`" != "ext4" ]] && { echo ${DISK} is not mount on type ext4! Only ext4 file system support!;exit 2; } debugfs -R "stat " ${DISK}

使用:

chmod +x statx mv statx /usr/sbin/statx [root@192-168-199-198 backups]# statx 2.txt debugfs 1.42.9 (28-Dec-2013) Inode: 14 Type: regular Mode: 0644 Flags: 0x80000 Generation: 737271740 Version: 0x00000000:00000001 User: 0 Group: 0 Size: 30 File ACL: 0 Directory ACL: 0 Links: 1 Blockcount: 8 Fragment: Address: 0 Number: 0 Size: 0 ctime: 0x5d369644:211c1170 -- Tue Jul 23 13:08:20 2019 atime: 0x5d36bb8f:56eb1e70 -- Tue Jul 23 15:47:27 2019 mtime: 0x5d3691c7:6e5dbb68 -- Tue Jul 23 12:49:11 2019 crtime: 0x5d3691c7:6e5dbb68 -- Tue Jul 23 12:49:11 2019 Size of extra inode fields: 28 EXTENTS: (0):35337

!!!请谨慎在生产环境使用,shell脚本没有做太多的异常处理,不支持管道,不支持目录也没有经过大量的测试

实战 我们回过头来,用这个方法,确认《xtrabackup 原理图》是否准确。

我们需要验证的是:

xtrabackup_log 是最早创建 并且是 最晚保存的文件

1. 创建备份

DATE=`date "+%Y%m%d%H%M%S"` xtrabackup -uroot -proot \ -S /tmp/mysql3306.sock \ --backup \ --target-dir=/backups/$DATE

2. 查找所有备份文件的crtime

cd /backups/$DATE >/tmp/1.txt >/tmp/2.txt find . -type f >/tmp/1.txt for i in `cat /tmp/1.txt` do { echo -n $i" ";statx $i 2>/dev/null |grep crtime |awk '{print $7}'; } >>/tmp/2.txt done cat /tmp/2.txt |sort -k2 |less ###以下为输出### ./ibdata1 23:32:59 ./xtrabackup_logfile 23:32:59 #/tmp/1.txt >/tmp/2.txt find . -type f >/tmp/1.txt for i in `cat /tmp/1.txt` do { echo -n $i" ";statx $i 2>/dev/null |grep mtime |awk '{print $7}'; } >>/tmp/2.txt done cat /tmp/2.txt |sort -k2 |less ###以下为输出### ./ibdata1 23:33:00 ./mysql/engine_cost.ibd 23:33:00 ./mysql/gtid_executed.ibd 23:33:00 ./mysql/help_category.ibd 23:33:00 ./mysql/help_keyword.ibd 23:33:00 ./mysql/help_relation.ibd 23:33:00 ./mysql/help_topic.ibd 23:33:00 ./mysql/innodb_index_stats.ibd 23:33:00 ./mysql/innodb_table_stats.ibd 23:33:00 ./mysql/plugin.ibd 23:33:00 ... ./xtrabackup_logfile 23:33:09 #


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3