Code: Select all
15 2 * * tue /sbin/fstrim -va |logger
I am not an expert in shell, but I have some comments.Tony0945 wrote:I wrote a shell script for the latter scenario but wound up doing the steps by hand. I'm bad at writing shell. Maybe you could fix it.
/usr/local/bin/cloneme
I would rewrite it asCode: Select all
TARGET="999.999.999" if [ "$#" -ne 1 ] ; then echo "ERROR: use target ip address as a parameter" && exit 1; fi TARGET=$1;
Code: Select all
IP_ADDRESS="${1}"
if [[ -z "${IP_ADDRESS}" ]]; then
echo "${0}: IP_ADDRESS argument not passed. Exiting 1." >&2
exit 1
fi1. Why do you prefer rsync to scp? The latter is vulnerable, are you aware about it?Code: Select all
scp -p -B -r /"$d" root@"$TARGET":/mnt/gentoo/ # rsync -av "$d" root@:"$TARGET":/mnt/gentoo/
Code: Select all
2 Code: Select all
3 TARGET="999.999.999"Code: Select all
7 echo "ERROR: use target ip address as a parameter" && exit 1;Code: Select all
$ /bin/sh -c 'echo && exit 5'; echo $?
5
$ /bin/sh -c 'echo && exit 5' >/dev/full; echo $?
/bin/sh: line 0: echo: write error: No space left on device
1
$ /bin/sh -c 'echo && exit 5; exit 0' >/dev/full; echo $?
/bin/sh: line 0: echo: write error: No space left on device
0Code: Select all
10 set +xCode: Select all
15 echo "Copying $TRAGET"Code: Select all
16 scp -p -B -r /"$d" root@"$TARGET":/mnt/gentoo/
17 # rsync -av "$d" root@:"$TARGET":/mnt/gentoo/Code: Select all
20 ln -s @"$TARGET":/mnt/gentoo/lib64 @"$TARGET":/mnt/gentoo/libCode: Select all
23 for P in $( dev proc sys tmp mnt root media )Code: Select all
25 mkdir -p root@"$TARGET":/mnt/gentoo/"$P"
27 chmod 0755 root@"$TARGET"/mnt/gentoo/homeFor completeness, I will note that while what you propose would work, it has two failure modes:halcon wrote:I would rewrite it asCode: Select all
IP_ADDRESS="${1}"
Code: Select all
$ /bin/sh -c 'echo $#;echo 1=$1'
0
1=
$ /bin/sh -u -c 'echo $#;echo 1=$1'
0
/bin/sh: $1: unbound variableAre you referring to the scp glob vulnerability? If so, I think that is not applicable here since he is copying to the peer, not from it. Additionally, that vulnerability requires a server with unusual filesystem contents. For the case in question here, where the operator fully controls both systems, I think it is reasonable to assume the server will not try to trigger the glob expansion vulnerability.halcon wrote:1. Why do you prefer rsync to scp? The latter is vulnerable, are you aware about it?
I generally discourage remote root login, but for the case described here, it is much simpler to configure. If the login is restricted to key files (not passwords), and only well trusted systems have the private key, I think remote root is acceptable.halcon wrote:2. I would never recommend to login remotely as root. A user login + rsync in sudoers would be much more safe.
i am going for this:Designing a partition scheme
Code: Select all
mount /dev/sda1 /mnt/old
mount /dev/sdc1 /mnt/new
mkdir /mnt/new/boot
mount /dev/sdc2 /mnt/new/boot
cp -a /mnt/old/. /mnt/new
Chrooting and Selecting a boot loader
Hu wrote:I suggest always starting with set -eu unless you have a good reason not to do so. These make the shell more strict by enabling the shell options errexit and nounset.
Thanks for the reminder! I've been thinking about set -eu for a long time but never used it because worried many things would be broken. May be, indeed, it's time to just try - what will be broken and what won'tHu wrote:For completeness, I will note that while what you propose would work, it has two failure modes:
- It will not warn the user for attempting to pass too many arguments.
- It is not compatible with the set -u I recommend earlier in my post. Attempting to read $1 fails under set -u when there are no arguments:
Code: Select all
$ /bin/sh -c 'echo $#;echo 1=$1' 0 1= $ /bin/sh -u -c 'echo $#;echo 1=$1' 0 /bin/sh: $1: unbound variable
Yes, I was referring to that. Okay then...Hu wrote:Are you referring to the scp glob vulnerability? If so, I think that is not applicable here since he is copying to the peer, not from it. Additionally, that vulnerability requires a server with unusual filesystem contents. For the case in question here, where the operator fully controls both systems, I think it is reasonable to assume the server will not try to trigger the glob expansion vulnerability.
Well... That's rightHu wrote:I generally discourage remote root login, but for the case described here, it is much simpler to configure. If the login is restricted to key files (not passwords), and only well trusted systems have the private key, I think remote root is acceptable.
Code: Select all
#!/bin/sh
#stage4.scr
# Also note: https://wiki.archlinux.org/index.php/rsync#Full_system_backup
mount /mnt/backup0
cd /
date > /mnt/backup0/stage4date.txt
tar cpf /mnt/backup0/stage4.tar.zst -I "zstd -19 -T0" --acls --xattrs --numeric-owner --no-wildcards-match-slash -X /home/USERNAME/bin/exclude.master /
date >> /mnt/backup0/stage4date.txt
cdCode: Select all
$ cat bin/exclude.master
dev/*
home/USERNAME/*
lost+found
media/*
mnt/*/*
proc/*
root/.cache/*
run/*
sys/*
tmp/*
var/tmp/*
var/cache/distfiles/*Code: Select all
rsync -av --delete-after --progress --stats /source/directory/ /destination/directory/I've tried. set -u is great, I like it!halcon wrote:I've been thinking about set -eu for a long time but never used it because worried many things would be broken. May be, indeed, it's time to just try - what will be broken and what won't![]()
Code: Select all
LC_ALL=C /bin/bash -c 'set -e; source /usr/local/bin/mclass_utilities.sh; var=$(get_newest_file_or_dir "file") || exit $?; echo "var = ${var}"'
Wrong number of arguments: 1 instead of 5.
Exiting 1.Code: Select all
LC_ALL=C /bin/bash -c 'set -e; set -x; source /usr/local/bin/mclass_utilities.sh; var=$(get_newest_file_or_dir "file") || exit $?; echo "var = ${var}"'
+ source /usr/local/bin/mclass_utilities.sh
++ get_newest_file_or_dir file
++ [[ 1 -ne 5 ]]
++ exit_err_1 'Wrong number of arguments: 1 instead of 5'
++ local '__arg_error=Wrong number of arguments: 1 instead of 5'
++ echo 'Wrong number of arguments: 1 instead of 5.
Exiting 1.'
Wrong number of arguments: 1 instead of 5.
Exiting 1.
++ exit 1
+ var=
+ exit 1Code: Select all
if [[ $# -ne 5 ]]; then
exit_err_1 'Wrong number of arguments: '"$#"' instead of 5'
fiCode: Select all
function exit_err_1 {
local __arg_error="${1}"
echo "${__arg_error}"'.
Exiting 1.' >&2
exit 1
}Code: Select all
set -e
if false; then
echo Nothing
fi
echo but we made it here
Indeed. I see now that set -x has nothing to do with this...Hu wrote:You are misreading the output. set -x shows you that the variable was assigned an empty value, then your explicit exit happens. Your echo is not hit. You can see this more clearly if you rewrite your echo to look less like the output of set -x.
Code: Select all
#!/bin/bash
set -eu
function step_clean_boot {
local __newest=$(get_newest_file_or_dir 'file' 'file') || exit $?
echo
ls -a /boot/ || exit $?
}
step_clean_boot
exit 0Code: Select all
LC_ALL=C kerneler_test.sh
/usr/local/bin/kerneler_test.sh: line 6: get_newest_file_or_dir: command not found
. .. .keep grubCode: Select all
#!/bin/bash
set -eu
source /usr/local/bin/mclass_utilities.sh
function step_clean_boot {
local __newest=$(get_newest_file_or_dir 'file' 'file') || exit $?
echo
ls -a /boot/ || exit $?
}
step_clean_boot
exit 0Code: Select all
LC_ALL=C kerneler_test.sh
Wrong number of arguments: 2 instead of 5.
Exiting 1.
. .. .keep grubCode: Select all
The return status
is zero unless 'local' is used outside a function, an invalid NAME
is supplied, or NAME is a readonly variable.Code: Select all
f() {
local a
a=$(expression_that_may_fail)
}Ah! I fell for it again... (knew but forgot)Hu wrote:local succeeds, so there is no error to trigger the exit.
Found one of the good reasons for not using set -u:Hu wrote:I suggest always starting with set -eu unless you have a good reason not to do so.
Code: Select all
echo "${NO_SUCH:-default}"
: "${USUALLY_UNDEFINED:=abc}"
echo "$USUALLY_UNDEFINED"hedmo wrote:i am following the handbook with :i am going for this:Designing a partition scheme
after that the handbook with :Code: Select all
mount /dev/sda1 /mnt/old mount /dev/sdc1 /mnt/new mkdir /mnt/new/boot mount /dev/sdc2 /mnt/new/boot cp -a /mnt/old/. /mnt/new
Chrooting and Selecting a boot loader