使用 WSL 进行 Windows 下不同磁盘之间的文件备份

modified.sh

1
2
3
4
5
6
7
8
#!/bin/bash

sourceDir=$1
find $1 -type f -mmin -60 | \
awk \
'BEGIN { "date \"+%F %T\""| getline; print $0 } \
{ print "modified:"$0 } \
END { print "\n\n"}'

backup.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
# copy source to target
#
# system(cmdrm);
baseDir="/mnt/e/SoftConfigBackup"
awk -F"=" '!/^\s*#/ \
{ origDir=$1; \
cmd0=bd"/modified.sh "$1" >> "bd"/modified.log"; \
system(cmd0); \
sub("^.*/", "", origDir); \
cmdrm="rm -rf "$2"/"origDir; \
cmd1="mkdir -p "$2; \
cmd2="cp -r " $1 " " $2; \
system(cmd1); \
system(cmd2) }' \
bd=$baseDir "$baseDir/backupToNewDisk.properties"

date >>$baseDir/date.txt
1
2
3
crontab -l

*/60 * * * * /mnt/e/SoftConfigBackup/backup.sh

文件目录大小排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env bash

# 当前目录下,根据文件或目录的大小进行降序排序, 需要有对应目录的权限

# set current directory as default directory
dir=${1:-$(pwd)}
dir=$(echo $dir | sed -E 's/\/$//g')

IFSOld=$IFS
IFS=$'\n'
rs=""
for i in $(ls "${dir}"); do
fpath="${dir}/$i"
if [ -f "$fpath" ]; then
size=$(du -B 1G "$fpath" | cut -f1)
rs=$rs"\n[f]\x1c${size}\x1c$fpath"
elif [ -d "$fpath" ]; then
size=$(du -B 1G -c "$fpath" | grep total | cut -f1)
rs=$rs"\n[d]\x1c${size}\x1c$fpath"
fi
done
echo "block size = 1GB"
echo -e "\nFLG Block Path"
echo -e "-----------------------------------------------"
echo -e $rs | sort -t $'\x1c' -n -r -k2 | tr $'\x1c' ' '
IFS=$IFSOld

it also works

1
sep=$(echo -n -e '\x1c'); echo -e '2\x1cB\n1\x1cD' | sort -t $sep -n -k1
1
2
cat /dev/null > testfile
# 由于/dev/null文件不含有任何内容,通常用它来快速清除现有文件中的数据,而不用先删除文件再重新创建

重定向

1
2
3
4
5
6
7
8
9
10
11
12
$ IFS=','; while read name age; do cat >> p.sql << EOF
> insert into table (name, age) values ('$name', '$age');
> EOF
> done < a.csv

$ cat p.sql
insert into table (name, age) values ('Jason', '24');
insert into table (name, age) values ('Guo', '29');

$ cat a.csv
Jason,24
Guo,29