Backup VDS and Dedicated Servers (Linux)

Below are two editions of the script for organizing data backup (backup) with VDS Linux. Both versions once a week, on Sundays, create a complete copy of all data and the MySQL database, and in the rest - only incremental archives. To run automatically, add a line like the following to the cron job manager:

10 0 * /root/bin/backup.sh >/dev/null 2>&1

For the scripts to work, the amount of free disk space in the system must exceed the size of the copied data. ободного места на диске в системе превышал размер копируемых данных.

Backup to FTP server

Initially assign values to several variables. DIRS - space-separated list of directories for backup. MUSER and MPASS - MySQL DBMS administrator data for the connection. FTPU and FTPP - user data for connecting to the FTP server. FTPS is the name of the FTP server. EMAILID - email address to which error reports will be sent. For correct operation, the ncftp client must be installed on the system:
#!/bin/sh

### System Setup ###
DIRS="/etc /home /var/www"
BACKUP=/tmp/backup.$$
NOW=$(date +"%d-%m-%Y")
INCFILE="/root/tar-inc-backup.dat"
DAY=$(date +"%a")
FULLBACKUP="Sun"

### MySQL Setup ###
MUSER="root"
MPASS="password"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"

### FTP server Setup ###
FTPD="/backup/incremental"
FTPU="user"
FTPP="password"
FTPS="ftp1.sim-networks.com"
NCFTP="$(which ncftpput)"

### Other stuff ###
EMAILID="user@yourdomain.com"

### Start Backup for file system ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :

### See if we want to make a full backup ###
if [ "$DAY" == "$FULLBACKUP" ]; then
FTPD="/backup/full"
FILE="fs-full-$NOW.tar.gz"
tar -zcvf $BACKUP/$FILE $DIRS
else
i=$(date +"%Hh%Mm%Ss")
FILE="fs-i-$NOW-$i.tar.gz"
tar -g $INCFILE -zcvf $BACKUP/$FILE $DIRS
fi

### Start MySQL Backup ###
# Get all databases name
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done

### Dump backup using FTP ###
#Start FTP backup using ncftp
ncftp -u"$FTPU" -p"$FTPP" $FTPS<<EOF
mkdir $FTPD
mkdir $FTPD/$NOW
cd $FTPD/$NOW
lcd $BACKUP
mput
quit
EOF

### Find out if ftp backup failed or not ###
if [ "$?" == "0" ]; then
rm -f $BACKUP/

else
T=/tmp/backup.fail
echo "Date: $(date)">$T
echo "Hostname: $(hostname)" >>$T
echo "Backup failed" >>$T
mail -s "BACKUP FAILED" "$EMAILID" <$T
rm -f $T
fi

Colocation

Colocation

Find out how to store your server in secure data center in Germany

Read More

Backup to local disk, NFS, iSCSI

Consider the variables that need to be configured. DIRS - space-separated list of directories for backup. MUSER and MPASS - MySQL DBMS administrator data for the connection. MNTD is the directory where the backup storage will be organized. This can be just a directory on the local hard drive, or an NFS or iSCSI network storage mounted at this point: #!/bin/sh

### System Setup ###
DIRS="/etc /home /var/www"
BACKUP=/tmp/backup.$$
NOW=$(date +"%d-%m-%Y")
INCFILE="/root/tar-inc-backup.dat"
DAY=$(date +"%a")
FULLBACKUP="Sun"

### MySQL Setup ###
MUSER="root"
MPASS="password"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"

### Datastore Setup ###
MNTD="/mnt"
DSD="backup/incremental"

### Start Backup for file system ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :

### See if we want to make a full backup ###
if [ "$DAY" == "$FULLBACKUP" ]; then
DSD="backup/full"
FILE="fs-full-$NOW.tar.gz"
tar -zcvf $BACKUP/$FILE $DIRS
else
i=$(date +"%Hh%Mm%Ss")
FILE="fs-i-$NOW-$i.tar.gz"
tar -g $INCFILE -zcvf $BACKUP/$FILE $DIRS
fi

### Start MySQL Backup ###
# Get all databases name
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done

mkdir -p $MNTD/$DSD/$NOW
mv -f $BACKUP/ $MNTD/$DSD/$NOW
rm -f $BACKUP/

Was this article helpful?

Tags:

#server

Did you like the article?

Cookie consent

By clicking «I agree», you consent to our website's use of cookies to give you the most relevant experience by remembering your preferences and repeat visits. However, you may visit «Manage сookies» to provide controlled consent. Learn more

Cookies settings

functional

Necessary cookies are crucial for the basic functions of the website and the website will not work in its intended way without them.

Analytics

Analytical cookies are used to understand how visitors interact with the website.

Advertisement

Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns.