Could someone please guide for below logic? How can I exclude a few directories from source path while taking the backup?
#!/bin/bash
source=/home/ec2-user/source
dest=/home/ec2-user/destination
for file in $(find $source -printf "%P\n") ; do
if [ -a $dest/$file ] ; then
if [ $source/$file -nt $dest/$file ]; then
echo "Newer file detected, copying .."
cp -r $source/$file $dest/$file
else
echo "File $file exists, skipping"
fi
else
echo "$file is being copied over to $dest"
cp -r $source/$file $dest/$file
fi
done
And more script is added below , but while running this script i am getting an error like:
/home/ec2-user/source is being copied over to /home/ec2-user/destination/home/ec2-user/source
cp: omitting directory ‘//home/ec2-user/source’
/home/ec2-user/source/apple.txt is being copied over to /home/ec2-user/destination/home/ec2-user/source/apple.txt
cp: cannot create regular file ‘/home/ec2-user/destination/home/ec2-user/source/apple.txt//home/ec2-user/source/apple.txt’: No such file or directory
/home/ec2-user/source/durga.txt is being copied over to /home/ec2-user/destination/home/ec2-user/source/durga.txt
cp: cannot create regular file ‘/home/ec2-user/destination/home/ec2-user/source/durga.txt//home/ec2-user/source/durga.txt’: No such file or directory
/home/ec2-user/source/profiles is being copied over to /home/ec2-user/destination/home/ec2-user/source/profiles
cp: omitting directory ‘//home/ec2-user/source/profiles’
#!/bin/bash
source=/home/ec2-user/source
dest=/home/ec2-user/destination
find $source -path "$source/logs" -prune -o -exec sh -c '
dest=$0
for file; do
if [ -a "$dest/$file" ] ; then
if [ "$source/$file" -nt "$dest/$file" ]; then
echo "Newer file detected, copying .." >&2
cp "$source/$file" "$dest/$file"
else
echo "File $file exists, skipping" >&2
fi
else
echo "$file is being copied over to $dest" >&2
cp "$source/$file" "$dest/$file"
fi
done' "$dest" {} +
There are several problems with your scripts. They find not only files but also directories, which is not what you want. They don't create the destination directory before copying. The way you use find
is not robust and will fail if file names contain spaces (blanks, tabs, newlines...). Try something like (not tested):
#!/bin/bash
source=/home/ec2-user/source
dest=/home/ec2-user/destination
find "$source" -path "$source/logs" -prune -o -printf '%P\0' |
while IFS= read -r -d '' file; do
if [ -d "$src/$file" ]; then
if ! [ -d "$dst/$file" ]; then
echo "Creating directory $dst/$file"
mkdir -p "$dst/$file"
fi
continue
fi
if [ -a "$dest/$file" ] && [ "$source/$file" -ot "$dest/$file" ]; then
echo "File $file exists, skipping"
continue
fi
if [ -a "$dest/$file" ]; then
echo "Newer file detected, copying .."
else
echo "$file is being copied over to $dest"
fi
mkdir -p "$(dirname "$source/$file")"
cp -f "$source/$file" "$dest/$file"
done
Note that first building the lists of files and directories, next creating all directories at once and finally copying all files at once would probably be faster.
Note also that if you can use tar
or something equivalent to create an archive of all files to copy, and then unpack this archive in the destination directory, things would probably be easier and faster. Especially if you have GNU tar
because it has several handy options for backups.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.