-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-saver.sh
executable file
·91 lines (70 loc) · 1.71 KB
/
config-saver.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
if [ ! -f tosave ]
then
echo "File 'tosave' is not present."
exit 1
fi
name=`head -n 1 tosave` # folder where files will be copied
if [ ! -e $name ]
then
mkdir $name
elif [ ! -d $name ]
then
echo "A file named $name already exists."
exit 1
else
rm -rf $name # to ease the update of files, start with removing the folder
mkdir $name
fi
# Check we are allowed to copy all files:
for file in `tail -n +2 tosave`
do
if [ ! -e $file ]
then
echo "Warning: $file doesn't exist."
elif [ ! -r $file ]
then
echo "Can't read $file. Try to execute $0 with root user ?"
exit 1
fi
done
dest=`readlink -f $name` # full path of saving folder
# Symbolic links are not used during copying, because Git stores them as symbolic
# links and not as plain text files (by default).
for file in `tail -n +2 tosave`
do
echo "Copying $file..."
if [ ! -e $file ]
then
continue
elif [ -d $file ]
then
cp -r --parents $file $dest
else
cp --parents $file $dest
fi
done
echo "Saving list of installed package..."
dpkg -l > $dest/packages.list
function save_cron_of_user () {
cron=`crontab -u $1 -l 2>&1`
if [[ $cron != no* ]]
then
echo $1 >> $dest/crons
echo "$cron" | tail -n+22 >> $dest/crons
fi
}
echo "Saving installed CRONs..."
if [[ "$(id -u)" != "0" ]]; then
echo "!! Script must be launched as root to save CRONs of other users. !!"
save_cron_of_user $USER
else
for user in $(cut -f1 -d: /etc/passwd);
do
save_cron_of_user $user
done
# If we are executed as root, give files to the initial user, to ease later file manipulation:
user=${SUDO_USER:-$(whoami)}
chown -R $user:$user $dest
fi
echo "Files copied. Don't forget to commit !"