Score:0

I'm trying to make a simple shell script to backup files on the same machine but I keep running into errors I don't know how to resolve

us flag

Ok, so I use an Ubuntu server 18.04 for my small business and I have very minimal knowledge outside of basic security and running what I need to run. So, apologies if this is a really silly issue.

I want to set up a simple backup script that backups files and stores them in another directory on the same machine.

To do this, I followed this example by Ubuntu: https://ubuntu.com/server/docs/backups-shell-scripts

I created the relevant directories and updated the script to this:

#!/bin/bash
####################################
#
# Backup to NFS mount script.
#
####################################

# What to backup. 
backup_files="/home/server/testserver/logs"

# Where to backup to.
dest="/home/server/backups"

# Create archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tgz"

# 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

I saved the bash inside "/home/server/backups" and then tried to run it on some test logs. However, I get this error:

-bash: ./backup.sh: /bin/bash^M: bad interpreter: No such file or directory

I researched this online and tried running the file as "bash backup.sh" and removing the "#!/bin/bash" line altogether, both resulted in this:

backup.sh: line 7: $'\r': command not found
backup.sh: line 10: $'\r': command not found
backup.sh: line 13: $'\r': command not found
backup.sh: line 18: $'\r': command not found
.tgzday2427erver/backupstestserver/logs
backup.sh: line 21: $'date\r': command not found
backup.sh: line 22: $'echo\r': command not found
backup.sh: line 23: $'\r': command not found
tar: Removing leading `/' from member names
tar: /home/server/testserver/logs\r\r: Cannot stat: No such file or directory
tar (child): /home/server/backups\r/ns31202427\r-Friday\r.tgz\r: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
backup.sh: line 26: $'\r': command not found
backup.sh: line 28: $'echo\r': command not found
Backup finished
backup.sh: line 30: $'date\r': command not found
backup.sh: line 31: $'\r': command not found
ls: cannot access '/home/server/backups'$'\r': No such file or directory

So can anyone explain what is happening here and help me resolve the issue so I can safely backup my server? :)

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.