In reply to your first question, it could be a matter of any of the following, or any combination thereof…
- the speed of the SD card
- the speed of the bus that the SD card is connected to
- the amount of available RAM versus the total size of the files being copied
In reply to your second question, first of all, there is a syntax error in your script, namely in the cp
command. You are not telling cp
which of the three directories on its command line are the source and which is the destination, and you’re also missing the trailing slash on the destination directory ─ see…
man cp
… for details. Note the -t
option.
Your script is also inefficient. For instance, you are trying to copy all the files to the SD card, then archive and compress them with the SD card as both the source medium and destination medium ─ please also note that SD cards do not support TRIM and that such an operation therefore creates write amplification ─ which is bound to be slow. You would be much better off using /tmp
for the copying, archiving and compressing, because /tmp
is a tmpfs
, i.e. a virtual-memory-based filesystem.
So, allow me to rewrite your script more efficiently and more safely…
#!/bin/sh
#
# We will invoke "sh" as the shell, instead of "bash". In Manjaro, this
# will still invoke GNU Bash, but in a more portable form ─ i.e. in
# strict Bourne Shell mode, so that the script can also be executed on
# machines that have a traditional Bourne Shell and do not have GNU
# Bash installed.
# First, let's see whether we're in our home directory, and if not,
# we'll "cd" into it.
if [ ! "${PWD}" = "/home/user" ]
then
cd "/home/user"
fi
# Now we test for the existence of a backup directory in /tmp, and
# if it doesn't exist, we'll create it, and then we'll copy the desired
# contents over from our home directory to the backup directory
# by way of a simple loop.
if [ ! -d /tmp/user/backups ]
then
mkdir -p /tmp/user/backups
for dir in Documents Downloads scripts
do
cp -r /home/user/${dir} /tmp/user/backups/
done
fi
# Now it's time to create our archive. Note that we are not creating
# the archive in the same directory as the files it will contain.
tar -czf /tmp/user/backup.tar.gz /tmp/user/backups/*
# Since it is not clear whether you really want to do things this way,
# I am following your logic of deleting a pre-existing archive file
# on the SD card and creating a new one, but strictly speaking, this is
# redundant, because a pre-existing archive will either way get
# overwritten by the copy from "/tmp".
#
# Also do note that a "tar" archive can be appended to ─ see the
# "man" page. ;)
if [ -d /run/media/user/340C-1CEB ]
then
if [ -f /run/media/user/340C-1CEB/backup.tar.gz ]
rm -f /run/media/user/340C-1CEB/backup.tar.gz
fi
cp /tmp/user/backup.tar.gz /run/media/user/340C-1CEB/
rm -rf /tmp/user/backup*
fi
# That's all, folks! :p
The above script has not been tested, but should work. If you find any errors in its execution, then feel free to let me know and then I’ll fix them.
The above script could also be improved in efficiency by skipping the step of copying the source files to /tmp
, because if the archive on the SD card were to be extracted, then it will now extract the files to /tmp
again, instead of to your home directory. However, I will leave that to you as an exercise.