Simple Backup Shell Script

Here comes an example of a simple backup shell script that “tar“s the specified directory to an archive and uploads it on a FTP server using curl. Afterwards the local backup file will be removed. The only thing you have to do is define at least BACKUP_DATA, BACKUP_SERVER, BACKUP_USERNAME and BACKUP_PASSWORD.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/sh
 
# Name of the backup file
BACKUP_NAME="backup.tar.gz"
 
# Directory of the backup file
BACKUP_PATH="/tmp/"
 
# Directory + Name
BACKUP=${BACKUP_PATH}${BACKUP_NAME}
 
# Backup data
BACKUP_DATA="/home/username"
 
# Backup server
BACKUP_SERVER="ftp.backup.tld"
 
# Username and password
BACKUP_USERNAME="username"
BACKUP_PASSWORD="password"
 
# Execute backup
tar --exclude=${BACKUP} -czf ${BACKUP} ${BACKUP_DATA}
if [ "$?" -ne "0" ]; then
  echo "tar: Backup failed!"
  exit 1
fi
 
curl --upload-file ${BACKUP} ftp://${BACKUP_SERVER}\
--user ${BACKUP_USERNAME}:${BACKUP_PASSWORD}
if [ "$?" -ne "0" ]; then
  echo "curl: Backup failed!"
  exit 1
fi
 
rm ${BACKUP}
echo "Backup (" $(date) ") successful!"

0 Responses to “Simple Backup Shell Script”


  • No Comments

Leave a Reply