-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup.sh
45 lines (41 loc) · 1.02 KB
/
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash
cd /home/margarita
dateFiles="$(ls | grep -E "^Backup-" | sort -n | tail -1 | cut -d "-" -f 2,3,4)"
ifCanmkdir=0
dateNow="$(date +%Y-%m-%d)"
secondsPrev="$(date -d "$dateFiles" +%s)"
secondsNow="$(date -d "$dateNow" +%s )"
let ifCanmkdir=($secondsNow-$secondsPrev)/60/60/24
if [[ $ifCanmkdir -gt 7 ]]
then
mkdir "Backup-$dateNow"
cd ~/source
listInSource="$(ls)"
echo "$listInSource"
for i in $listInSource
do
cp -R $i $HOME/Backup-$dateNow
done
cd $HOME
echo "Directory Backup-$dateNow was created. Files: $listInSource" >> backup-report
else
cd ~/source
listInSource="$(ls)"
cd $HOME/Backup-$dateFiles
for i in $listInSource
do
if [ -f "$i" ]
then
sizeInSource="$(ls -l -- ~/source/$i | awk '{print$5}')"
sizeInBackup="$(ls -l $i | awk '{print$5}')"
if [[ $sizeInSource -ne $sizeInBackup ]]
then
mv $i $i-$dateNow
cp -R -- ~/source/$i $HOME/Backup-$dateFiles
echo "rename $i to $i-$dateNow" >> $HOME/backup-report
fi
else
cp -R ~/source/$i $HOME/Backup-$dateFiles
fi
done
fi