Backup directories with space in directory name

Jack McGee jack at greendesk.net
Sat Dec 31 03:47:53 UTC 2016


I am trying to use this perl script to backup a directory, but it fails 
with this error:

jmcgee at jmcgee-desktop:~$ perl backupsystemrotate.pl
Backing up /mnt/ntfs-d/My Documents to 
/mnt/media3/backups/desktop/jmcgee-desktop-Friday.tgz
Fri Dec 30 21:42:57 CST 2016

tar: Removing leading `/' from member names
tar: /mnt/ntfs-d/My: Cannot stat: No such file or directory

and then it proceeds to backup a directory called Documents.

Adding single quotes to the line inside the double quotes gives this error:

Backing up '/mnt/ntfs-d/My Documents' to 
/mnt/media3/backups/desktop/jmcgee-desktop-Friday.tgz
Fri Dec 30 21:45:43 CST 2016

tar: '/mnt/ntfs-d/My: Cannot stat: No such file or directory
tar: Documents': Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

Backup finished


Using a \ instead of the space also gives another error.


jmcgee at jmcgee-desktop:~$ cat backupsystemrotate.pl
#!/bin/bash
####################################
#
# Backup to NFS mount script with
# grandfather-father-son rotation.
#
####################################

# What to backup.
backup_files="/mnt/ntfs-d/My Documents"

# Where to backup to.
dest="/mnt/media3/backups/desktop"

# Setup variables for the archive filename.
day=$(date +%A)
hostname=$(hostname -s)

# Find which week of the month 1-4 it is.
day_num=$(date +%d)
if (( $day_num <= 7 )); then
         week_file="$hostname-week1.tgz"
elif (( $day_num > 7 && $day_num <= 14 )); then
         week_file="$hostname-week2.tgz"
elif (( $day_num > 14 && $day_num <= 21 )); then
         week_file="$hostname-week3.tgz"
elif (( $day_num > 21 && $day_num < 32 )); then
         week_file="$hostname-week4.tgz"
fi

# Find if the Month is odd or even.
month_num=$(date +%m)
month=$(expr $month_num % 2)
if [ $month -eq 0 ]; then
         month_file="$hostname-month2.tgz"
else
         month_file="$hostname-month1.tgz"
fi

# Create archive filename.
if [ $day_num == 1 ]; then
     archive_file=$month_file
elif [ $day != "Saturday" ]; then
         archive_file="$hostname-$day.tgz"
else
     archive_file=$week_file
fi

# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo

# Backup the files using tar.
tar czf $dest/$archive_file $backup_files

# Print end status message.
echo
echo "Backup finished"
date

# Long listing of files in $dest to check file sizes.
ls -lh $dest/






More information about the ubuntu-users mailing list